diff options
author | davidxu <davidxu@FreeBSD.org> | 2010-09-01 02:18:33 +0000 |
---|---|---|
committer | davidxu <davidxu@FreeBSD.org> | 2010-09-01 02:18:33 +0000 |
commit | 4dcb50723aa0d3f4264ee1d9ca3baf70b4adc2de (patch) | |
tree | e6fc679c894d98bf195c89ddd6197862a61c0297 /lib/libthr/thread/thr_exit.c | |
parent | be8bfd2384601163d68720d6e4a562afc9e7303d (diff) | |
download | FreeBSD-src-4dcb50723aa0d3f4264ee1d9ca3baf70b4adc2de.zip FreeBSD-src-4dcb50723aa0d3f4264ee1d9ca3baf70b4adc2de.tar.gz |
Add signal handler wrapper, the reason to add it becauses there are
some cases we want to improve:
1) if a thread signal got a signal while in cancellation point,
it is possible the TDP_WAKEUP may be eaten by signal handler
if the handler called some interruptibly system calls.
2) In signal handler, we want to disable cancellation.
3) When thread holding some low level locks, it is better to
disable signal, those code need not to worry reentrancy,
sigprocmask system call is avoided because it is a bit expensive.
The signal handler wrapper works in this way:
1) libthr installs its signal handler if user code invokes sigaction
to install its handler, the user handler is recorded in internal
array.
2) when a signal is delivered, libthr's signal handler is invoke,
libthr checks if thread holds some low level lock or is in critical
region, if it is true, the signal is buffered, and all signals are
masked, once the thread leaves critical region, correct signal
mask is restored and buffered signal is processed.
3) before user signal handler is invoked, cancellation is temporarily
disabled, after user signal handler is returned, cancellation state
is restored, and pending cancellation is rescheduled.
Diffstat (limited to 'lib/libthr/thread/thr_exit.c')
-rw-r--r-- | lib/libthr/thread/thr_exit.c | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/lib/libthr/thread/thr_exit.c b/lib/libthr/thread/thr_exit.c index e6facd9..10581ab 100644 --- a/lib/libthr/thread/thr_exit.c +++ b/lib/libthr/thread/thr_exit.c @@ -34,6 +34,8 @@ #include <stdio.h> #include <stdlib.h> #include <pthread.h> +#include <sys/types.h> +#include <sys/signalvar.h> #include "un-namespace.h" #include "libc_private.h" @@ -58,6 +60,12 @@ _thread_exit(const char *fname, int lineno, const char *msg) void _pthread_exit(void *status) { + _pthread_exit_mask(status, NULL); +} + +void +_pthread_exit_mask(void *status, sigset_t *mask) +{ struct pthread *curthread = _get_curthread(); /* Check if this thread is already in the process of exiting: */ @@ -73,6 +81,17 @@ _pthread_exit(void *status) curthread->cancelling = 1; curthread->cancel_enable = 0; curthread->cancel_async = 0; + curthread->cancel_point = 0; + if (mask != NULL) + __sys_sigprocmask(SIG_SETMASK, mask, NULL); + if (curthread->unblock_sigcancel) { + sigset_t set; + + curthread->unblock_sigcancel = 0; + SIGEMPTYSET(set); + SIGADDSET(set, SIGCANCEL); + __sys_sigprocmask(SIG_UNBLOCK, mask, NULL); + } /* Save the return value: */ curthread->ret = status; |