About the use of wait_event_interruptible () and wake_up ()

Source: Internet
Author: User

Source:http://blog.csdn.net/allen6268198/article/details/8112551

1. Use of wait_event_interruptible () and wake_up ()

Read the source code of Wait_event_interruptible (), it is not difficult to find this function first
The status of the current process is set to task_interruptible, then schedule () is called,
The schedule () will take the current process in the task_interruptible state from Runqueue
deleted in the queue. The result of the removal from the Runqueue queue is that the current process will no longer participate in the
and scheduling, unless the process is re-placed in the Runqueue queue through other functions,
This is the role of WAKE_UP ().

Since this section of code is in a for (;;) controlled by condition Loop, so when the
Shedule () returns (after being wake_up, of course, through the schedule () of other processes
Again), if the condition condition not satisfied, the process will automatically be set again
Task_interruptible state, the result of the next execution of schedule () is
Removed from the Runqueue queue. You need to re-add it to the wake_up again by
The Runqueue queue.

So again and again, until condition is really wake_up.

Visible, the successful awakening of a process that is wait_event_interruptible () needs to be met:

In 1) condition is true, 2) calls Wake_up ().

So, if you just modify condition, then just meet one of the conditions, this time,
The process that was wait_event_interruptible () is not already in the Runqueue queue because
This will not be schedule. This time, as long as the WAKE_UP will immediately re-enter the running schedule.

2. About the return value of wait_event_interruptible

According to Wait_event_interruptible's macro definition, you know:

1) When the condition condition is true, calling this function will return 0 directly, and the current process will not
is wait_event_interruptible and removed from the Runqueue queue.

2) If the current process to be wait_event_interruptible is nonblocked pending
Signals, then the-erestartsys (i.e.-512) will be returned directly, and the current process will not
is wait_event_interruptible and removed from the Runqueue queue.

3) In other cases, the current process is normally wait_event_interruptible and
Runqueue queue removed, enter task_interruptible state exit run schedule,
Until it is woken up again to join the Runqueue queue and participates in scheduling, it returns 0 normally.

Attached 1:WAIT_EVENT_INTERRUPTIBLE macro

#define WAIT_EVENT_INTERRUPTIBLE (Wq, condition) \
({                                                 \
int __ret = 0; \
if (! ( condition)) \
__wait_event_interruptible (Wq, condition, __ret); \
__ret; \
})

Note: The value of {A, B, ..., x} in the C language is equal to the last item, that is, X, so the above
The value of the macro is __ret.


Equivalent code with 2:wait_event_interruptible () and WAKE_UP

Wait_event_interruptible (Wq, condition)/* Equivalent does not consider the return value */
{
if (! ( condition))
{
wait_queue_t _ _wait;
Init_waitqueue_entry (&_ _wait, current);
Add_wait_queue (&WQ, &_ _wait);
for (;;)
{
Set_current_state (task_interruptible);
if (condition)
Break
Schedule (); /* implicit call:del_from_runqueue (current) */
}
Current->state = task_running;
Remove_wait_queue (&WQ, &_ _wait);
}
}


void Wake_up (wait_queue_head_t *q)
{
struct List_head *tmp;
wait_queue_t *curr;
List_for_each (TMP, &q->task_list)
{
Curr = List_entry (tmp, wait_queue_t, task_list);
Wake_up_process (Curr->task);
/* Implicit call:add_to_runqueue (curr->task); * *
if (curr->flags)
Break
}
}

About the use of wait_event_interruptible () and wake_up ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.