forked from sysprog21/lkmpg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_spinlock.c
62 lines (47 loc) · 1.4 KB
/
example_spinlock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* example_spinlock.c
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/printk.h>
#include <linux/spinlock.h>
static DEFINE_SPINLOCK(sl_static);
static spinlock_t sl_dynamic;
static void example_spinlock_static(void)
{
unsigned long flags;
spin_lock_irqsave(&sl_static, flags);
pr_info("Locked static spinlock\n");
/* Do something or other safely. Because this uses 100% CPU time, this
* code should take no more than a few milliseconds to run.
*/
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");
/* Do something or other safely. Because this uses 100% CPU time, this
* code should take no more than a few milliseconds to run.
*/
spin_unlock_irqrestore(&sl_dynamic, flags);
pr_info("Unlocked dynamic spinlock\n");
}
static int __init example_spinlock_init(void)
{
pr_info("example spinlock started\n");
example_spinlock_static();
example_spinlock_dynamic();
return 0;
}
static void __exit example_spinlock_exit(void)
{
pr_info("example spinlock exit\n");
}
module_init(example_spinlock_init);
module_exit(example_spinlock_exit);
MODULE_DESCRIPTION("Spinlock example");
MODULE_LICENSE("GPL");