feisky

云计算、虚拟化与Linux技术笔记
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Linux Kernel Development -- 设置当前进程的状态

Posted on 2013-03-04 18:44  feisky  阅读(1697)  评论(0编辑  收藏  举报

首先查看内核源码中是如何定义的

#define __set_task_state(tsk, state_value) \ 
do { (tsk)->state = (state_value); } while (0) 
#define set_task_state(tsk, state_value) \ 
set_mb((tsk)->state, (state_value)) 
/* 
* set_current_state() includes a barrier so that the write of current->state 
* is correctly serialised wrt the caller's subsequent test of whether to 
* actually sleep: 
* 
* set_current_state(TASK_UNINTERRUPTIBLE); 
* if (do_i_need_to_sleep()) 
* schedule(); 
* 
* If the caller does not need such serialisation then use __set_current_state()
*/ 
#define __set_current_state(state_value) \ 
do { current->state = (state_value); } while (0) 
#define set_current_state(state_value) \ 
set_mb(current->state, (state_value)) 

 

如果是单处理器,则 set_mb 的定义为(asm-x86/system_32.h):

#define set_mb(var, value) do { var = value; barrier(); } while (0)

在对 current->state 复制后,简单的用 barrier() 刷新一下内存和寄存器之间的关系。

如果是多处理器 SMP,则 set_mb 的定义为:

#define set_mb(var, value) do { (void) xchg(&var, value); } while (0)

xchg(&var, value) 定义为(asm-x86/cmpxchg_32.h):
#define xchg(ptr,v) ((__typeof__(*(ptr)))__xchg((unsigned long)(v),(ptr),sizeof(*(ptr))))

上面,(__typeof__(*(ptr))) 获得 *ptr 的类型,因为 *ptr 就是 var,而 var 的类型为 long 型,所以这里也是对 __xchg() 返回的类型强制转换为 long 型。

__xchg() 被定义为:

/*
* Note: no "lock" prefix even on SMP: xchg always implies lock anyway
* Note 2: xchg has side effect, so that attribute volatile is necessary,
* but generally the primitive is invalid, *ptr is output argument. --ANK
*/
static inline unsigned long __xchg(unsigned long x, volatile void * ptr, int size)
{
switch (size) {
case 1:
__asm__ __volatile__("xchgb %b0,%1"
:"=q" (x)
:"m" (*__xg(ptr)), "0" (x)
:"memory");
break;
case 2:
__asm__ __volatile__("xchgw %w0,%1"
:"=r" (x)
:"m" (*__xg(ptr)), "0" (x)
:"memory");
break;
case 4:
__asm__ __volatile__("xchgl %0,%1"
:"=r" (x)
:"m" (*__xg(ptr)), "0" (x)
:"memory");
break;
}
return x;
}

上面,根据 __xchg 第 3 个表示长度的参数来使用 xchg 指令将要设置的新 state 的值交换到 current->state 中。


set_current_state()与set_task_state()


set_current_state(state)等价于set_task_state(current, state)

__set_current_state()与set_current_state()

set_task_state()带有一个memory barrier,__set_task_state()则没有,当状态state是RUNNING时,因为scheduler可能访问这个state,因 此此时要变成其他状态(如INTERRUPTIBLE),就要用set_task_state();
而当state不是RUNNING时,因为没有其他人会 访问这个state,因此可以用__set_task_state().
所以用set_task_state()肯定是安全的,但 __set_task_state()可能会快些。


set_current_state() 的使用


在驱动程序中,进程睡眠往往通过 3 个步骤进行:
1. 将进程加入等待队列中。
2. 然后使用 set_current_state() 来设置进程的状态,设置的状态为 TASK_INTERRUPTIBLE 或 TASK_UNINTERRUTIBLE 。
3. 上面的设置完后,我们就要放弃处理器了。但在放弃处理器之前,还有一件重要的事情需要做:检查睡眠等待的条件。如果不检查,如果此时条件正好变为真,那么就漏掉了继续运行的机会,从而会睡眠更长的时间。

所以,一般在睡前需要类似的动作:

set_current_state(TASK_UNINTERRUPTIBLE);
if (do_i_need_to_sleep())
schedule();


参考:
http://blog.21ic.com/user1/6406/archives/2010/73685.html
http://www.groad.net/bbs/simple/?t3279.html
Linux Kernel Development

无觅相关文章插件,快速提升流量