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.4 2005/03/29 19:26:20 joerg Exp $
35 #include <sys/param.h>
36 #include <sys/signalvar.h>
38 #include <machine/tls.h>
47 #include "thr_private.h"
49 /* #define DEBUG_SIGNAL */
51 #define DBG_MSG stdout_debug
57 sigcancel_handler(int sig, siginfo_t *info, ucontext_t *ucp)
59 struct pthread *curthread = tls_get_curthread();
61 if (curthread->cancelflags & THR_CANCEL_AT_POINT)
63 if (curthread->flags & THR_FLAGS_NEED_SUSPEND) {
64 __sys_sigprocmask(SIG_SETMASK, &ucp->uc_sigmask, NULL);
65 _thr_suspend_check(curthread);
70 _thr_suspend_check(struct pthread *curthread)
75 _thr_signal_block(curthread);
77 if ((curthread->flags & (THR_FLAGS_NEED_SUSPEND | THR_FLAGS_SUSPENDED))
78 == THR_FLAGS_NEED_SUSPEND) {
79 curthread->flags |= THR_FLAGS_SUSPENDED;
80 while (curthread->flags & THR_FLAGS_NEED_SUSPEND) {
81 cycle = curthread->cycle;
82 THR_UNLOCK(curthread);
83 _thr_signal_unblock(curthread);
84 _thr_umtx_wait(&curthread->cycle, cycle, NULL, 0);
85 _thr_signal_block(curthread);
88 curthread->flags &= ~THR_FLAGS_SUSPENDED;
90 THR_UNLOCK(curthread);
91 _thr_signal_unblock(curthread);
95 _thr_signal_init(void)
99 /* Install cancel handler. */
100 SIGEMPTYSET(act.sa_mask);
101 act.sa_flags = SA_SIGINFO | SA_RESTART;
102 act.sa_sigaction = (__siginfohandler_t *)&sigcancel_handler;
103 __sys_sigaction(SIGCANCEL, &act, NULL);
107 _thr_signal_deinit(void)
111 __weak_reference(_sigaction, sigaction);
114 _sigaction(int sig, const struct sigaction * act, struct sigaction * oact)
116 /* Check if the signal number is out of range: */
117 if (sig < 1 || sig > _SIG_MAXSIG || sig == SIGCANCEL) {
118 /* Return an invalid argument: */
123 return __sys_sigaction(sig, act, oact);
126 __weak_reference(_sigprocmask, sigprocmask);
129 _sigprocmask(int how, const sigset_t *set, sigset_t *oset)
131 const sigset_t *p = set;
134 if (how != SIG_UNBLOCK) {
137 SIGDELSET(newset, SIGCANCEL);
141 return (__sys_sigprocmask(how, p, oset));
144 __weak_reference(_pthread_sigmask, pthread_sigmask);
147 _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
149 if (_sigprocmask(how, set, oset))
154 __weak_reference(_sigsuspend, sigsuspend);
157 _sigsuspend(const sigset_t * set)
159 struct pthread *curthread = tls_get_curthread();
161 const sigset_t *pset;
165 if (SIGISMEMBER(*set, SIGCANCEL)) {
167 SIGDELSET(newset, SIGCANCEL);
172 oldcancel = _thr_cancel_enter(curthread);
173 ret = __sys_sigsuspend(pset);
174 _thr_cancel_leave(curthread, oldcancel);
179 __weak_reference(__sigwait, sigwait);
180 __weak_reference(__sigtimedwait, sigtimedwait);
181 __weak_reference(__sigwaitinfo, sigwaitinfo);
184 __sigtimedwait(const sigset_t *set, siginfo_t *info,
185 const struct timespec * timeout)
187 struct pthread *curthread = tls_get_curthread();
189 const sigset_t *pset;
193 if (SIGISMEMBER(*set, SIGCANCEL)) {
195 SIGDELSET(newset, SIGCANCEL);
199 oldcancel = _thr_cancel_enter(curthread);
200 ret = __sys_sigtimedwait(pset, info, timeout);
201 _thr_cancel_leave(curthread, oldcancel);
206 __sigwaitinfo(const sigset_t *set, siginfo_t *info)
208 struct pthread *curthread = tls_get_curthread();
210 const sigset_t *pset;
214 if (SIGISMEMBER(*set, SIGCANCEL)) {
216 SIGDELSET(newset, SIGCANCEL);
221 oldcancel = _thr_cancel_enter(curthread);
222 ret = __sys_sigwaitinfo(pset, info);
223 _thr_cancel_leave(curthread, oldcancel);
228 __sigwait(const sigset_t *set, int *sig)
232 s = __sigwaitinfo(set, NULL);