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.6 2006/04/05 00:24:36 davidxu 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)
67 _thr_ast(struct pthread *curthread)
69 if (!THR_IN_CRITICAL(curthread)) {
70 if (__predict_false((curthread->flags &
71 (THR_FLAGS_NEED_SUSPEND | THR_FLAGS_SUSPENDED))
72 == THR_FLAGS_NEED_SUSPEND))
73 _thr_suspend_check(curthread);
78 _thr_suspend_check(struct pthread *curthread)
83 * Blocks SIGCANCEL which other threads must send.
85 _thr_signal_block(curthread);
88 * Increase critical_count, here we don't use THR_LOCK/UNLOCK
89 * because we are leaf code, we don't want to recursively call
92 curthread->critical_count++;
93 THR_UMTX_LOCK(curthread, &(curthread)->lock);
94 while ((curthread->flags & (THR_FLAGS_NEED_SUSPEND |
95 THR_FLAGS_SUSPENDED)) == THR_FLAGS_NEED_SUSPEND) {
97 cycle = curthread->cycle;
99 /* Wake the thread suspending us. */
100 _thr_umtx_wake(&curthread->cycle, INT_MAX);
103 * if we are from pthread_exit, we don't want to
104 * suspend, just go and die.
106 if (curthread->state == PS_DEAD)
108 curthread->flags |= THR_FLAGS_SUSPENDED;
109 THR_UMTX_UNLOCK(curthread, &(curthread)->lock);
110 _thr_umtx_wait(&curthread->cycle, cycle, NULL, 0);
111 THR_UMTX_LOCK(curthread, &(curthread)->lock);
112 curthread->flags &= ~THR_FLAGS_SUSPENDED;
114 THR_UMTX_UNLOCK(curthread, &(curthread)->lock);
115 curthread->critical_count--;
117 _thr_signal_unblock(curthread);
121 _thr_signal_init(void)
123 struct sigaction act;
125 /* Install cancel handler. */
126 SIGEMPTYSET(act.sa_mask);
127 act.sa_flags = SA_SIGINFO | SA_RESTART;
128 act.sa_sigaction = (__siginfohandler_t *)&sigcancel_handler;
129 __sys_sigaction(SIGCANCEL, &act, NULL);
133 _thr_signal_deinit(void)
138 _sigaction(int sig, const struct sigaction * act, struct sigaction * oact)
140 /* Check if the signal number is out of range: */
141 if (sig < 1 || sig > _SIG_MAXSIG || sig == SIGCANCEL) {
142 /* Return an invalid argument: */
147 return __sys_sigaction(sig, act, oact);
150 __strong_reference(_sigaction, sigaction);
153 _sigprocmask(int how, const sigset_t *set, sigset_t *oset)
155 const sigset_t *p = set;
158 if (how != SIG_UNBLOCK) {
161 SIGDELSET(newset, SIGCANCEL);
165 return (__sys_sigprocmask(how, p, oset));
168 __strong_reference(_sigprocmask, sigprocmask);
171 _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
173 if (_sigprocmask(how, set, oset))
178 __strong_reference(_pthread_sigmask, pthread_sigmask);
181 _sigsuspend(const sigset_t * set)
183 struct pthread *curthread = tls_get_curthread();
185 const sigset_t *pset;
189 if (SIGISMEMBER(*set, SIGCANCEL)) {
191 SIGDELSET(newset, SIGCANCEL);
196 oldcancel = _thr_cancel_enter(curthread);
197 ret = __sys_sigsuspend(pset);
198 _thr_cancel_leave(curthread, oldcancel);
203 __strong_reference(_sigsuspend, sigsuspend);
206 __sigtimedwait(const sigset_t *set, siginfo_t *info,
207 const struct timespec * timeout)
209 struct pthread *curthread = tls_get_curthread();
211 const sigset_t *pset;
215 if (SIGISMEMBER(*set, SIGCANCEL)) {
217 SIGDELSET(newset, SIGCANCEL);
221 oldcancel = _thr_cancel_enter(curthread);
222 ret = __sys_sigtimedwait(pset, info, timeout);
223 _thr_cancel_leave(curthread, oldcancel);
227 __strong_reference(__sigtimedwait, sigtimedwait);
230 __sigwaitinfo(const sigset_t *set, siginfo_t *info)
232 struct pthread *curthread = tls_get_curthread();
234 const sigset_t *pset;
238 if (SIGISMEMBER(*set, SIGCANCEL)) {
240 SIGDELSET(newset, SIGCANCEL);
245 oldcancel = _thr_cancel_enter(curthread);
246 ret = __sys_sigwaitinfo(pset, info);
247 _thr_cancel_leave(curthread, oldcancel);
251 __strong_reference(__sigwaitinfo, sigwaitinfo);
254 __sigwait(const sigset_t *set, int *sig)
258 s = __sigwaitinfo(set, NULL);
266 __strong_reference(__sigwait, sigwait);