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.7 2006/04/06 13:03:09 davidxu Exp $
35 #include "namespace.h"
36 #include <sys/signalvar.h>
38 #include <machine/tls.h>
44 #include "un-namespace.h"
46 #include "thr_private.h"
48 /* #define DEBUG_SIGNAL */
50 #define DBG_MSG stdout_debug
55 int __sigwait(const sigset_t
*set
, int *sig
);
56 int __sigwaitinfo(const sigset_t
*set
, siginfo_t
*info
);
57 int __sigtimedwait(const sigset_t
*set
, siginfo_t
*info
,
58 const struct timespec
* timeout
);
61 sigcancel_handler(int sig __unused
, siginfo_t
*info __unused
,
62 ucontext_t
*ucp __unused
)
64 struct pthread
*curthread
= tls_get_curthread();
66 if (curthread
->cancelflags
& THR_CANCEL_AT_POINT
)
67 _pthread_testcancel();
72 _thr_ast(struct pthread
*curthread
)
74 if (!THR_IN_CRITICAL(curthread
)) {
75 if (__predict_false((curthread
->flags
&
76 (THR_FLAGS_NEED_SUSPEND
| THR_FLAGS_SUSPENDED
))
77 == THR_FLAGS_NEED_SUSPEND
))
78 _thr_suspend_check(curthread
);
83 _thr_suspend_check(struct pthread
*curthread
)
88 * Blocks SIGCANCEL which other threads must send.
90 _thr_signal_block(curthread
);
93 * Increase critical_count, here we don't use THR_LOCK/UNLOCK
94 * because we are leaf code, we don't want to recursively call
97 curthread
->critical_count
++;
98 THR_UMTX_LOCK(curthread
, &(curthread
)->lock
);
99 while ((curthread
->flags
& (THR_FLAGS_NEED_SUSPEND
|
100 THR_FLAGS_SUSPENDED
)) == THR_FLAGS_NEED_SUSPEND
) {
102 cycle
= curthread
->cycle
;
104 /* Wake the thread suspending us. */
105 _thr_umtx_wake(&curthread
->cycle
, INT_MAX
);
108 * if we are from pthread_exit, we don't want to
109 * suspend, just go and die.
111 if (curthread
->state
== PS_DEAD
)
113 curthread
->flags
|= THR_FLAGS_SUSPENDED
;
114 THR_UMTX_UNLOCK(curthread
, &(curthread
)->lock
);
115 _thr_umtx_wait(&curthread
->cycle
, cycle
, NULL
, 0);
116 THR_UMTX_LOCK(curthread
, &(curthread
)->lock
);
117 curthread
->flags
&= ~THR_FLAGS_SUSPENDED
;
119 THR_UMTX_UNLOCK(curthread
, &(curthread
)->lock
);
120 curthread
->critical_count
--;
122 _thr_signal_unblock(curthread
);
126 _thr_signal_init(void)
128 struct sigaction act
;
130 /* Install cancel handler. */
131 SIGEMPTYSET(act
.sa_mask
);
132 act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
133 act
.sa_sigaction
= (__siginfohandler_t
*)&sigcancel_handler
;
134 __sys_sigaction(SIGCANCEL
, &act
, NULL
);
138 _thr_signal_deinit(void)
143 _sigaction(int sig
, const struct sigaction
* act
, struct sigaction
* oact
)
145 /* Check if the signal number is out of range: */
146 if (sig
< 1 || sig
> _SIG_MAXSIG
|| sig
== SIGCANCEL
) {
147 /* Return an invalid argument: */
152 return __sys_sigaction(sig
, act
, oact
);
155 __strong_reference(_sigaction
, sigaction
);
158 _sigprocmask(int how
, const sigset_t
*set
, sigset_t
*oset
)
160 const sigset_t
*p
= set
;
163 if (how
!= SIG_UNBLOCK
) {
166 SIGDELSET(newset
, SIGCANCEL
);
170 return (__sys_sigprocmask(how
, p
, oset
));
173 __strong_reference(_sigprocmask
, sigprocmask
);
176 _pthread_sigmask(int how
, const sigset_t
*set
, sigset_t
*oset
)
178 if (_sigprocmask(how
, set
, oset
))
183 __strong_reference(_pthread_sigmask
, pthread_sigmask
);
186 _sigsuspend(const sigset_t
* set
)
188 struct pthread
*curthread
= tls_get_curthread();
190 const sigset_t
*pset
;
194 if (SIGISMEMBER(*set
, SIGCANCEL
)) {
196 SIGDELSET(newset
, SIGCANCEL
);
201 oldcancel
= _thr_cancel_enter(curthread
);
202 ret
= __sys_sigsuspend(pset
);
203 _thr_cancel_leave(curthread
, oldcancel
);
208 __strong_reference(_sigsuspend
, sigsuspend
);
211 __sigtimedwait(const sigset_t
*set
, siginfo_t
*info
,
212 const struct timespec
* timeout
)
214 struct pthread
*curthread
= tls_get_curthread();
216 const sigset_t
*pset
;
220 if (SIGISMEMBER(*set
, SIGCANCEL
)) {
222 SIGDELSET(newset
, SIGCANCEL
);
226 oldcancel
= _thr_cancel_enter(curthread
);
227 ret
= __sys_sigtimedwait(pset
, info
, timeout
);
228 _thr_cancel_leave(curthread
, oldcancel
);
232 __strong_reference(__sigtimedwait
, sigtimedwait
);
235 __sigwaitinfo(const sigset_t
*set
, siginfo_t
*info
)
237 struct pthread
*curthread
= tls_get_curthread();
239 const sigset_t
*pset
;
243 if (SIGISMEMBER(*set
, SIGCANCEL
)) {
245 SIGDELSET(newset
, SIGCANCEL
);
250 oldcancel
= _thr_cancel_enter(curthread
);
251 ret
= __sys_sigwaitinfo(pset
, info
);
252 _thr_cancel_leave(curthread
, oldcancel
);
256 __strong_reference(__sigwaitinfo
, sigwaitinfo
);
259 __sigwait(const sigset_t
*set
, int *sig
)
263 s
= __sigwaitinfo(set
, NULL
);
271 __strong_reference(__sigwait
, sigwait
);