2 * Copyright (c) 2005, David Xu <davidxu@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/signalvar.h>
39 #include "thr_private.h"
41 /* #define DEBUG_SIGNAL */
43 #define DBG_MSG stdout_debug
49 sigcancel_handler(int sig, siginfo_t *info, ucontext_t *ucp)
51 struct pthread *curthread = _get_curthread();
57 _thr_ast(struct pthread *curthread)
59 if (!THR_IN_CRITICAL(curthread)) {
61 SHOULD_ASYNC_CANCEL(curthread->cancelflags)))
62 _pthread_testcancel();
63 if (__predict_false((curthread->flags &
64 (THR_FLAGS_NEED_SUSPEND | THR_FLAGS_SUSPENDED))
65 == THR_FLAGS_NEED_SUSPEND))
66 _thr_suspend_check(curthread);
71 _thr_suspend_check(struct pthread *curthread)
78 * Blocks SIGCANCEL which other threads must send.
80 _thr_signal_block(curthread);
83 * Increase critical_count, here we don't use THR_LOCK/UNLOCK
84 * because we are leaf code, we don't want to recursively call
87 curthread->critical_count++;
88 THR_UMTX_LOCK(curthread, &(curthread)->lock);
89 while ((curthread->flags & (THR_FLAGS_NEED_SUSPEND |
90 THR_FLAGS_SUSPENDED)) == THR_FLAGS_NEED_SUSPEND) {
92 cycle = curthread->cycle;
94 /* Wake the thread suspending us. */
95 _thr_umtx_wake(&curthread->cycle, INT_MAX);
98 * if we are from pthread_exit, we don't want to
99 * suspend, just go and die.
101 if (curthread->state == PS_DEAD)
103 curthread->flags |= THR_FLAGS_SUSPENDED;
104 THR_UMTX_UNLOCK(curthread, &(curthread)->lock);
105 _thr_umtx_wait(&curthread->cycle, cycle, NULL);
106 THR_UMTX_LOCK(curthread, &(curthread)->lock);
107 curthread->flags &= ~THR_FLAGS_SUSPENDED;
109 THR_UMTX_UNLOCK(curthread, &(curthread)->lock);
110 curthread->critical_count--;
113 * Unblocks SIGCANCEL, it is possible a new SIGCANCEL is ready and
114 * a new signal frame will nest us, this seems a problem because
115 * stack will grow and overflow, but because kernel will automatically
116 * mask the SIGCANCEL when delivering the signal, so we at most only
117 * have one nesting signal frame, this should be fine.
119 _thr_signal_unblock(curthread);
124 _thr_signal_init(void)
126 struct sigaction act;
128 /* Install cancel handler. */
129 SIGEMPTYSET(act.sa_mask);
130 act.sa_flags = SA_SIGINFO | SA_RESTART;
131 act.sa_sigaction = (__siginfohandler_t *)&sigcancel_handler;
132 __sys_sigaction(SIGCANCEL, &act, NULL);
136 _thr_signal_deinit(void)
140 __weak_reference(_sigaction, sigaction);
143 _sigaction(int sig, const struct sigaction * act, struct sigaction * oact)
145 /* Check if the signal number is out of range: */
146 if (sig < 1 || sig > _SIG_MAXSIG || sig == SIGCANCEL) {
147 /* Return an invalid argument: */
152 return __sys_sigaction(sig, act, oact);
155 __weak_reference(_sigprocmask, sigprocmask);
158 _sigprocmask(int how, const sigset_t *set, sigset_t *oset)
160 const sigset_t *p = set;
163 if (how != SIG_UNBLOCK) {
166 SIGDELSET(newset, SIGCANCEL);
170 return (__sys_sigprocmask(how, p, oset));
173 __weak_reference(_pthread_sigmask, pthread_sigmask);
176 _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
178 if (_sigprocmask(how, set, oset))
183 __weak_reference(_sigsuspend, sigsuspend);
186 _sigsuspend(const sigset_t * set)
188 struct pthread *curthread = _get_curthread();
190 const sigset_t *pset;
194 if (SIGISMEMBER(*set, SIGCANCEL)) {
196 SIGDELSET(newset, SIGCANCEL);
201 oldcancel = _thr_cancel_enter(curthread);
202 ret = __sys_sigsuspend(pset);
203 _thr_cancel_leave(curthread, oldcancel);
208 __weak_reference(__sigwait, sigwait);
209 __weak_reference(__sigtimedwait, sigtimedwait);
210 __weak_reference(__sigwaitinfo, sigwaitinfo);
213 __sigtimedwait(const sigset_t *set, siginfo_t *info,
214 const struct timespec * timeout)
216 struct pthread *curthread = _get_curthread();
218 const sigset_t *pset;
222 if (SIGISMEMBER(*set, SIGCANCEL)) {
224 SIGDELSET(newset, SIGCANCEL);
228 oldcancel = _thr_cancel_enter(curthread);
229 ret = __sys_sigtimedwait(pset, info, timeout);
230 _thr_cancel_leave(curthread, oldcancel);
235 __sigwaitinfo(const sigset_t *set, siginfo_t *info)
237 struct pthread *curthread = _get_curthread();
239 const sigset_t *pset;
243 if (SIGISMEMBER(*set, SIGCANCEL)) {
245 SIGDELSET(newset, SIGCANCEL);
250 oldcancel = _thr_cancel_enter(curthread);
251 ret = __sys_sigwaitinfo(pset, info);
252 _thr_cancel_leave(curthread, oldcancel);
257 __sigwait(const sigset_t *set, int *sig)
259 struct pthread *curthread = _get_curthread();
261 const sigset_t *pset;
265 if (SIGISMEMBER(*set, SIGCANCEL)) {
267 SIGDELSET(newset, SIGCANCEL);
272 oldcancel = _thr_cancel_enter(curthread);
273 ret = __sys_sigwait(pset, sig);
274 _thr_cancel_leave(curthread, oldcancel);