diff options
author | mtm <mtm@FreeBSD.org> | 2004-05-20 12:06:16 +0000 |
---|---|---|
committer | mtm <mtm@FreeBSD.org> | 2004-05-20 12:06:16 +0000 |
commit | d835c0621f34028252fad957e6061920d493021d (patch) | |
tree | 191973c93de49160bb4144ea98ee350f5906d883 /lib/libthr/thread/thr_exit.c | |
parent | 2b949599bf280f4ce2bc5250ba241e9ee64bed2b (diff) | |
download | FreeBSD-src-d835c0621f34028252fad957e6061920d493021d.zip FreeBSD-src-d835c0621f34028252fad957e6061920d493021d.tar.gz |
Make libthr async-signal-safe without costly signal masking. The guidlines I
followed are: Only 3 functions (pthread_cancel, pthread_setcancelstate,
pthread_setcanceltype) are required to be async-signal-safe by POSIX. None of
the rest of the pthread api is required to be async-signal-safe. This means
that only the three mentioned functions are safe to use from inside
signal handlers.
However, there are certain system/libc calls that are
cancellation points that a caller may call from within a signal handler,
and since they are cancellation points calls have to be made into libthr
to test for cancellation and exit the thread if necessary. So, the
cancellation test and thread exit code paths must be async-signal-safe
as well. A summary of the changes follows:
o Almost all of the code paths that masked signals, as well as locking the
pthread structure now lock only the pthread structure.
o Signals are masked (and left that way) as soon as a thread enters
pthread_exit().
o The active and dead threads locks now explicitly require that signals
are masked.
o Access to the isdead field of the pthread structure is protected by both
the active and dead list locks for writing. Either one is sufficient for
reading.
o The thread state and type fields have been combined into one three-state
switch to make it easier to read without requiring a lock. It doesn't need
a lock for writing (and therefore for reading either) because only the
current thread can write to it and it is an integer value.
o The thread state field of the pthread structure has been eliminated. It
was an unnecessary field that mostly duplicated the flags field, but
required additional locking that would make a lot more code paths require
signal masking. Any truly unique values (such as PS_DEAD) have been
reborn as separate members of the pthread structure.
o Since the mutex and condvar pthread functions are not async-signal-safe
there is no need to muck about with the wait queues when handling
a signal ...
o ... which also removes the need for wrapping signal handlers and sigaction(2).
o The condvar and mutex async-cancellation code had to be revised as a result
of some of these changes, which resulted in semi-unrelated changes which
would have been difficult to work on as a separate commit, so they are
included as well.
The only part of the changes I am worried about is related to locking for
the pthread joining fields. But, I will take a closer look at them once this
mega-patch is committed.
Diffstat (limited to 'lib/libthr/thread/thr_exit.c')
-rw-r--r-- | lib/libthr/thread/thr_exit.c | 41 |
1 files changed, 12 insertions, 29 deletions
diff --git a/lib/libthr/thread/thr_exit.c b/lib/libthr/thread/thr_exit.c index 0455a97..f71c59a 100644 --- a/lib/libthr/thread/thr_exit.c +++ b/lib/libthr/thread/thr_exit.c @@ -96,18 +96,23 @@ _thread_exit_cleanup(void) void _pthread_exit(void *status) { - pthread_t pthread, joiner; + struct pthread *pthread; int exitNow = 0; + /* + * This thread will no longer handle any signals. + */ + _thread_sigblock(); + /* Check if this thread is already in the process of exiting: */ - if ((curthread->flags & PTHREAD_EXITING) != 0) { + if (curthread->exiting) { char msg[128]; snprintf(msg, sizeof(msg), "Thread %p has called pthread_exit() from a destructor. POSIX 1003.1 1996 s16.2.5.2 does not allow this!",curthread); PANIC(msg); } /* Flag this thread as exiting: */ - curthread->flags |= PTHREAD_EXITING; + curthread->exiting = 1; /* Save the return value: */ curthread->ret = status; @@ -130,45 +135,24 @@ _pthread_exit(void *status) */ if (curthread->rwlockList != NULL) free(curthread->rwlockList); -retry: - /* - * Proper lock order, to minimize deadlocks, between joining - * and exiting threads is: DEAD_LIST, THREAD_LIST, exiting, joiner. - * In order to do this *and* protect from races, we must resort - * this test-and-retry loop. - */ - joiner = curthread->joiner; /* Lock the dead list first to maintain correct lock order */ DEAD_LIST_LOCK; THREAD_LIST_LOCK; - _thread_critical_enter(curthread); - - if (joiner != curthread->joiner) { - _thread_critical_exit(curthread); - THREAD_LIST_UNLOCK; - DEAD_LIST_UNLOCK; - goto retry; - } /* Check if there is a thread joining this one: */ if (curthread->joiner != NULL) { pthread = curthread->joiner; - UMTX_LOCK(&pthread->lock); curthread->joiner = NULL; - /* Make the joining thread runnable: */ - PTHREAD_NEW_STATE(pthread, PS_RUNNING); - /* Set the return value for the joining thread: */ pthread->join_status.ret = curthread->ret; pthread->join_status.error = 0; pthread->join_status.thread = NULL; - UMTX_UNLOCK(&pthread->lock); - /* Make this thread collectable by the garbage collector. */ - PTHREAD_ASSERT(((curthread->attr.flags & PTHREAD_DETACHED) == - 0), "Cannot join a detached thread"); + /* Make the joining thread runnable: */ + PTHREAD_WAKE(pthread); + curthread->attr.flags |= PTHREAD_DETACHED; } @@ -180,8 +164,7 @@ retry: deadlist_free_threads(); TAILQ_INSERT_HEAD(&_dead_list, curthread, dle); TAILQ_REMOVE(&_thread_list, curthread, tle); - PTHREAD_SET_STATE(curthread, PS_DEAD); - _thread_critical_exit(curthread); + curthread->isdead = 1; /* If we're the last thread, call it quits */ if (TAILQ_EMPTY(&_thread_list)) |