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 _sigtimedwait(const sigset_t *set, siginfo_t *info,
56 const struct timespec * timeout);
57 int __sigwaitinfo(const sigset_t *set, siginfo_t *info);
58 int _sigwaitinfo(const sigset_t *set, siginfo_t *info);
59 int __sigwait(const sigset_t *set, int *sig);
60 int _sigwait(const sigset_t *set, int *sig);
61 int __sigsuspend(const sigset_t *sigmask);
65 sigcancel_handler(int sig __unused,
66 siginfo_t *info __unused, ucontext_t *ucp __unused)
68 struct pthread *curthread = _get_curthread();
70 if (curthread->cancel_defer && curthread->cancel_pending)
71 thr_wake(curthread->tid);
76 _thr_ast(struct pthread *curthread)
78 if (!THR_IN_CRITICAL(curthread)) {
79 _thr_testcancel(curthread);
80 if (__predict_false((curthread->flags &
81 (THR_FLAGS_NEED_SUSPEND | THR_FLAGS_SUSPENDED))
82 == THR_FLAGS_NEED_SUSPEND))
83 _thr_suspend_check(curthread);
88 _thr_suspend_check(struct pthread *curthread)
95 * Blocks SIGCANCEL which other threads must send.
97 _thr_signal_block(curthread);
100 * Increase critical_count, here we don't use THR_LOCK/UNLOCK
101 * because we are leaf code, we don't want to recursively call
104 curthread->critical_count++;
105 THR_UMUTEX_LOCK(curthread, &(curthread)->lock);
106 while ((curthread->flags & (THR_FLAGS_NEED_SUSPEND |
107 THR_FLAGS_SUSPENDED)) == THR_FLAGS_NEED_SUSPEND) {
109 cycle = curthread->cycle;
111 /* Wake the thread suspending us. */
112 _thr_umtx_wake(&curthread->cycle, INT_MAX);
115 * if we are from pthread_exit, we don't want to
116 * suspend, just go and die.
118 if (curthread->state == PS_DEAD)
120 curthread->flags |= THR_FLAGS_SUSPENDED;
121 THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock);
122 _thr_umtx_wait(&curthread->cycle, cycle, NULL);
123 THR_UMUTEX_LOCK(curthread, &(curthread)->lock);
124 curthread->flags &= ~THR_FLAGS_SUSPENDED;
126 THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock);
127 curthread->critical_count--;
130 * Unblocks SIGCANCEL, it is possible a new SIGCANCEL is ready and
131 * a new signal frame will nest us, this seems a problem because
132 * stack will grow and overflow, but because kernel will automatically
133 * mask the SIGCANCEL when delivering the signal, so we at most only
134 * have one nesting signal frame, this should be fine.
136 _thr_signal_unblock(curthread);
141 _thr_signal_init(void)
143 struct sigaction act;
145 /* Install cancel handler. */
146 SIGEMPTYSET(act.sa_mask);
147 act.sa_flags = SA_SIGINFO | SA_RESTART;
148 act.sa_sigaction = (__siginfohandler_t *)&sigcancel_handler;
149 __sys_sigaction(SIGCANCEL, &act, NULL);
153 _thr_signal_deinit(void)
157 __weak_reference(___pause, pause);
162 struct pthread *curthread = _get_curthread();
165 _thr_cancel_enter(curthread);
167 _thr_cancel_leave(curthread);
172 __weak_reference(_raise, raise);
179 if (!_thr_isthreaded())
180 ret = kill(getpid(), sig);
182 ret = _thr_send_sig(_get_curthread(), sig);
186 __weak_reference(_sigaction, sigaction);
189 _sigaction(int sig, const struct sigaction * act, struct sigaction * oact)
191 /* Check if the signal number is out of range: */
192 if (sig < 1 || sig > _SIG_MAXSIG || sig == SIGCANCEL) {
193 /* Return an invalid argument: */
198 return __sys_sigaction(sig, act, oact);
201 __weak_reference(_sigprocmask, sigprocmask);
204 _sigprocmask(int how, const sigset_t *set, sigset_t *oset)
206 const sigset_t *p = set;
209 if (how != SIG_UNBLOCK) {
212 SIGDELSET(newset, SIGCANCEL);
216 return (__sys_sigprocmask(how, p, oset));
219 __weak_reference(_pthread_sigmask, pthread_sigmask);
222 _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
224 if (_sigprocmask(how, set, oset))
229 __weak_reference(__sigsuspend, sigsuspend);
232 _sigsuspend(const sigset_t * set)
235 const sigset_t *pset;
238 if (SIGISMEMBER(*set, SIGCANCEL)) {
240 SIGDELSET(newset, SIGCANCEL);
245 ret = __sys_sigsuspend(pset);
251 __sigsuspend(const sigset_t * set)
253 struct pthread *curthread = _get_curthread();
255 const sigset_t *pset;
258 if (SIGISMEMBER(*set, SIGCANCEL)) {
260 SIGDELSET(newset, SIGCANCEL);
265 _thr_cancel_enter(curthread);
266 ret = __sys_sigsuspend(pset);
267 _thr_cancel_leave(curthread);
272 __weak_reference(__sigwait, sigwait);
273 __weak_reference(__sigtimedwait, sigtimedwait);
274 __weak_reference(__sigwaitinfo, sigwaitinfo);
277 _sigtimedwait(const sigset_t *set, siginfo_t *info,
278 const struct timespec * timeout)
281 const sigset_t *pset;
284 if (SIGISMEMBER(*set, SIGCANCEL)) {
286 SIGDELSET(newset, SIGCANCEL);
290 ret = __sys_sigtimedwait(pset, info, timeout);
295 __sigtimedwait(const sigset_t *set, siginfo_t *info,
296 const struct timespec * timeout)
298 struct pthread *curthread = _get_curthread();
300 const sigset_t *pset;
303 if (SIGISMEMBER(*set, SIGCANCEL)) {
305 SIGDELSET(newset, SIGCANCEL);
309 _thr_cancel_enter(curthread);
310 ret = __sys_sigtimedwait(pset, info, timeout);
311 _thr_cancel_leave(curthread);
316 _sigwaitinfo(const sigset_t *set, siginfo_t *info)
319 const sigset_t *pset;
322 if (SIGISMEMBER(*set, SIGCANCEL)) {
324 SIGDELSET(newset, SIGCANCEL);
329 ret = __sys_sigwaitinfo(pset, info);
334 __sigwaitinfo(const sigset_t *set, siginfo_t *info)
336 struct pthread *curthread = _get_curthread();
338 const sigset_t *pset;
341 if (SIGISMEMBER(*set, SIGCANCEL)) {
343 SIGDELSET(newset, SIGCANCEL);
348 _thr_cancel_enter(curthread);
349 ret = __sys_sigwaitinfo(pset, info);
350 _thr_cancel_leave(curthread);
355 _sigwait(const sigset_t *set, int *sig)
358 const sigset_t *pset;
361 if (SIGISMEMBER(*set, SIGCANCEL)) {
363 SIGDELSET(newset, SIGCANCEL);
368 ret = __sys_sigwait(pset, sig);
373 __sigwait(const sigset_t *set, int *sig)
375 struct pthread *curthread = _get_curthread();
377 const sigset_t *pset;
380 if (SIGISMEMBER(*set, SIGCANCEL)) {
382 SIGDELSET(newset, SIGCANCEL);
387 _thr_cancel_enter(curthread);
388 ret = __sys_sigwait(pset, sig);
389 _thr_cancel_leave(curthread);