kernel中的per_CPU variable用法
Reason to use per-CPU variable
1 Remember that the 2.6 kernel is preemptible; it would not do for a processor to be preempted in
the middle of a critical section that modifies a per-CPU variable.
2 It also would not be good if your process were to be moved to another processor in the middle of a per-
CPU variable access.
How to access per-CPU variable
1 get_cpu_var(sockets_in_use)++;
2 put_cpu_var(sockets_in_use);
3
4 /* Must be an lvalue. */
5 #define get_cpu_var(var) (*({ preempt_disable(); &__get_cpu_var(var); }))
6 #define put_cpu_var(var) preempt_enable()
这里的有3个牛叉的地方:
1 &__get_cpu_var(var); ------- 直接没有临时变量,直接就搞定。
2 *({...}); ------- 说明{}的返回值,果然就是最后一条语句的返回值。
3 跟1有相同之处 get_cput_var(sockets_in_use)++; 不用临时变量,直接++之。