先看一下代码:
void sleep_on(struct task_struct **p)
{
struct task_struct *tmp;
if (!p)
return;
if (current == &(init_task.task))
panic("task[0] trying to sleep");
tmp = *p;
*p = current;
current->state = TASK_UNINTERRUPTIBLE;
schedule();
if (tmp)
tmp->state=0;
}
void interruptible_sleep_on(struct task_struct **p)
{
struct task_struct *tmp;
if (!p)
return;
if (current == &(init_task.task))
panic("task[0] trying to sleep");
tmp=*p;
*p=current;
repeat: current->state = TASK_INTERRUPTIBLE;
schedule();
if (*p && *p != current) {
(**p).state=0;
goto repeat;
}
*p=NULL;
if (tmp)
tmp->state=0;
}
void wake_up(struct task_struct **p)
{
if (p && *p) {
(**p).state=0;
*p=NULL;
}
}
先看函数,函数实现很简单。原理有一些抽象,这里先说一下sleep_on函数的应用。
sleep_on函数中只要涉及了:*p,*tmp,current三个人物指针操作。
其中*p表示等待队列头指针,如文件系统i节点的i_wait指针,内存缓冲区操作中的buffer_wait指针;*tmp是临时指针,current代表当前任务指针。
sleep_on