dma: beautify queue listing output
[dragonfly.git] / lib / libthread_xu / thread / thr_sig.c
blob78498679806f54058732f085a25020482fab3bb1
1 /*
2 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
30 * SUCH DAMAGE.
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>
40 #include <signal.h>
41 #include <errno.h>
42 #include <string.h>
43 #include <pthread.h>
44 #include "un-namespace.h"
46 #include "thr_private.h"
48 /* #define DEBUG_SIGNAL */
49 #ifdef DEBUG_SIGNAL
50 #define DBG_MSG stdout_debug
51 #else
52 #define DBG_MSG(x...)
53 #endif
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);
60 static void
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();
68 _thr_ast(curthread);
71 void
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);
82 void
83 _thr_suspend_check(struct pthread *curthread)
85 umtx_t cycle;
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
95 * ourself.
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) {
101 curthread->cycle++;
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)
112 break;
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);
125 void
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);
137 void
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: */
148 errno = EINVAL;
149 return (-1);
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;
161 sigset_t newset;
163 if (how != SIG_UNBLOCK) {
164 if (set != NULL) {
165 newset = *set;
166 SIGDELSET(newset, SIGCANCEL);
167 p = &newset;
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))
179 return (errno);
180 return (0);
183 __strong_reference(_pthread_sigmask, pthread_sigmask);
186 _sigsuspend(const sigset_t * set)
188 struct pthread *curthread = tls_get_curthread();
189 sigset_t newset;
190 const sigset_t *pset;
191 int oldcancel;
192 int ret;
194 if (SIGISMEMBER(*set, SIGCANCEL)) {
195 newset = *set;
196 SIGDELSET(newset, SIGCANCEL);
197 pset = &newset;
198 } else
199 pset = set;
201 oldcancel = _thr_cancel_enter(curthread);
202 ret = __sys_sigsuspend(pset);
203 _thr_cancel_leave(curthread, oldcancel);
205 return (ret);
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();
215 sigset_t newset;
216 const sigset_t *pset;
217 int oldcancel;
218 int ret;
220 if (SIGISMEMBER(*set, SIGCANCEL)) {
221 newset = *set;
222 SIGDELSET(newset, SIGCANCEL);
223 pset = &newset;
224 } else
225 pset = set;
226 oldcancel = _thr_cancel_enter(curthread);
227 ret = __sys_sigtimedwait(pset, info, timeout);
228 _thr_cancel_leave(curthread, oldcancel);
229 return (ret);
232 __strong_reference(__sigtimedwait, sigtimedwait);
235 __sigwaitinfo(const sigset_t *set, siginfo_t *info)
237 struct pthread *curthread = tls_get_curthread();
238 sigset_t newset;
239 const sigset_t *pset;
240 int oldcancel;
241 int ret;
243 if (SIGISMEMBER(*set, SIGCANCEL)) {
244 newset = *set;
245 SIGDELSET(newset, SIGCANCEL);
246 pset = &newset;
247 } else
248 pset = set;
250 oldcancel = _thr_cancel_enter(curthread);
251 ret = __sys_sigwaitinfo(pset, info);
252 _thr_cancel_leave(curthread, oldcancel);
253 return (ret);
256 __strong_reference(__sigwaitinfo, sigwaitinfo);
259 __sigwait(const sigset_t *set, int *sig)
261 int s;
263 s = __sigwaitinfo(set, NULL);
264 if (s > 0) {
265 *sig = s;
266 return (0);
268 return (errno);
271 __strong_reference(__sigwait, sigwait);