Using sigprocmask to implement locks

Posted by EpsilonVector on Stack Overflow See other posts from Stack Overflow or by EpsilonVector
Published on 2010-05-18T00:48:52Z Indexed on 2010/05/18 1:00 UTC
Read the original article Hit count: 248

Filed under:
|
|
|

I'm implementing user threads in Linux kernel 2.4, and I'm using ualarm to invoke context switches between the threads.

We have a requirement that our thread library's functions should be uninterruptable, so I looked into blocking signals and learned that using sigprocmask is the standard way to do this.

However, it looks like I need to do quite a lot to implement this:

sigset_t new_set, old_set;

sigemptyset(&new_set);
sigaddset(&new_set, SIGALRM);
sigprocmask(SIG_BLOCK, &new_set, &old_set);

This blocks SIGALARM but it does this with 3 function invocations! A lot can happen in the time it takes for these functions to run, including the signal being sent. The best idea I had to mitigate this was temporarily disabling ualarm, like this:

sigset_t new_set, old_set;

time=ualarm(0,0);
sigemptyset(&new_set);
sigaddset(&new_set, SIGALRM);
sigprocmask(SIG_BLOCK, &new_set, &old_set);
ualarm(time, 0);

Which is fine except that this feels verbose. Isn't there a better way to do this?

© Stack Overflow or respective owner

Related posts about linux-kernel

Related posts about threads