set_task_state()与__set_task_state()的区别

#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()这一函数的功能是基本上一致的,只是两者所作用的对象的不同而已:

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值