set_task_state()与__set_task_state()的区别

来源:互联网 发布:魔方矩阵 编辑:程序博客网 时间:2024/05/21 10:39

#define__set_task_state(tsk, state_value) \
do { (tsk)->state =(state_value); } while (0)

#defineset_task_state(tsk, state_value) \
set_mb((tsk)->state,(state_value))

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

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lm_tom/archive/2008/05/16/2453087.aspx

下面是对函数set_task_state()和函数set_current_state()的解析。

函数Set_task_state()

#define__set_task_state(tsk, state_value) \

do{ (tsk)->state = (state_value); } while (0)//这个函数有别于set_taskl_state(tsk,state_value),因为前者

没有使用mb()这样的一个函数,而仅仅是设置了state这个变量值,对于保护内存事件发生的次序根本就没有执行。

所以,后者更加具有安全性。

#defineset_task_state(tsk, state_value) \

set_mb((tsk)->state,(state_value))

这里要深入的解释函数set_mb((tsk)->state,(state_value)):

#defineset_mb(var, value) do { var = value; mb(); } while (0)

#definemb() __asm__ __volatile__ ("" :::"memory")//这个函数所实现的功能就是barrior()

功能是PC采用内存一致性模型,使用mb强加的严格的CPU内存事件次序,保证程序的执行看上去就象是遵循顺序

一致性(SC)模型,当然,即使对于UP,由于内存和设备见仍然有一致性问题,这些MB也是必须的。

Set_current_state()函数:

下面是对set_current_state()函数的一个简要的解析:

/*

*set_current_state() includes a barrier so that the write ofcurrent->state

*is correctly serialised wrt the caller's subsequent test of whetherto

*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)//这个函数的两者类似于上面的函数

#defineset_current_state(state_value)

set_mb(current->state,(state_value))

//这个函数的功能和set_task_state()这一函数的功能是基本上一致的,只是两者所作用的对象的不同而已:

此函数对应的是对所选定的进程进行设置,而后者是对当前进程进行设置。


原创粉丝点击