Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 9 Jul 2001 08:45:49 -0400 (EDT)
From:      Daniel Eischen <eischen@vigrid.com>
To:        Jason Evans <jasone@canonware.com>
Cc:        Julian Elischer <julian@elischer.org>, arch@FreeBSD.ORG
Subject:   Re: help needed in threads.. (Signals)
Message-ID:  <Pine.SUN.3.91.1010709082118.15060A-100000@pcnet1.pcnet.com>
In-Reply-To: <20010708232131.O8775@canonware.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On 8 Jul 2001, Jason Evans wrote:
> On Sun, Jul 08, 2001 at 10:51:19PM -0700, Julian Elischer wrote:
> > Can someone tell me if an alarm() call that sets an alarm signal for
> > some time in the futire (in a threaded environment, has teh signal delivered
> > to the process (an random thread), the same thread that made the request,
> > or a predesignated thread.. I just can't work out
> > what the right way to handle it in the code is..
> > 
> > posix?
> 
> Bwahaha, signals and threads.
> 
> Each thread has its own signal mask.  A signal sent to the process is
> delivered to a thread that doesn't have the signal masked (if any).  If
> more than one thread has the signal unmasked, which thread receives the
> signal is undefined.
> 
> That's what happens (or is supposed to happen).  If you want to know the
> right way to do signal handling in threaded programs, that requires more
> explanation.
> 
> Bwahaha.

:-)

Here is part of a log from a commit to uthread.c:

  Modify the search for a thread to which a signal is delivered.
  The search algorithm is now:

    o First thread found in sigwait() with signal in wait mask.
    o First thread found sigsuspend()'d on the signal.
    o Current thread if signal is unmasked.
    o First thread found with signal unmasked.

Note that the signal handler is not called if the thread is
in sigwait, but only called for the last 3 bullets.  If a thread
isn't found that can handle the signal, then the signal gets
marked as pending for the process, and will be delivered to the
first thread to sigwait/sigsuspend on it, or to unmask the
signal.

For pthread_kill(), the signal is directed to a specific thread.
If the signal is masked in the thread, then it is marked as pending
for that thread and will be delivered when the thread sigwaits/sigsuspends
on it or unmasks it.  If the process action is SIG_DFL for the signal, 
then the signal also gets delivered to the process.  This surprises a
few people when their program aborts ;-)

You can use both sigprocmask and pthread_sigmask to set the signal
mask of the current thread; they both behave the same way in a
threaded environment.

-- 
Dan Eischen

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-arch" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.SUN.3.91.1010709082118.15060A-100000>