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);
59 sigcancel_handler(int sig __unused,
60 siginfo_t *info __unused, ucontext_t *ucp __unused)
62 struct pthread *curthread = _get_curthread();
64 if (curthread->cancel_defer && curthread->cancel_pending)
65 thr_wake(curthread->tid);
70 _thr_ast(struct pthread *curthread)
72 if (!THR_IN_CRITICAL(curthread)) {
73 _thr_testcancel(curthread);
74 if (__predict_false((curthread->flags &
75 (THR_FLAGS_NEED_SUSPEND | THR_FLAGS_SUSPENDED))
76 == THR_FLAGS_NEED_SUSPEND))
77 _thr_suspend_check(curthread);
82 _thr_suspend_check(struct pthread *curthread)
89 * Blocks SIGCANCEL which other threads must send.
91 _thr_signal_block(curthread);
94 * Increase critical_count, here we don't use THR_LOCK/UNLOCK
95 * because we are leaf code, we don't want to recursively call
98 curthread->critical_count++;
99 THR_UMUTEX_LOCK(curthread, &(curthread)->lock);
100 while ((curthread->flags & (THR_FLAGS_NEED_SUSPEND |
101 THR_FLAGS_SUSPENDED)) == THR_FLAGS_NEED_SUSPEND) {
103 cycle = curthread->cycle;
105 /* Wake the thread suspending us. */
106 _thr_umtx_wake(&curthread->cycle, INT_MAX);
109 * if we are from pthread_exit, we don't want to
110 * suspend, just go and die.
112 if (curthread->state == PS_DEAD)
114 curthread->flags |= THR_FLAGS_SUSPENDED;
115 THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock);
116 _thr_umtx_wait(&curthread->cycle, cycle, NULL);
117 THR_UMUTEX_LOCK(curthread, &(curthread)->lock);
118 curthread->flags &= ~THR_FLAGS_SUSPENDED;
120 THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock);
121 curthread->critical_count--;
124 * Unblocks SIGCANCEL, it is possible a new SIGCANCEL is ready and
125 * a new signal frame will nest us, this seems a problem because
126 * stack will grow and overflow, but because kernel will automatically
127 * mask the SIGCANCEL when delivering the signal, so we at most only
128 * have one nesting signal frame, this should be fine.
130 _thr_signal_unblock(curthread);
135 _thr_signal_init(void)
137 struct sigaction act;
139 /* Install cancel handler. */
140 SIGEMPTYSET(act.sa_mask);
141 act.sa_flags = SA_SIGINFO | SA_RESTART;
142 act.sa_sigaction = (__siginfohandler_t *)&sigcancel_handler;
143 __sys_sigaction(SIGCANCEL, &act, NULL);
147 _thr_signal_deinit(void)
151 __weak_reference(___pause, pause);
156 struct pthread *curthread = _get_curthread();
159 _thr_cancel_enter(curthread);
161 _thr_cancel_leave(curthread);
166 __weak_reference(_raise, raise);
173 if (!_thr_isthreaded())
174 ret = kill(getpid(), sig);
176 ret = _thr_send_sig(_get_curthread(), sig);
180 __weak_reference(_sigaction, sigaction);
183 _sigaction(int sig, const struct sigaction * act, struct sigaction * oact)
185 /* Check if the signal number is out of range: */
186 if (sig < 1 || sig > _SIG_MAXSIG || sig == SIGCANCEL) {
187 /* Return an invalid argument: */
192 return __sys_sigaction(sig, act, oact);
195 __weak_reference(_sigprocmask, sigprocmask);
198 _sigprocmask(int how, const sigset_t *set, sigset_t *oset)
200 const sigset_t *p = set;
203 if (how != SIG_UNBLOCK) {
206 SIGDELSET(newset, SIGCANCEL);
210 return (__sys_sigprocmask(how, p, oset));
213 __weak_reference(_pthread_sigmask, pthread_sigmask);
216 _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
218 if (_sigprocmask(how, set, oset))
223 __weak_reference(__sigsuspend, sigsuspend);
226 _sigsuspend(const sigset_t * set)
229 const sigset_t *pset;
232 if (SIGISMEMBER(*set, SIGCANCEL)) {
234 SIGDELSET(newset, SIGCANCEL);
239 ret = __sys_sigsuspend(pset);
245 __sigsuspend(const sigset_t * set)
247 struct pthread *curthread = _get_curthread();
249 const sigset_t *pset;
252 if (SIGISMEMBER(*set, SIGCANCEL)) {
254 SIGDELSET(newset, SIGCANCEL);
259 _thr_cancel_enter(curthread);
260 ret = __sys_sigsuspend(pset);
261 _thr_cancel_leave(curthread);
266 __weak_reference(__sigwait, sigwait);
267 __weak_reference(__sigtimedwait, sigtimedwait);
268 __weak_reference(__sigwaitinfo, sigwaitinfo);
271 _sigtimedwait(const sigset_t *set, siginfo_t *info,
272 const struct timespec * timeout)
275 const sigset_t *pset;
278 if (SIGISMEMBER(*set, SIGCANCEL)) {
280 SIGDELSET(newset, SIGCANCEL);
284 ret = __sys_sigtimedwait(pset, info, timeout);
289 __sigtimedwait(const sigset_t *set, siginfo_t *info,
290 const struct timespec * timeout)
292 struct pthread *curthread = _get_curthread();
294 const sigset_t *pset;
297 if (SIGISMEMBER(*set, SIGCANCEL)) {
299 SIGDELSET(newset, SIGCANCEL);
303 _thr_cancel_enter(curthread);
304 ret = __sys_sigtimedwait(pset, info, timeout);
305 _thr_cancel_leave(curthread);
310 _sigwaitinfo(const sigset_t *set, siginfo_t *info)
313 const sigset_t *pset;
316 if (SIGISMEMBER(*set, SIGCANCEL)) {
318 SIGDELSET(newset, SIGCANCEL);
323 ret = __sys_sigwaitinfo(pset, info);
328 __sigwaitinfo(const sigset_t *set, siginfo_t *info)
330 struct pthread *curthread = _get_curthread();
332 const sigset_t *pset;
335 if (SIGISMEMBER(*set, SIGCANCEL)) {
337 SIGDELSET(newset, SIGCANCEL);
342 _thr_cancel_enter(curthread);
343 ret = __sys_sigwaitinfo(pset, info);
344 _thr_cancel_leave(curthread);
349 _sigwait(const sigset_t *set, int *sig)
352 const sigset_t *pset;
355 if (SIGISMEMBER(*set, SIGCANCEL)) {
357 SIGDELSET(newset, SIGCANCEL);
362 ret = __sys_sigwait(pset, sig);
367 __sigwait(const sigset_t *set, int *sig)
369 struct pthread *curthread = _get_curthread();
371 const sigset_t *pset;
374 if (SIGISMEMBER(*set, SIGCANCEL)) {
376 SIGDELSET(newset, SIGCANCEL);
381 _thr_cancel_enter(curthread);
382 ret = __sys_sigwait(pset, sig);
383 _thr_cancel_leave(curthread);