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 "namespace.h"
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/signalvar.h>
39 #include "un-namespace.h"
41 #include "thr_private.h"
43 /* #define DEBUG_SIGNAL */
45 #define DBG_MSG stdout_debug
50 extern int __pause(void);
53 int __sigtimedwait(const sigset_t *set, siginfo_t *info,
54 const struct timespec * timeout);
55 int __sigwaitinfo(const sigset_t *set, siginfo_t *info);
56 int __sigwait(const sigset_t *set, int *sig);
60 sigcancel_handler(int sig __unused,
61 siginfo_t *info __unused, ucontext_t *ucp __unused)
63 struct pthread *curthread = _get_curthread();
65 if (curthread->cancel_defer && curthread->cancel_pending)
66 thr_wake(curthread->tid);
71 _thr_ast(struct pthread *curthread)
73 if (!THR_IN_CRITICAL(curthread)) {
74 _thr_testcancel(curthread);
75 if (__predict_false((curthread->flags &
76 (THR_FLAGS_NEED_SUSPEND | THR_FLAGS_SUSPENDED))
77 == THR_FLAGS_NEED_SUSPEND))
78 _thr_suspend_check(curthread);
83 _thr_suspend_check(struct pthread *curthread)
90 * Blocks SIGCANCEL which other threads must send.
92 _thr_signal_block(curthread);
95 * Increase critical_count, here we don't use THR_LOCK/UNLOCK
96 * because we are leaf code, we don't want to recursively call
99 curthread->critical_count++;
100 THR_UMUTEX_LOCK(curthread, &(curthread)->lock);
101 while ((curthread->flags & (THR_FLAGS_NEED_SUSPEND |
102 THR_FLAGS_SUSPENDED)) == THR_FLAGS_NEED_SUSPEND) {
104 cycle = curthread->cycle;
106 /* Wake the thread suspending us. */
107 _thr_umtx_wake(&curthread->cycle, INT_MAX);
110 * if we are from pthread_exit, we don't want to
111 * suspend, just go and die.
113 if (curthread->state == PS_DEAD)
115 curthread->flags |= THR_FLAGS_SUSPENDED;
116 THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock);
117 _thr_umtx_wait(&curthread->cycle, cycle, NULL);
118 THR_UMUTEX_LOCK(curthread, &(curthread)->lock);
119 curthread->flags &= ~THR_FLAGS_SUSPENDED;
121 THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock);
122 curthread->critical_count--;
125 * Unblocks SIGCANCEL, it is possible a new SIGCANCEL is ready and
126 * a new signal frame will nest us, this seems a problem because
127 * stack will grow and overflow, but because kernel will automatically
128 * mask the SIGCANCEL when delivering the signal, so we at most only
129 * have one nesting signal frame, this should be fine.
131 _thr_signal_unblock(curthread);
136 _thr_signal_init(void)
138 struct sigaction act;
140 /* Install cancel handler. */
141 SIGEMPTYSET(act.sa_mask);
142 act.sa_flags = SA_SIGINFO | SA_RESTART;
143 act.sa_sigaction = (__siginfohandler_t *)&sigcancel_handler;
144 __sys_sigaction(SIGCANCEL, &act, NULL);
148 _thr_signal_deinit(void)
152 __weak_reference(___pause, pause);
157 struct pthread *curthread = _get_curthread();
160 _thr_cancel_enter(curthread);
162 _thr_cancel_leave(curthread);
167 __weak_reference(_raise, raise);
174 if (!_thr_isthreaded())
175 ret = kill(getpid(), sig);
177 ret = _thr_send_sig(_get_curthread(), sig);
181 __weak_reference(_sigaction, sigaction);
184 _sigaction(int sig, const struct sigaction * act, struct sigaction * oact)
186 /* Check if the signal number is out of range: */
187 if (sig < 1 || sig > _SIG_MAXSIG || sig == SIGCANCEL) {
188 /* Return an invalid argument: */
193 return __sys_sigaction(sig, act, oact);
196 __weak_reference(_sigprocmask, sigprocmask);
199 _sigprocmask(int how, const sigset_t *set, sigset_t *oset)
201 const sigset_t *p = set;
204 if (how != SIG_UNBLOCK) {
207 SIGDELSET(newset, SIGCANCEL);
211 return (__sys_sigprocmask(how, p, oset));
214 __weak_reference(_pthread_sigmask, pthread_sigmask);
217 _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
219 if (_sigprocmask(how, set, oset))
224 __weak_reference(__sigsuspend, sigsuspend);
227 _sigsuspend(const sigset_t * set)
230 const sigset_t *pset;
233 if (SIGISMEMBER(*set, SIGCANCEL)) {
235 SIGDELSET(newset, SIGCANCEL);
240 ret = __sys_sigsuspend(pset);
246 __sigsuspend(const sigset_t * set)
248 struct pthread *curthread = _get_curthread();
250 const sigset_t *pset;
253 if (SIGISMEMBER(*set, SIGCANCEL)) {
255 SIGDELSET(newset, SIGCANCEL);
260 _thr_cancel_enter(curthread);
261 ret = __sys_sigsuspend(pset);
262 _thr_cancel_leave(curthread);
267 __weak_reference(__sigwait, sigwait);
268 __weak_reference(__sigtimedwait, sigtimedwait);
269 __weak_reference(__sigwaitinfo, sigwaitinfo);
272 _sigtimedwait(const sigset_t *set, siginfo_t *info,
273 const struct timespec * timeout)
276 const sigset_t *pset;
279 if (SIGISMEMBER(*set, SIGCANCEL)) {
281 SIGDELSET(newset, SIGCANCEL);
285 ret = __sys_sigtimedwait(pset, info, timeout);
290 __sigtimedwait(const sigset_t *set, siginfo_t *info,
291 const struct timespec * timeout)
293 struct pthread *curthread = _get_curthread();
295 const sigset_t *pset;
298 if (SIGISMEMBER(*set, SIGCANCEL)) {
300 SIGDELSET(newset, SIGCANCEL);
304 _thr_cancel_enter(curthread);
305 ret = __sys_sigtimedwait(pset, info, timeout);
306 _thr_cancel_leave(curthread);
311 _sigwaitinfo(const sigset_t *set, siginfo_t *info)
314 const sigset_t *pset;
317 if (SIGISMEMBER(*set, SIGCANCEL)) {
319 SIGDELSET(newset, SIGCANCEL);
324 ret = __sys_sigwaitinfo(pset, info);
329 __sigwaitinfo(const sigset_t *set, siginfo_t *info)
331 struct pthread *curthread = _get_curthread();
333 const sigset_t *pset;
336 if (SIGISMEMBER(*set, SIGCANCEL)) {
338 SIGDELSET(newset, SIGCANCEL);
343 _thr_cancel_enter(curthread);
344 ret = __sys_sigwaitinfo(pset, info);
345 _thr_cancel_leave(curthread);
350 _sigwait(const sigset_t *set, int *sig)
353 const sigset_t *pset;
356 if (SIGISMEMBER(*set, SIGCANCEL)) {
358 SIGDELSET(newset, SIGCANCEL);
363 ret = __sys_sigwait(pset, sig);
368 __sigwait(const sigset_t *set, int *sig)
370 struct pthread *curthread = _get_curthread();
372 const sigset_t *pset;
375 if (SIGISMEMBER(*set, SIGCANCEL)) {
377 SIGDELSET(newset, SIGCANCEL);
382 _thr_cancel_enter(curthread);
383 ret = __sys_sigwait(pset, sig);
384 _thr_cancel_leave(curthread);