2021-07-22 11:25:32 +08:00
|
|
|
/*
|
2021-08-08 01:24:59 +08:00
|
|
|
* example_spinlock.c
|
2021-07-22 11:25:32 +08:00
|
|
|
*/
|
2021-07-22 06:58:13 +08:00
|
|
|
#include <linux/init.h>
|
2021-07-22 06:35:24 +08:00
|
|
|
#include <linux/module.h>
|
2023-02-23 18:41:23 +08:00
|
|
|
#include <linux/printk.h>
|
2021-07-22 06:35:24 +08:00
|
|
|
#include <linux/spinlock.h>
|
|
|
|
|
2021-09-04 17:53:29 +08:00
|
|
|
static DEFINE_SPINLOCK(sl_static);
|
|
|
|
static spinlock_t sl_dynamic;
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
static void example_spinlock_static(void)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&sl_static, flags);
|
|
|
|
pr_info("Locked static spinlock\n");
|
|
|
|
|
2021-08-08 01:24:59 +08:00
|
|
|
/* Do something or other safely. Because this uses 100% CPU time, this
|
|
|
|
* code should take no more than a few milliseconds to run.
|
|
|
|
*/
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
spin_unlock_irqrestore(&sl_static, flags);
|
|
|
|
pr_info("Unlocked static spinlock\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void example_spinlock_dynamic(void)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_init(&sl_dynamic);
|
|
|
|
spin_lock_irqsave(&sl_dynamic, flags);
|
|
|
|
pr_info("Locked dynamic spinlock\n");
|
|
|
|
|
2021-08-08 01:24:59 +08:00
|
|
|
/* Do something or other safely. Because this uses 100% CPU time, this
|
|
|
|
* code should take no more than a few milliseconds to run.
|
|
|
|
*/
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
spin_unlock_irqrestore(&sl_dynamic, flags);
|
|
|
|
pr_info("Unlocked dynamic spinlock\n");
|
|
|
|
}
|
|
|
|
|
2023-07-05 09:44:21 +08:00
|
|
|
static int __init example_spinlock_init(void)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
|
|
|
pr_info("example spinlock started\n");
|
|
|
|
|
|
|
|
example_spinlock_static();
|
|
|
|
example_spinlock_dynamic();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-05 09:44:21 +08:00
|
|
|
static void __exit example_spinlock_exit(void)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
|
|
|
pr_info("example spinlock exit\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(example_spinlock_init);
|
|
|
|
module_exit(example_spinlock_exit);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("Spinlock example");
|
|
|
|
MODULE_LICENSE("GPL");
|