2 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
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, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by John Birrell.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $DragonFly: src/lib/libthread_xu/thread/thr_sig.c,v 1.3 2005/03/15 11:24:23 davidxu Exp $
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/signalvar.h>
45 #include "thr_private.h"
47 /* #define DEBUG_SIGNAL */
49 #define DBG_MSG stdout_debug
55 sigcancel_handler(int sig, siginfo_t *info, ucontext_t *ucp)
57 struct pthread *curthread = _get_curthread();
59 if (curthread->cancelflags & THR_CANCEL_AT_POINT)
61 if (curthread->flags & THR_FLAGS_NEED_SUSPEND) {
62 __sys_sigprocmask(SIG_SETMASK, &ucp->uc_sigmask, NULL);
63 _thr_suspend_check(curthread);
68 _thr_suspend_check(struct pthread *curthread)
73 _thr_signal_block(curthread);
75 if ((curthread->flags & (THR_FLAGS_NEED_SUSPEND | THR_FLAGS_SUSPENDED))
76 == THR_FLAGS_NEED_SUSPEND) {
77 curthread->flags |= THR_FLAGS_SUSPENDED;
78 while (curthread->flags & THR_FLAGS_NEED_SUSPEND) {
79 cycle = curthread->cycle;
80 THR_UNLOCK(curthread);
81 _thr_signal_unblock(curthread);
82 _thr_umtx_wait(&curthread->cycle, cycle, NULL, 0);
83 _thr_signal_block(curthread);
86 curthread->flags &= ~THR_FLAGS_SUSPENDED;
88 THR_UNLOCK(curthread);
89 _thr_signal_unblock(curthread);
93 _thr_signal_init(void)
97 /* Install cancel handler. */
98 SIGEMPTYSET(act.sa_mask);
99 act.sa_flags = SA_SIGINFO | SA_RESTART;
100 act.sa_sigaction = (__siginfohandler_t *)&sigcancel_handler;
101 __sys_sigaction(SIGCANCEL, &act, NULL);
105 _thr_signal_deinit(void)
109 __weak_reference(_sigaction, sigaction);
112 _sigaction(int sig, const struct sigaction * act, struct sigaction * oact)
114 /* Check if the signal number is out of range: */
115 if (sig < 1 || sig > _SIG_MAXSIG || sig == SIGCANCEL) {
116 /* Return an invalid argument: */
121 return __sys_sigaction(sig, act, oact);
124 __weak_reference(_sigprocmask, sigprocmask);
127 _sigprocmask(int how, const sigset_t *set, sigset_t *oset)
129 const sigset_t *p = set;
132 if (how != SIG_UNBLOCK) {
135 SIGDELSET(newset, SIGCANCEL);
139 return (__sys_sigprocmask(how, p, oset));
142 __weak_reference(_pthread_sigmask, pthread_sigmask);
145 _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
147 if (_sigprocmask(how, set, oset))
152 __weak_reference(_sigsuspend, sigsuspend);
155 _sigsuspend(const sigset_t * set)
157 struct pthread *curthread = _get_curthread();
159 const sigset_t *pset;
163 if (SIGISMEMBER(*set, SIGCANCEL)) {
165 SIGDELSET(newset, SIGCANCEL);
170 oldcancel = _thr_cancel_enter(curthread);
171 ret = __sys_sigsuspend(pset);
172 _thr_cancel_leave(curthread, oldcancel);
177 __weak_reference(__sigwait, sigwait);
178 __weak_reference(__sigtimedwait, sigtimedwait);
179 __weak_reference(__sigwaitinfo, sigwaitinfo);
182 __sigtimedwait(const sigset_t *set, siginfo_t *info,
183 const struct timespec * timeout)
185 struct pthread *curthread = _get_curthread();
187 const sigset_t *pset;
191 if (SIGISMEMBER(*set, SIGCANCEL)) {
193 SIGDELSET(newset, SIGCANCEL);
197 oldcancel = _thr_cancel_enter(curthread);
198 ret = __sys_sigtimedwait(pset, info, timeout);
199 _thr_cancel_leave(curthread, oldcancel);
204 __sigwaitinfo(const sigset_t *set, siginfo_t *info)
206 struct pthread *curthread = _get_curthread();
208 const sigset_t *pset;
212 if (SIGISMEMBER(*set, SIGCANCEL)) {
214 SIGDELSET(newset, SIGCANCEL);
219 oldcancel = _thr_cancel_enter(curthread);
220 ret = __sys_sigwaitinfo(pset, info);
221 _thr_cancel_leave(curthread, oldcancel);
226 __sigwait(const sigset_t *set, int *sig)
230 s = __sigwaitinfo(set, NULL);