pthread_cancel, sigwait, and signal blocking

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

pthread_cancel, sigwait, and signal blocking

I have studied the man pages, asked on irc and been told the standard
isn't obvious on my question, so hopefully it qualifies to be posted
here as a documentation issue at least.

I have a signal handling thread which sigwait()s to process signals
synchronously, as suggested in O'Reilly Pthread Programming chapter 5.
At a certain point, my program needs to return to single threaded
operation and restore normal signal handling.  The trouble is, I'm not
sure how to safely cancel the signal handling thread.  It needs to be
cancelled, since its in sigwait, but I don't want to lose a signal.
What I would like to do is:

   Depend on atomicity of cancellation and signal delivery with
   respect to each other, and assume that if cancellation is deferred
   around sigwait and PTHREAD_CANCEL_DISABLE'ed elsewhere in the
   synchronous signal handling thread, then either

      a.  The cancel will take effect first during the cancellation
          point sigwait, and the signal will be handled normally after
          it is unblocked in the main thread, or

      b.  The signal will be 'caught' by sigwait, after which deferred
          cancellation will be considered impossible until sigwait has
          returned, after which cancellation is immediately disabled,
          gauranteeing that the signal will be processed before
          sigwait is entered again, at which point the cancel will
          happen (assuming no new signal comes in).

Is this safe?  If not, what should I do?  The signal handling thread
could be shut down with a signal, but usually its bad to have to
control threads with signals.

Britton Kerin