Search code examples
linuxlinux-kernelembeddedlinux-device-driver

wait_event_interruptible and signal_pending


I've been reading LDD3 book and I've stumbled upon the following piece of code:

int somefunc()
{
   [...]

    wait_event_interruptible(data->wait, !data->loops);
    if (signal_pending(current))
        return -ERESTARTSYS;
}

However, in some other parts of this book, the authors used a little bit different approach to (at least for me) do the same thing.

int someOtherFunc()
{
   [...]

    if (wait_event_interruptible(data->wait, !data->loops)) {
      return -ERESTARTSYS;
   }
}

Is there any difference between these two? Or perhaps it's just a matter of style?

From what I understand the non-negative return code from wait_event_interruptible indicates that the call was interrupted by some source of signal, so it kinda looks the same as what the call to signal_pending checks. Am I right?


Solution

  • Both code snippets wait on a waitqueue and return -ERESTARTSYS if the wait gets interrupted by a signal. The call to signal_pending() after wait_event_interruptible() could be avoided, because the latter already returns -ERESTARTSYS in case of interruption by signal (see the docstring comment on its definition). The second code snippet seems like a cleaner way to implement the logic.

    You should check the rest of the code to see if there is an outstanding reason to call signal_pending() explicitly. The first function may want to return early regardless of the result of wait_event_interruptible() if there is a signal to be delivered.