3 * Sample module for local.h usage.
8 #include <linux/module.h>
9 #include <linux/timer.h>
11 static DEFINE_PER_CPU(local_t
, counters
) = LOCAL_INIT(0);
13 static struct timer_list test_timer
;
15 /* IPI called on each CPU. */
16 static void test_each(void *info
)
18 /* Increment the counter from a non preemptible context */
19 printk("Increment on cpu %d\n", smp_processor_id());
20 local_inc(&__get_cpu_var(counters
));
22 /* This is what incrementing the variable would look like within a
23 * preemptible context (it disables preemption) :
25 * local_inc(&get_cpu_var(counters));
26 * put_cpu_var(counters);
30 static void do_test_timer(unsigned long data
)
34 /* Increment the counters */
35 on_each_cpu(test_each
, NULL
, 0, 1);
36 /* Read all the counters */
37 printk("Counters read from CPU %d\n", smp_processor_id());
38 for_each_online_cpu(cpu
) {
39 printk("Read : CPU %d, count %ld\n", cpu
,
40 local_read(&per_cpu(counters
, cpu
)));
42 del_timer(&test_timer
);
43 test_timer
.expires
= jiffies
+ 1000;
44 add_timer(&test_timer
);
47 static int __init
test_init(void)
49 /* initialize the timer that will increment the counter */
50 init_timer(&test_timer
);
51 test_timer
.function
= do_test_timer
;
52 test_timer
.expires
= jiffies
+ 1;
53 add_timer(&test_timer
);
58 static void __exit
test_exit(void)
60 del_timer_sync(&test_timer
);
63 module_init(test_init
);
64 module_exit(test_exit
);
66 MODULE_LICENSE("GPL");
67 MODULE_AUTHOR("Mathieu Desnoyers");
68 MODULE_DESCRIPTION("Local Atomic Ops");
This page took 0.029682 seconds and 4 git commands to generate.