하나 :
int ret = 0; // Result of waiting; in form 0/-err.
// Define wait objects, one object per waitqueue.
DEFINE_WAIT_FUNC(wait1, default_wake_function);
DEFINE_WAIT_FUNC(wait2, default_wake_function);
// Add ourselves to all waitqueues.
add_wait_queue(wq1, &wait1);
add_wait_queue(wq2, &wait2);
// Waiting cycle
while(1) {
// Change task state for waiting.
// NOTE: this should come **before** condition checking for avoid races.
set_current_state(TASK_INTERRUPTIBLE);
// Check condition(s) which we are waiting
if(cond) break;
// Need to wait
schedule();
// Check if waiting has been interrupted by signal
if (signal_pending(current)) {
ret = -ERESTARTSYS;
break;
}
}
// Remove ourselves from all waitqueues.
remove_wait_queue(wq1, &wait1);
remove_wait_queue(wq2, &wait2);
// Restore task state
__set_current_state(TASK_RUNNING);
// 'ret' contains result of waiting.
주,이 시나리오는 wait_event
중 하나에서 약간 다른 것을 :
wait_event
이 (DEFINE_WAIT
로 만든) 대기 개체에 대한 autoremove_wake_function
사용합니다. 이 함수는 wake_up()
에서 호출하여 대기열에서 대기중인 객체를 제거합니다. 따라서 에 다시 입력하여 대기열에있는 객체 각 반복.
그러나 대기 대기열이 여러 개있는 경우 알 수없는 대기 대기열이 있습니다. 따라서이 전략을 따르면 은 모든 반복 객체가 인 모든 대기 객체을 다시 추가해야하므로 비효율적입니다.
대신에 wait 객체에 default_wake_function
을 사용하므로 wake_up()
호출에서 객체가 waitqueue에서 제거되지 않으며 대기 전에 한 번만 대기 객체를 대기열에 추가하면 충분합니다.