#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()則沒有,當狀態state是RUNNING時,因爲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()這一函數的功能是基本上一致的,只是兩者所作用的對象的不同而已:
此函數對應的是對所選定的進程進行設置,而後者是對當前進程進行設置。