2003-04-01 03:46:29 +00:00
|
|
|
/*
|
2005-12-17 09:42:45 +00:00
|
|
|
* Copyright (c) 2005, David Xu <davidxu@freebsd.org>
|
2003-04-01 03:46:29 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
2005-12-17 09:42:45 +00:00
|
|
|
* notice unmodified, this list of conditions, and the following
|
|
|
|
* disclaimer.
|
2003-04-01 03:46:29 +00:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
2005-12-17 09:42:45 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2003-04-01 03:46:29 +00:00
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
2006-04-04 02:57:49 +00:00
|
|
|
#include "namespace.h"
|
2003-04-01 03:46:29 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/signalvar.h>
|
2015-08-10 17:02:42 +00:00
|
|
|
#include <sys/syscall.h>
|
2003-04-01 03:46:29 +00:00
|
|
|
#include <signal.h>
|
2005-04-02 01:20:00 +00:00
|
|
|
#include <errno.h>
|
2012-01-21 18:06:18 +00:00
|
|
|
#include <stdlib.h>
|
2005-04-02 01:20:00 +00:00
|
|
|
#include <string.h>
|
2003-04-01 03:46:29 +00:00
|
|
|
#include <pthread.h>
|
2006-04-04 02:57:49 +00:00
|
|
|
#include "un-namespace.h"
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
#include "libc_private.h"
|
2003-12-09 11:12:11 +00:00
|
|
|
|
2015-01-03 18:38:46 +00:00
|
|
|
#include "libc_private.h"
|
2003-04-01 03:46:29 +00:00
|
|
|
#include "thr_private.h"
|
|
|
|
|
|
|
|
/* #define DEBUG_SIGNAL */
|
|
|
|
#ifdef DEBUG_SIGNAL
|
|
|
|
#define DBG_MSG stdout_debug
|
|
|
|
#else
|
|
|
|
#define DBG_MSG(x...)
|
|
|
|
#endif
|
|
|
|
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
struct usigaction {
|
|
|
|
struct sigaction sigact;
|
|
|
|
struct urwlock lock;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct usigaction _thr_sigact[_SIG_MAXSIG];
|
|
|
|
|
2015-01-03 18:38:46 +00:00
|
|
|
static inline struct usigaction *
|
|
|
|
__libc_sigaction_slot(int signo)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (&_thr_sigact[signo - 1]);
|
|
|
|
}
|
|
|
|
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
static void thr_sighandler(int, siginfo_t *, void *);
|
|
|
|
static void handle_signal(struct sigaction *, int, siginfo_t *, ucontext_t *);
|
|
|
|
static void check_deferred_signal(struct pthread *);
|
|
|
|
static void check_suspend(struct pthread *);
|
|
|
|
static void check_cancel(struct pthread *curthread, ucontext_t *ucp);
|
|
|
|
|
2007-11-21 05:23:54 +00:00
|
|
|
int _sigtimedwait(const sigset_t *set, siginfo_t *info,
|
|
|
|
const struct timespec * timeout);
|
|
|
|
int _sigwaitinfo(const sigset_t *set, siginfo_t *info);
|
|
|
|
int _sigwait(const sigset_t *set, int *sig);
|
2010-08-24 09:57:06 +00:00
|
|
|
int _setcontext(const ucontext_t *);
|
|
|
|
int _swapcontext(ucontext_t *, const ucontext_t *);
|
2006-04-04 02:57:49 +00:00
|
|
|
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
static const sigset_t _thr_deferset={{
|
|
|
|
0xffffffff & ~(_SIG_BIT(SIGBUS)|_SIG_BIT(SIGILL)|_SIG_BIT(SIGFPE)|
|
|
|
|
_SIG_BIT(SIGSEGV)|_SIG_BIT(SIGTRAP)|_SIG_BIT(SIGSYS)),
|
|
|
|
0xffffffff,
|
|
|
|
0xffffffff,
|
|
|
|
0xffffffff}};
|
|
|
|
|
|
|
|
static const sigset_t _thr_maskset={{
|
|
|
|
0xffffffff,
|
|
|
|
0xffffffff,
|
|
|
|
0xffffffff,
|
|
|
|
0xffffffff}};
|
|
|
|
|
|
|
|
void
|
|
|
|
_thr_signal_block(struct pthread *curthread)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (curthread->sigblock > 0) {
|
|
|
|
curthread->sigblock++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
__sys_sigprocmask(SIG_BLOCK, &_thr_maskset, &curthread->sigmask);
|
|
|
|
curthread->sigblock++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_thr_signal_unblock(struct pthread *curthread)
|
|
|
|
{
|
|
|
|
if (--curthread->sigblock == 0)
|
|
|
|
__sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
_thr_send_sig(struct pthread *thread, int sig)
|
|
|
|
{
|
|
|
|
return thr_kill(thread->tid, sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2010-08-24 09:57:06 +00:00
|
|
|
remove_thr_signals(sigset_t *set)
|
|
|
|
{
|
|
|
|
if (SIGISMEMBER(*set, SIGCANCEL))
|
|
|
|
SIGDELSET(*set, SIGCANCEL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const sigset_t *
|
|
|
|
thr_remove_thr_signals(const sigset_t *set, sigset_t *newset)
|
|
|
|
{
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
*newset = *set;
|
|
|
|
remove_thr_signals(newset);
|
|
|
|
return (newset);
|
2010-08-24 09:57:06 +00:00
|
|
|
}
|
2007-11-21 05:21:58 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
static void
|
2006-04-04 02:57:49 +00:00
|
|
|
sigcancel_handler(int sig __unused,
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
siginfo_t *info __unused, ucontext_t *ucp)
|
2005-04-02 01:20:00 +00:00
|
|
|
{
|
|
|
|
struct pthread *curthread = _get_curthread();
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
int err;
|
2005-04-02 01:20:00 +00:00
|
|
|
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
if (THR_IN_CRITICAL(curthread))
|
|
|
|
return;
|
|
|
|
err = errno;
|
|
|
|
check_suspend(curthread);
|
|
|
|
check_cancel(curthread, ucp);
|
|
|
|
errno = err;
|
|
|
|
}
|
|
|
|
|
2015-01-03 18:38:46 +00:00
|
|
|
typedef void (*ohandler)(int sig, int code, struct sigcontext *scp,
|
|
|
|
char *addr, __sighandler_t *catcher);
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The signal handler wrapper is entered with all signal masked.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
thr_sighandler(int sig, siginfo_t *info, void *_ucp)
|
|
|
|
{
|
2015-01-03 18:38:46 +00:00
|
|
|
struct pthread *curthread;
|
|
|
|
ucontext_t *ucp;
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
struct sigaction act;
|
2015-01-03 18:38:46 +00:00
|
|
|
struct usigaction *usa;
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
err = errno;
|
2015-01-03 18:38:46 +00:00
|
|
|
curthread = _get_curthread();
|
|
|
|
ucp = _ucp;
|
|
|
|
usa = __libc_sigaction_slot(sig);
|
|
|
|
_thr_rwl_rdlock(&usa->lock);
|
|
|
|
act = usa->sigact;
|
|
|
|
_thr_rwl_unlock(&usa->lock);
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
errno = err;
|
2013-11-23 15:48:17 +00:00
|
|
|
curthread->deferred_run = 0;
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* if a thread is in critical region, for example it holds low level locks,
|
|
|
|
* try to defer the signal processing, however if the signal is synchronous
|
|
|
|
* signal, it means a bad thing has happened, this is a programming error,
|
|
|
|
* resuming fault point can not help anything (normally causes deadloop),
|
|
|
|
* so here we let user code handle it immediately.
|
|
|
|
*/
|
|
|
|
if (THR_IN_CRITICAL(curthread) && SIGISMEMBER(_thr_deferset, sig)) {
|
|
|
|
memcpy(&curthread->deferred_sigact, &act, sizeof(struct sigaction));
|
|
|
|
memcpy(&curthread->deferred_siginfo, info, sizeof(siginfo_t));
|
|
|
|
curthread->deferred_sigmask = ucp->uc_sigmask;
|
|
|
|
/* mask all signals, we will restore it later. */
|
|
|
|
ucp->uc_sigmask = _thr_deferset;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle_signal(&act, sig, info, ucp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_signal(struct sigaction *actp, int sig, siginfo_t *info, ucontext_t *ucp)
|
|
|
|
{
|
|
|
|
struct pthread *curthread = _get_curthread();
|
|
|
|
ucontext_t uc2;
|
|
|
|
__siginfohandler_t *sigfunc;
|
|
|
|
int cancel_point;
|
|
|
|
int cancel_async;
|
|
|
|
int cancel_enable;
|
|
|
|
int in_sigsuspend;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/* add previous level mask */
|
|
|
|
SIGSETOR(actp->sa_mask, ucp->uc_sigmask);
|
|
|
|
|
|
|
|
/* add this signal's mask */
|
|
|
|
if (!(actp->sa_flags & SA_NODEFER))
|
|
|
|
SIGADDSET(actp->sa_mask, sig);
|
|
|
|
|
|
|
|
in_sigsuspend = curthread->in_sigsuspend;
|
|
|
|
curthread->in_sigsuspend = 0;
|
|
|
|
|
|
|
|
/*
|
2013-05-27 18:45:45 +00:00
|
|
|
* If thread is in deferred cancellation mode, disable cancellation
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
* in signal handler.
|
2013-05-27 18:45:45 +00:00
|
|
|
* If user signal handler calls a cancellation point function, e.g,
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
* it calls write() to write data to file, because write() is a
|
|
|
|
* cancellation point, the thread is immediately cancelled if
|
|
|
|
* cancellation is pending, to avoid this problem while thread is in
|
|
|
|
* deferring mode, cancellation is temporarily disabled.
|
|
|
|
*/
|
|
|
|
cancel_point = curthread->cancel_point;
|
|
|
|
cancel_async = curthread->cancel_async;
|
|
|
|
cancel_enable = curthread->cancel_enable;
|
|
|
|
curthread->cancel_point = 0;
|
|
|
|
if (!cancel_async)
|
|
|
|
curthread->cancel_enable = 0;
|
|
|
|
|
|
|
|
/* restore correct mask before calling user handler */
|
|
|
|
__sys_sigprocmask(SIG_SETMASK, &actp->sa_mask, NULL);
|
|
|
|
|
|
|
|
sigfunc = actp->sa_sigaction;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We have already reset cancellation point flags, so if user's code
|
|
|
|
* longjmp()s out of its signal handler, wish its jmpbuf was set
|
|
|
|
* outside of a cancellation point, in most cases, this would be
|
2013-05-27 18:45:45 +00:00
|
|
|
* true. However, there is no way to save cancel_enable in jmpbuf,
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
* so after setjmps() returns once more, the user code may need to
|
|
|
|
* re-set cancel_enable flag by calling pthread_setcancelstate().
|
|
|
|
*/
|
2015-01-03 18:38:46 +00:00
|
|
|
if ((actp->sa_flags & SA_SIGINFO) != 0) {
|
|
|
|
sigfunc(sig, info, ucp);
|
|
|
|
} else {
|
|
|
|
((ohandler)sigfunc)(sig, info->si_code,
|
|
|
|
(struct sigcontext *)ucp, info->si_addr,
|
|
|
|
(__sighandler_t *)sigfunc);
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
}
|
|
|
|
err = errno;
|
|
|
|
|
|
|
|
curthread->in_sigsuspend = in_sigsuspend;
|
|
|
|
curthread->cancel_point = cancel_point;
|
|
|
|
curthread->cancel_enable = cancel_enable;
|
|
|
|
|
|
|
|
memcpy(&uc2, ucp, sizeof(uc2));
|
|
|
|
SIGDELSET(uc2.uc_sigmask, SIGCANCEL);
|
|
|
|
|
|
|
|
/* reschedule cancellation */
|
|
|
|
check_cancel(curthread, &uc2);
|
|
|
|
errno = err;
|
2015-08-10 17:02:42 +00:00
|
|
|
syscall(SYS_sigreturn, &uc2);
|
2006-01-05 13:51:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_thr_ast(struct pthread *curthread)
|
|
|
|
{
|
In current implementation, thread cancellation is done in signal handler,
which does not know what is the state of interrupted system call, for
example, open() system call opened a file and the thread is still cancelled,
result is descriptor leak, there are other problems which can cause resource
leak or undeterminable side effect when a thread is cancelled. However, this
is no longer true in new implementation.
In defering mode, a thread is canceled if cancellation request is pending and
later the thread enters a cancellation point, otherwise, a later
pthread_cancel() just causes SIGCANCEL to be sent to the target thread, and
causes target thread to abort system call, userland code in libthr then checks
cancellation state, and cancels the thread if needed. For example, the
cancellation point open(), the thread may be canceled at start,
but later, if it opened a file descriptor, it is not canceled, this avoids
file handle leak. Another example is read(), a thread may be canceled at start
of the function, but later, if it read some bytes from a socket, the thread
is not canceled, the caller then can decide if it should still enable cancelling
or disable it and continue reading data until it thinks it has read all
bytes of a packet, and keeps a protocol stream in health state, if user ignores
partly reading of a packet without disabling cancellation, then second iteration
of read loop cause the thread to be cancelled.
An exception is that the close() cancellation point always closes a file handle
despite whether the thread is cancelled or not.
The old mechanism is still kept, for a functions which is not so easily to
fix a cancellation problem, the rough mechanism is used.
Reviewed by: kib@
2010-08-20 05:15:39 +00:00
|
|
|
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
if (!THR_IN_CRITICAL(curthread)) {
|
|
|
|
check_deferred_signal(curthread);
|
|
|
|
check_suspend(curthread);
|
|
|
|
check_cancel(curthread, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reschedule cancellation */
|
|
|
|
static void
|
|
|
|
check_cancel(struct pthread *curthread, ucontext_t *ucp)
|
|
|
|
{
|
|
|
|
|
2010-09-21 06:47:04 +00:00
|
|
|
if (__predict_true(!curthread->cancel_pending ||
|
|
|
|
!curthread->cancel_enable || curthread->no_cancel))
|
In current implementation, thread cancellation is done in signal handler,
which does not know what is the state of interrupted system call, for
example, open() system call opened a file and the thread is still cancelled,
result is descriptor leak, there are other problems which can cause resource
leak or undeterminable side effect when a thread is cancelled. However, this
is no longer true in new implementation.
In defering mode, a thread is canceled if cancellation request is pending and
later the thread enters a cancellation point, otherwise, a later
pthread_cancel() just causes SIGCANCEL to be sent to the target thread, and
causes target thread to abort system call, userland code in libthr then checks
cancellation state, and cancels the thread if needed. For example, the
cancellation point open(), the thread may be canceled at start,
but later, if it opened a file descriptor, it is not canceled, this avoids
file handle leak. Another example is read(), a thread may be canceled at start
of the function, but later, if it read some bytes from a socket, the thread
is not canceled, the caller then can decide if it should still enable cancelling
or disable it and continue reading data until it thinks it has read all
bytes of a packet, and keeps a protocol stream in health state, if user ignores
partly reading of a packet without disabling cancellation, then second iteration
of read loop cause the thread to be cancelled.
An exception is that the close() cancellation point always closes a file handle
despite whether the thread is cancelled or not.
The old mechanism is still kept, for a functions which is not so easily to
fix a cancellation problem, the rough mechanism is used.
Reviewed by: kib@
2010-08-20 05:15:39 +00:00
|
|
|
return;
|
|
|
|
|
2010-09-21 06:47:04 +00:00
|
|
|
/*
|
|
|
|
* Otherwise, we are in defer mode, and we are at
|
|
|
|
* cancel point, tell kernel to not block the current
|
|
|
|
* thread on next cancelable system call.
|
|
|
|
*
|
|
|
|
* There are three cases we should call thr_wake() to
|
|
|
|
* turn on TDP_WAKEUP or send SIGCANCEL in kernel:
|
|
|
|
* 1) we are going to call a cancelable system call,
|
|
|
|
* non-zero cancel_point means we are already in
|
|
|
|
* cancelable state, next system call is cancelable.
|
|
|
|
* 2) because _thr_ast() may be called by
|
|
|
|
* THR_CRITICAL_LEAVE() which is used by rtld rwlock
|
|
|
|
* and any libthr internal locks, when rtld rwlock
|
2015-06-14 19:19:46 +00:00
|
|
|
* is used, it is mostly caused by an unresolved PLT.
|
|
|
|
* Those routines may clear the TDP_WAKEUP flag by
|
2010-09-21 06:47:04 +00:00
|
|
|
* invoking some system calls, in those cases, we
|
|
|
|
* also should reenable the flag.
|
|
|
|
* 3) thread is in sigsuspend(), and the syscall insists
|
|
|
|
* on getting a signal before it agrees to return.
|
|
|
|
*/
|
|
|
|
if (curthread->cancel_point) {
|
|
|
|
if (curthread->in_sigsuspend && ucp) {
|
|
|
|
SIGADDSET(ucp->uc_sigmask, SIGCANCEL);
|
|
|
|
curthread->unblock_sigcancel = 1;
|
|
|
|
_thr_send_sig(curthread, SIGCANCEL);
|
|
|
|
} else
|
|
|
|
thr_wake(curthread->tid);
|
|
|
|
} else if (curthread->cancel_async) {
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
/*
|
2010-09-21 06:47:04 +00:00
|
|
|
* asynchronous cancellation mode, act upon
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
* immediately.
|
2010-09-21 06:47:04 +00:00
|
|
|
*/
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
_pthread_exit_mask(PTHREAD_CANCELED,
|
|
|
|
ucp? &ucp->uc_sigmask : NULL);
|
2005-04-02 01:20:00 +00:00
|
|
|
}
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
check_deferred_signal(struct pthread *curthread)
|
|
|
|
{
|
2012-01-21 18:06:18 +00:00
|
|
|
ucontext_t *uc;
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
struct sigaction act;
|
|
|
|
siginfo_t info;
|
2013-06-03 04:22:42 +00:00
|
|
|
int uc_len;
|
In current implementation, thread cancellation is done in signal handler,
which does not know what is the state of interrupted system call, for
example, open() system call opened a file and the thread is still cancelled,
result is descriptor leak, there are other problems which can cause resource
leak or undeterminable side effect when a thread is cancelled. However, this
is no longer true in new implementation.
In defering mode, a thread is canceled if cancellation request is pending and
later the thread enters a cancellation point, otherwise, a later
pthread_cancel() just causes SIGCANCEL to be sent to the target thread, and
causes target thread to abort system call, userland code in libthr then checks
cancellation state, and cancels the thread if needed. For example, the
cancellation point open(), the thread may be canceled at start,
but later, if it opened a file descriptor, it is not canceled, this avoids
file handle leak. Another example is read(), a thread may be canceled at start
of the function, but later, if it read some bytes from a socket, the thread
is not canceled, the caller then can decide if it should still enable cancelling
or disable it and continue reading data until it thinks it has read all
bytes of a packet, and keeps a protocol stream in health state, if user ignores
partly reading of a packet without disabling cancellation, then second iteration
of read loop cause the thread to be cancelled.
An exception is that the close() cancellation point always closes a file handle
despite whether the thread is cancelled or not.
The old mechanism is still kept, for a functions which is not so easily to
fix a cancellation problem, the rough mechanism is used.
Reviewed by: kib@
2010-08-20 05:15:39 +00:00
|
|
|
|
2013-11-23 15:48:17 +00:00
|
|
|
if (__predict_true(curthread->deferred_siginfo.si_signo == 0 ||
|
|
|
|
curthread->deferred_run))
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
return;
|
2012-01-21 18:06:18 +00:00
|
|
|
|
2013-11-23 15:48:17 +00:00
|
|
|
curthread->deferred_run = 1;
|
2013-05-28 04:54:16 +00:00
|
|
|
uc_len = __getcontextx_size();
|
|
|
|
uc = alloca(uc_len);
|
|
|
|
getcontext(uc);
|
2013-11-23 15:48:17 +00:00
|
|
|
if (curthread->deferred_siginfo.si_signo == 0) {
|
|
|
|
curthread->deferred_run = 0;
|
2013-05-28 04:54:16 +00:00
|
|
|
return;
|
2013-11-23 15:48:17 +00:00
|
|
|
}
|
2013-05-28 04:54:16 +00:00
|
|
|
__fillcontextx2((char *)uc);
|
2013-06-03 04:22:42 +00:00
|
|
|
act = curthread->deferred_sigact;
|
|
|
|
uc->uc_sigmask = curthread->deferred_sigmask;
|
|
|
|
memcpy(&info, &curthread->deferred_siginfo, sizeof(siginfo_t));
|
|
|
|
/* remove signal */
|
|
|
|
curthread->deferred_siginfo.si_signo = 0;
|
|
|
|
handle_signal(&act, info.si_signo, &info, uc);
|
2005-04-02 01:20:00 +00:00
|
|
|
}
|
|
|
|
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
static void
|
|
|
|
check_suspend(struct pthread *curthread)
|
2005-04-02 01:20:00 +00:00
|
|
|
{
|
2008-04-29 03:58:18 +00:00
|
|
|
uint32_t cycle;
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
|
|
|
|
if (__predict_true((curthread->flags &
|
|
|
|
(THR_FLAGS_NEED_SUSPEND | THR_FLAGS_SUSPENDED))
|
|
|
|
!= THR_FLAGS_NEED_SUSPEND))
|
|
|
|
return;
|
2012-08-27 03:09:39 +00:00
|
|
|
if (curthread == _single_thread)
|
|
|
|
return;
|
2008-03-18 02:06:51 +00:00
|
|
|
if (curthread->force_exit)
|
|
|
|
return;
|
|
|
|
|
2006-01-05 13:51:22 +00:00
|
|
|
/*
|
|
|
|
* Blocks SIGCANCEL which other threads must send.
|
|
|
|
*/
|
2005-04-02 01:20:00 +00:00
|
|
|
_thr_signal_block(curthread);
|
2006-01-05 13:51:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Increase critical_count, here we don't use THR_LOCK/UNLOCK
|
|
|
|
* because we are leaf code, we don't want to recursively call
|
|
|
|
* ourself.
|
|
|
|
*/
|
|
|
|
curthread->critical_count++;
|
2006-09-06 04:04:10 +00:00
|
|
|
THR_UMUTEX_LOCK(curthread, &(curthread)->lock);
|
2006-01-05 13:51:22 +00:00
|
|
|
while ((curthread->flags & (THR_FLAGS_NEED_SUSPEND |
|
|
|
|
THR_FLAGS_SUSPENDED)) == THR_FLAGS_NEED_SUSPEND) {
|
|
|
|
curthread->cycle++;
|
|
|
|
cycle = curthread->cycle;
|
|
|
|
|
|
|
|
/* Wake the thread suspending us. */
|
2008-04-29 03:58:18 +00:00
|
|
|
_thr_umtx_wake(&curthread->cycle, INT_MAX, 0);
|
2006-01-05 13:51:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* if we are from pthread_exit, we don't want to
|
|
|
|
* suspend, just go and die.
|
|
|
|
*/
|
|
|
|
if (curthread->state == PS_DEAD)
|
|
|
|
break;
|
2005-04-02 01:20:00 +00:00
|
|
|
curthread->flags |= THR_FLAGS_SUSPENDED;
|
2006-09-06 04:04:10 +00:00
|
|
|
THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock);
|
2008-04-29 03:58:18 +00:00
|
|
|
_thr_umtx_wait_uint(&curthread->cycle, cycle, NULL, 0);
|
2006-09-06 04:04:10 +00:00
|
|
|
THR_UMUTEX_LOCK(curthread, &(curthread)->lock);
|
2005-04-02 01:20:00 +00:00
|
|
|
curthread->flags &= ~THR_FLAGS_SUSPENDED;
|
|
|
|
}
|
2006-09-06 04:04:10 +00:00
|
|
|
THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock);
|
2006-01-05 13:51:22 +00:00
|
|
|
curthread->critical_count--;
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
_thr_signal_unblock(curthread);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-03 18:38:46 +00:00
|
|
|
_thr_signal_init(int dlopened)
|
2005-04-02 01:20:00 +00:00
|
|
|
{
|
2015-01-03 18:38:46 +00:00
|
|
|
struct sigaction act, nact, oact;
|
|
|
|
struct usigaction *usa;
|
|
|
|
sigset_t oldset;
|
|
|
|
int sig, error;
|
|
|
|
|
|
|
|
if (dlopened) {
|
|
|
|
__sys_sigprocmask(SIG_SETMASK, &_thr_maskset, &oldset);
|
|
|
|
for (sig = 1; sig <= _SIG_MAXSIG; sig++) {
|
|
|
|
if (sig == SIGCANCEL)
|
|
|
|
continue;
|
|
|
|
error = __sys_sigaction(sig, NULL, &oact);
|
|
|
|
if (error == -1 || oact.sa_handler == SIG_DFL ||
|
|
|
|
oact.sa_handler == SIG_IGN)
|
|
|
|
continue;
|
|
|
|
usa = __libc_sigaction_slot(sig);
|
|
|
|
usa->sigact = oact;
|
|
|
|
nact = oact;
|
|
|
|
remove_thr_signals(&usa->sigact.sa_mask);
|
|
|
|
nact.sa_flags &= ~SA_NODEFER;
|
|
|
|
nact.sa_flags |= SA_SIGINFO;
|
|
|
|
nact.sa_sigaction = thr_sighandler;
|
|
|
|
nact.sa_mask = _thr_maskset;
|
|
|
|
(void)__sys_sigaction(sig, &nact, NULL);
|
|
|
|
}
|
|
|
|
__sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
|
|
|
|
}
|
2005-04-02 01:20:00 +00:00
|
|
|
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
/* Install SIGCANCEL handler. */
|
|
|
|
SIGFILLSET(act.sa_mask);
|
|
|
|
act.sa_flags = SA_SIGINFO;
|
2005-04-02 01:20:00 +00:00
|
|
|
act.sa_sigaction = (__siginfohandler_t *)&sigcancel_handler;
|
|
|
|
__sys_sigaction(SIGCANCEL, &act, NULL);
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
|
|
|
|
/* Unblock SIGCANCEL */
|
|
|
|
SIGEMPTYSET(act.sa_mask);
|
|
|
|
SIGADDSET(act.sa_mask, SIGCANCEL);
|
|
|
|
__sys_sigprocmask(SIG_UNBLOCK, &act.sa_mask, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_thr_sigact_unload(struct dl_phdr_info *phdr_info)
|
|
|
|
{
|
2010-09-06 03:00:54 +00:00
|
|
|
#if 0
|
2010-09-01 13:22:55 +00:00
|
|
|
struct pthread *curthread = _get_curthread();
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
struct urwlock *rwlp;
|
|
|
|
struct sigaction *actp;
|
2015-01-03 18:38:46 +00:00
|
|
|
struct usigaction *usa;
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
struct sigaction kact;
|
|
|
|
void (*handler)(int);
|
|
|
|
int sig;
|
|
|
|
|
2010-09-01 13:22:55 +00:00
|
|
|
_thr_signal_block(curthread);
|
2010-09-06 03:00:54 +00:00
|
|
|
for (sig = 1; sig <= _SIG_MAXSIG; sig++) {
|
2015-01-03 18:38:46 +00:00
|
|
|
usa = __libc_sigaction_slot(sig);
|
|
|
|
actp = &usa->sigact;
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
retry:
|
|
|
|
handler = actp->sa_handler;
|
|
|
|
if (handler != SIG_DFL && handler != SIG_IGN &&
|
|
|
|
__elf_phdr_match_addr(phdr_info, handler)) {
|
2015-01-03 18:38:46 +00:00
|
|
|
rwlp = &usa->lock;
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
_thr_rwl_wrlock(rwlp);
|
|
|
|
if (handler != actp->sa_handler) {
|
|
|
|
_thr_rwl_unlock(rwlp);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
actp->sa_handler = SIG_DFL;
|
|
|
|
actp->sa_flags = SA_SIGINFO;
|
|
|
|
SIGEMPTYSET(actp->sa_mask);
|
|
|
|
if (__sys_sigaction(sig, NULL, &kact) == 0 &&
|
|
|
|
kact.sa_handler != SIG_DFL &&
|
|
|
|
kact.sa_handler != SIG_IGN)
|
|
|
|
__sys_sigaction(sig, actp, NULL);
|
2010-09-01 13:22:55 +00:00
|
|
|
_thr_rwl_unlock(rwlp);
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-01 13:22:55 +00:00
|
|
|
_thr_signal_unblock(curthread);
|
2010-09-06 03:00:54 +00:00
|
|
|
#endif
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_thr_signal_prefork(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2012-03-26 17:05:26 +00:00
|
|
|
for (i = 1; i <= _SIG_MAXSIG; ++i)
|
2015-01-03 18:38:46 +00:00
|
|
|
_thr_rwl_rdlock(&__libc_sigaction_slot(i)->lock);
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_thr_signal_postfork(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2012-03-26 17:05:26 +00:00
|
|
|
for (i = 1; i <= _SIG_MAXSIG; ++i)
|
2015-01-03 18:38:46 +00:00
|
|
|
_thr_rwl_unlock(&__libc_sigaction_slot(i)->lock);
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_thr_signal_postfork_child(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2015-01-03 18:38:46 +00:00
|
|
|
for (i = 1; i <= _SIG_MAXSIG; ++i) {
|
|
|
|
bzero(&__libc_sigaction_slot(i) -> lock,
|
|
|
|
sizeof(struct urwlock));
|
|
|
|
}
|
2005-04-02 01:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_thr_signal_deinit(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-01-03 18:38:46 +00:00
|
|
|
__thr_sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
|
2005-04-02 01:20:00 +00:00
|
|
|
{
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
struct sigaction newact, oldact, oldact2;
|
|
|
|
sigset_t oldset;
|
2015-01-03 18:38:46 +00:00
|
|
|
struct usigaction *usa;
|
|
|
|
int ret, err;
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
|
2010-07-12 10:15:33 +00:00
|
|
|
if (!_SIG_VALID(sig) || sig == SIGCANCEL) {
|
2005-04-02 01:20:00 +00:00
|
|
|
errno = EINVAL;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2015-01-03 18:38:46 +00:00
|
|
|
ret = 0;
|
|
|
|
err = 0;
|
|
|
|
usa = __libc_sigaction_slot(sig);
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
|
|
|
|
__sys_sigprocmask(SIG_SETMASK, &_thr_maskset, &oldset);
|
2015-01-03 18:38:46 +00:00
|
|
|
_thr_rwl_wrlock(&usa->lock);
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
|
|
|
|
if (act != NULL) {
|
2015-01-03 18:38:46 +00:00
|
|
|
oldact2 = usa->sigact;
|
|
|
|
newact = *act;
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* if a new sig handler is SIG_DFL or SIG_IGN,
|
2015-01-03 18:38:46 +00:00
|
|
|
* don't remove old handler from __libc_sigact[],
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
* so deferred signals still can use the handlers,
|
|
|
|
* multiple threads invoking sigaction itself is
|
|
|
|
* a race condition, so it is not a problem.
|
|
|
|
*/
|
|
|
|
if (newact.sa_handler != SIG_DFL &&
|
|
|
|
newact.sa_handler != SIG_IGN) {
|
2015-01-03 18:38:46 +00:00
|
|
|
usa->sigact = newact;
|
|
|
|
remove_thr_signals(&usa->sigact.sa_mask);
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
newact.sa_flags &= ~SA_NODEFER;
|
|
|
|
newact.sa_flags |= SA_SIGINFO;
|
|
|
|
newact.sa_sigaction = thr_sighandler;
|
|
|
|
newact.sa_mask = _thr_maskset; /* mask all signals */
|
|
|
|
}
|
2015-01-03 18:38:46 +00:00
|
|
|
ret = __sys_sigaction(sig, &newact, &oldact);
|
|
|
|
if (ret == -1) {
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
err = errno;
|
2015-01-03 18:38:46 +00:00
|
|
|
usa->sigact = oldact2;
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
}
|
|
|
|
} else if (oact != NULL) {
|
|
|
|
ret = __sys_sigaction(sig, NULL, &oldact);
|
|
|
|
err = errno;
|
|
|
|
}
|
|
|
|
|
2015-01-03 18:38:46 +00:00
|
|
|
if (oldact.sa_handler != SIG_DFL && oldact.sa_handler != SIG_IGN) {
|
2010-10-29 09:35:36 +00:00
|
|
|
if (act != NULL)
|
|
|
|
oldact = oldact2;
|
|
|
|
else if (oact != NULL)
|
2015-01-03 18:38:46 +00:00
|
|
|
oldact = usa->sigact;
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
}
|
|
|
|
|
2015-01-03 18:38:46 +00:00
|
|
|
_thr_rwl_unlock(&usa->lock);
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
__sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
|
|
|
|
|
|
|
|
if (ret == 0) {
|
|
|
|
if (oact != NULL)
|
|
|
|
*oact = oldact;
|
|
|
|
} else {
|
|
|
|
errno = err;
|
|
|
|
}
|
|
|
|
return (ret);
|
2005-04-02 01:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-01-03 18:38:46 +00:00
|
|
|
__thr_sigprocmask(int how, const sigset_t *set, sigset_t *oset)
|
2005-04-02 01:20:00 +00:00
|
|
|
{
|
|
|
|
const sigset_t *p = set;
|
|
|
|
sigset_t newset;
|
|
|
|
|
|
|
|
if (how != SIG_UNBLOCK) {
|
|
|
|
if (set != NULL) {
|
|
|
|
newset = *set;
|
|
|
|
SIGDELSET(newset, SIGCANCEL);
|
|
|
|
p = &newset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (__sys_sigprocmask(how, p, oset));
|
|
|
|
}
|
|
|
|
|
2003-04-01 03:46:29 +00:00
|
|
|
__weak_reference(_pthread_sigmask, pthread_sigmask);
|
|
|
|
|
|
|
|
int
|
|
|
|
_pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
|
|
|
|
{
|
2015-01-21 16:13:37 +00:00
|
|
|
|
|
|
|
if (__thr_sigprocmask(how, set, oset))
|
2005-04-02 01:20:00 +00:00
|
|
|
return (errno);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2010-08-20 13:42:48 +00:00
|
|
|
int
|
|
|
|
_sigsuspend(const sigset_t * set)
|
|
|
|
{
|
|
|
|
sigset_t newset;
|
2006-07-25 12:50:05 +00:00
|
|
|
|
2010-08-20 13:42:48 +00:00
|
|
|
return (__sys_sigsuspend(thr_remove_thr_signals(set, &newset)));
|
2006-07-25 12:50:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-01-03 18:38:46 +00:00
|
|
|
__thr_sigsuspend(const sigset_t * set)
|
2005-04-02 01:20:00 +00:00
|
|
|
{
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
struct pthread *curthread;
|
2005-04-02 01:20:00 +00:00
|
|
|
sigset_t newset;
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
int ret, old;
|
2003-04-01 03:46:29 +00:00
|
|
|
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
curthread = _get_curthread();
|
|
|
|
|
|
|
|
old = curthread->in_sigsuspend;
|
|
|
|
curthread->in_sigsuspend = 1;
|
2006-11-24 09:57:38 +00:00
|
|
|
_thr_cancel_enter(curthread);
|
2010-08-20 13:42:48 +00:00
|
|
|
ret = __sys_sigsuspend(thr_remove_thr_signals(set, &newset));
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
_thr_cancel_leave(curthread, 1);
|
|
|
|
curthread->in_sigsuspend = old;
|
|
|
|
if (curthread->unblock_sigcancel) {
|
|
|
|
curthread->unblock_sigcancel = 0;
|
|
|
|
SIGEMPTYSET(newset);
|
|
|
|
SIGADDSET(newset, SIGCANCEL);
|
|
|
|
__sys_sigprocmask(SIG_UNBLOCK, &newset, NULL);
|
|
|
|
}
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
return (ret);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
2006-07-25 12:50:05 +00:00
|
|
|
int
|
|
|
|
_sigtimedwait(const sigset_t *set, siginfo_t *info,
|
|
|
|
const struct timespec * timeout)
|
|
|
|
{
|
|
|
|
sigset_t newset;
|
|
|
|
|
2010-08-20 13:42:48 +00:00
|
|
|
return (__sys_sigtimedwait(thr_remove_thr_signals(set, &newset), info,
|
|
|
|
timeout));
|
2006-07-25 12:50:05 +00:00
|
|
|
}
|
|
|
|
|
In current implementation, thread cancellation is done in signal handler,
which does not know what is the state of interrupted system call, for
example, open() system call opened a file and the thread is still cancelled,
result is descriptor leak, there are other problems which can cause resource
leak or undeterminable side effect when a thread is cancelled. However, this
is no longer true in new implementation.
In defering mode, a thread is canceled if cancellation request is pending and
later the thread enters a cancellation point, otherwise, a later
pthread_cancel() just causes SIGCANCEL to be sent to the target thread, and
causes target thread to abort system call, userland code in libthr then checks
cancellation state, and cancels the thread if needed. For example, the
cancellation point open(), the thread may be canceled at start,
but later, if it opened a file descriptor, it is not canceled, this avoids
file handle leak. Another example is read(), a thread may be canceled at start
of the function, but later, if it read some bytes from a socket, the thread
is not canceled, the caller then can decide if it should still enable cancelling
or disable it and continue reading data until it thinks it has read all
bytes of a packet, and keeps a protocol stream in health state, if user ignores
partly reading of a packet without disabling cancellation, then second iteration
of read loop cause the thread to be cancelled.
An exception is that the close() cancellation point always closes a file handle
despite whether the thread is cancelled or not.
The old mechanism is still kept, for a functions which is not so easily to
fix a cancellation problem, the rough mechanism is used.
Reviewed by: kib@
2010-08-20 05:15:39 +00:00
|
|
|
/*
|
|
|
|
* Cancellation behavior:
|
|
|
|
* Thread may be canceled at start, if thread got signal,
|
|
|
|
* it is not canceled.
|
|
|
|
*/
|
2005-04-02 01:20:00 +00:00
|
|
|
int
|
2015-01-03 18:38:46 +00:00
|
|
|
__thr_sigtimedwait(const sigset_t *set, siginfo_t *info,
|
|
|
|
const struct timespec * timeout)
|
2005-04-02 01:20:00 +00:00
|
|
|
{
|
|
|
|
struct pthread *curthread = _get_curthread();
|
|
|
|
sigset_t newset;
|
|
|
|
int ret;
|
|
|
|
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
_thr_cancel_enter(curthread);
|
2010-08-20 13:42:48 +00:00
|
|
|
ret = __sys_sigtimedwait(thr_remove_thr_signals(set, &newset), info,
|
|
|
|
timeout);
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
_thr_cancel_leave(curthread, (ret == -1));
|
2005-04-02 01:20:00 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2006-07-25 12:50:05 +00:00
|
|
|
int
|
|
|
|
_sigwaitinfo(const sigset_t *set, siginfo_t *info)
|
|
|
|
{
|
|
|
|
sigset_t newset;
|
|
|
|
|
2010-08-20 13:42:48 +00:00
|
|
|
return (__sys_sigwaitinfo(thr_remove_thr_signals(set, &newset), info));
|
2006-07-25 12:50:05 +00:00
|
|
|
}
|
|
|
|
|
In current implementation, thread cancellation is done in signal handler,
which does not know what is the state of interrupted system call, for
example, open() system call opened a file and the thread is still cancelled,
result is descriptor leak, there are other problems which can cause resource
leak or undeterminable side effect when a thread is cancelled. However, this
is no longer true in new implementation.
In defering mode, a thread is canceled if cancellation request is pending and
later the thread enters a cancellation point, otherwise, a later
pthread_cancel() just causes SIGCANCEL to be sent to the target thread, and
causes target thread to abort system call, userland code in libthr then checks
cancellation state, and cancels the thread if needed. For example, the
cancellation point open(), the thread may be canceled at start,
but later, if it opened a file descriptor, it is not canceled, this avoids
file handle leak. Another example is read(), a thread may be canceled at start
of the function, but later, if it read some bytes from a socket, the thread
is not canceled, the caller then can decide if it should still enable cancelling
or disable it and continue reading data until it thinks it has read all
bytes of a packet, and keeps a protocol stream in health state, if user ignores
partly reading of a packet without disabling cancellation, then second iteration
of read loop cause the thread to be cancelled.
An exception is that the close() cancellation point always closes a file handle
despite whether the thread is cancelled or not.
The old mechanism is still kept, for a functions which is not so easily to
fix a cancellation problem, the rough mechanism is used.
Reviewed by: kib@
2010-08-20 05:15:39 +00:00
|
|
|
/*
|
|
|
|
* Cancellation behavior:
|
|
|
|
* Thread may be canceled at start, if thread got signal,
|
|
|
|
* it is not canceled.
|
|
|
|
*/
|
2003-04-01 03:46:29 +00:00
|
|
|
int
|
2015-01-03 18:38:46 +00:00
|
|
|
__thr_sigwaitinfo(const sigset_t *set, siginfo_t *info)
|
2003-04-01 03:46:29 +00:00
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
struct pthread *curthread = _get_curthread();
|
|
|
|
sigset_t newset;
|
|
|
|
int ret;
|
|
|
|
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
_thr_cancel_enter(curthread);
|
2010-08-20 13:42:48 +00:00
|
|
|
ret = __sys_sigwaitinfo(thr_remove_thr_signals(set, &newset), info);
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
_thr_cancel_leave(curthread, ret == -1);
|
2005-04-02 01:20:00 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2006-07-25 12:50:05 +00:00
|
|
|
int
|
|
|
|
_sigwait(const sigset_t *set, int *sig)
|
|
|
|
{
|
|
|
|
sigset_t newset;
|
|
|
|
|
2010-08-20 13:42:48 +00:00
|
|
|
return (__sys_sigwait(thr_remove_thr_signals(set, &newset), sig));
|
2006-07-25 12:50:05 +00:00
|
|
|
}
|
|
|
|
|
In current implementation, thread cancellation is done in signal handler,
which does not know what is the state of interrupted system call, for
example, open() system call opened a file and the thread is still cancelled,
result is descriptor leak, there are other problems which can cause resource
leak or undeterminable side effect when a thread is cancelled. However, this
is no longer true in new implementation.
In defering mode, a thread is canceled if cancellation request is pending and
later the thread enters a cancellation point, otherwise, a later
pthread_cancel() just causes SIGCANCEL to be sent to the target thread, and
causes target thread to abort system call, userland code in libthr then checks
cancellation state, and cancels the thread if needed. For example, the
cancellation point open(), the thread may be canceled at start,
but later, if it opened a file descriptor, it is not canceled, this avoids
file handle leak. Another example is read(), a thread may be canceled at start
of the function, but later, if it read some bytes from a socket, the thread
is not canceled, the caller then can decide if it should still enable cancelling
or disable it and continue reading data until it thinks it has read all
bytes of a packet, and keeps a protocol stream in health state, if user ignores
partly reading of a packet without disabling cancellation, then second iteration
of read loop cause the thread to be cancelled.
An exception is that the close() cancellation point always closes a file handle
despite whether the thread is cancelled or not.
The old mechanism is still kept, for a functions which is not so easily to
fix a cancellation problem, the rough mechanism is used.
Reviewed by: kib@
2010-08-20 05:15:39 +00:00
|
|
|
/*
|
|
|
|
* Cancellation behavior:
|
|
|
|
* Thread may be canceled at start, if thread got signal,
|
|
|
|
* it is not canceled.
|
|
|
|
*/
|
2005-04-02 01:20:00 +00:00
|
|
|
int
|
2015-01-03 18:38:46 +00:00
|
|
|
__thr_sigwait(const sigset_t *set, int *sig)
|
2005-04-02 01:20:00 +00:00
|
|
|
{
|
|
|
|
struct pthread *curthread = _get_curthread();
|
|
|
|
sigset_t newset;
|
|
|
|
int ret;
|
|
|
|
|
2010-09-10 01:47:37 +00:00
|
|
|
do {
|
|
|
|
_thr_cancel_enter(curthread);
|
|
|
|
ret = __sys_sigwait(thr_remove_thr_signals(set, &newset), sig);
|
|
|
|
_thr_cancel_leave(curthread, (ret != 0));
|
|
|
|
} while (ret == EINTR);
|
2005-04-02 01:20:00 +00:00
|
|
|
return (ret);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
2010-08-24 09:57:06 +00:00
|
|
|
|
|
|
|
int
|
2015-01-03 18:38:46 +00:00
|
|
|
__thr_setcontext(const ucontext_t *ucp)
|
2010-08-24 09:57:06 +00:00
|
|
|
{
|
|
|
|
ucontext_t uc;
|
|
|
|
|
2013-05-09 04:41:03 +00:00
|
|
|
if (ucp == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return (-1);
|
|
|
|
}
|
2013-04-18 05:56:00 +00:00
|
|
|
if (!SIGISMEMBER(uc.uc_sigmask, SIGCANCEL))
|
|
|
|
return __sys_setcontext(ucp);
|
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.
2010-09-01 02:18:33 +00:00
|
|
|
(void) memcpy(&uc, ucp, sizeof(uc));
|
2013-04-18 05:56:00 +00:00
|
|
|
SIGDELSET(uc.uc_sigmask, SIGCANCEL);
|
2015-01-03 18:38:46 +00:00
|
|
|
return (__sys_setcontext(&uc));
|
2010-08-24 09:57:06 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 05:12:11 +00:00
|
|
|
int
|
2015-01-03 18:38:46 +00:00
|
|
|
__thr_swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
|
2013-04-18 05:12:11 +00:00
|
|
|
{
|
|
|
|
ucontext_t uc;
|
|
|
|
|
2013-05-09 04:41:03 +00:00
|
|
|
if (oucp == NULL || ucp == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return (-1);
|
|
|
|
}
|
2013-04-18 05:56:00 +00:00
|
|
|
if (SIGISMEMBER(ucp->uc_sigmask, SIGCANCEL)) {
|
|
|
|
(void) memcpy(&uc, ucp, sizeof(uc));
|
|
|
|
SIGDELSET(uc.uc_sigmask, SIGCANCEL);
|
|
|
|
ucp = &uc;
|
|
|
|
}
|
2015-01-03 18:38:46 +00:00
|
|
|
return (__sys_swapcontext(oucp, ucp));
|
2013-04-18 05:12:11 +00:00
|
|
|
}
|