I'm reading "Linux Kernel Development" by Robert Love and found the code below to wait for an event.
DEFINE_WAIT(wait);
add_wait_queue(q, &wait);
while (!condition) {
// What happens if condition is changed and wake_up() is called here ?
prepare_to_wait(&q, &wait, TASK_INTERRUPTIBLE);
if (signal_pending(current))
/* handle signal */
schedule();
}
finish_wait(&q, &wait);
My question is as in the code above. What happens if condition is changed and wake_up() is called after condition check but before prepare_to_wait?
My (probably wrong) interpretation here is that because prepare_to_wait makes the thread TASK_INTERRUPTIBLE and calls schedule() after the condition is changed, it sleeps forever (unless it gets signaled or another wake_up is called).