kernel中的per_CPU variable用法

本文介绍了在2.6内核中使用每CPU变量的原因及重要性,避免了在一个关键区域修改每CPU变量时被抢占的情况发生。同时,也讨论了在访问每CPU变量过程中可能出现的问题,并提供了具体的实现方法。

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)++; 不用临时变量,直接++之。

转载于:https://www.cnblogs.com/jack204/archive/2011/11/21/2256902.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值