首页 > 代码库 > 等待队列_睡眠和唤醒

等待队列_睡眠和唤醒

1.定义并初始化一个等待队列头:
DECLARE_WAIT_QUEUE_HEAD(wait_que);

wait_queue_head_t wait_que;
init_waitqueue_head( &wait_que);

2.简单休眠:
wait_event(wait_que, condition)
wait_event_timeout(wait_que, condition, timeout)
wait_event_interruptible(wait_que, condition)
wait_event_interruptible_timeout(wait_que, condition, timeout)
...

还有好多wait系列的函数;
它们的实现主体架构一致:
DEFINE_WAIT(__wait);
prepare_to_wait(&wq, &__wait, TASK_KILLABLE);
schedule();
finish_wait(&wq, &__wait);
只是其中参数有些差异,处理过程略有不同

3.高级休眠(手动休眠):
方式一:
DEFINE_WAIT(my_wait);

wait_queue_t my_wait;
init_wait(&my_wait);

prepare_to_wait(&wait_que, &my_wait, state);
可以根据自己的需要,修改state;比如独占等待标志

schedule();
//或者是同族函数;例如schedule_timeout(ret);

finish_wait(&wait_que, &my_wait);


方式二:
DECLARE_WAITQUEUE(my_wait, tsk)

__add_wait_queue(&wait_que, &my_wait);
set_current_state(state);
raw_spin_unlock_irq(&lock);
schedule();
raw_spin_lock_irq(&lock);
set_current_state(state);
__remove_wait_queue(&wait_que, &my_wait);

其实其他的休眠方式的实现都与方式二类似;
只是给用户提供了更方便的接口;
提供更底层的接口是以防提供的方法不能满足用户需求。

4.唤醒
wake_up(wait_que)
wake_up_interruptible(wait_que)
wake_up_nr(wait_que, nr)
wake_up_interruptible_nr(wait_que, nr)
wake_up_all(wait_que)
wake_up_interruptible_all(wait_que)
wake_up_interruptible_sync(wait_que)

 

等待队列_睡眠和唤醒