Make thread suspension really work.
[dragonfly.git] / lib / libthread_xu / thread / thr_sig.c
CommitLineData
71b3fa15
DX
1/*
2 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
3 * All rights reserved.
4 *
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.
19 *
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.
31 *
fd4288bd 32 * $DragonFly: src/lib/libthread_xu/thread/thr_sig.c,v 1.5 2006/03/19 13:07:12 davidxu Exp $
71b3fa15
DX
33 */
34
35#include <sys/param.h>
71b3fa15 36#include <sys/signalvar.h>
9e2ee207
JS
37
38#include <machine/tls.h>
39
71b3fa15
DX
40#include <signal.h>
41#include <errno.h>
42#include <fcntl.h>
43#include <unistd.h>
44#include <string.h>
45#include <pthread.h>
46
47#include "thr_private.h"
48
49/* #define DEBUG_SIGNAL */
50#ifdef DEBUG_SIGNAL
51#define DBG_MSG stdout_debug
52#else
53#define DBG_MSG(x...)
54#endif
55
56static void
57sigcancel_handler(int sig, siginfo_t *info, ucontext_t *ucp)
58{
9e2ee207 59 struct pthread *curthread = tls_get_curthread();
71b3fa15
DX
60
61 if (curthread->cancelflags & THR_CANCEL_AT_POINT)
62 pthread_testcancel();
fd4288bd
DX
63 _thr_ast(curthread);
64}
65
66void
67_thr_ast(struct pthread *curthread)
68{
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);
71b3fa15
DX
74 }
75}
76
77void
78_thr_suspend_check(struct pthread *curthread)
79{
fd4288bd 80 umtx_t cycle;
71b3fa15 81
fd4288bd
DX
82 /*
83 * Blocks SIGCANCEL which other threads must send.
84 */
71b3fa15 85 _thr_signal_block(curthread);
fd4288bd
DX
86
87 /*
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
90 * ourself.
91 */
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) {
96 curthread->cycle++;
97 cycle = curthread->cycle;
98
99 /* Wake the thread suspending us. */
100 _thr_umtx_wake(&curthread->cycle, INT_MAX);
101
102 /*
103 * if we are from pthread_exit, we don't want to
104 * suspend, just go and die.
105 */
106 if (curthread->state == PS_DEAD)
107 break;
71b3fa15 108 curthread->flags |= THR_FLAGS_SUSPENDED;
fd4288bd
DX
109 THR_UMTX_UNLOCK(curthread, &(curthread)->lock);
110 _thr_umtx_wait(&curthread->cycle, cycle, NULL, 0);
111 THR_UMTX_LOCK(curthread, &(curthread)->lock);
71b3fa15
DX
112 curthread->flags &= ~THR_FLAGS_SUSPENDED;
113 }
fd4288bd
DX
114 THR_UMTX_UNLOCK(curthread, &(curthread)->lock);
115 curthread->critical_count--;
116
71b3fa15
DX
117 _thr_signal_unblock(curthread);
118}
119
120void
121_thr_signal_init(void)
122{
123 struct sigaction act;
124
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);
130}
131
132void
133_thr_signal_deinit(void)
134{
135}
136
137__weak_reference(_sigaction, sigaction);
138
139int
140_sigaction(int sig, const struct sigaction * act, struct sigaction * oact)
141{
142 /* Check if the signal number is out of range: */
143 if (sig < 1 || sig > _SIG_MAXSIG || sig == SIGCANCEL) {
144 /* Return an invalid argument: */
145 errno = EINVAL;
146 return (-1);
147 }
148
149 return __sys_sigaction(sig, act, oact);
150}
151
152__weak_reference(_sigprocmask, sigprocmask);
153
154int
155_sigprocmask(int how, const sigset_t *set, sigset_t *oset)
156{
157 const sigset_t *p = set;
158 sigset_t newset;
159
160 if (how != SIG_UNBLOCK) {
161 if (set != NULL) {
162 newset = *set;
163 SIGDELSET(newset, SIGCANCEL);
164 p = &newset;
165 }
166 }
167 return (__sys_sigprocmask(how, p, oset));
168}
169
170__weak_reference(_pthread_sigmask, pthread_sigmask);
171
172int
173_pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
174{
175 if (_sigprocmask(how, set, oset))
176 return (errno);
177 return (0);
178}
179
180__weak_reference(_sigsuspend, sigsuspend);
181
182int
183_sigsuspend(const sigset_t * set)
184{
9e2ee207 185 struct pthread *curthread = tls_get_curthread();
71b3fa15
DX
186 sigset_t newset;
187 const sigset_t *pset;
188 int oldcancel;
189 int ret;
190
191 if (SIGISMEMBER(*set, SIGCANCEL)) {
192 newset = *set;
193 SIGDELSET(newset, SIGCANCEL);
194 pset = &newset;
195 } else
196 pset = set;
197
198 oldcancel = _thr_cancel_enter(curthread);
199 ret = __sys_sigsuspend(pset);
200 _thr_cancel_leave(curthread, oldcancel);
201
202 return (ret);
203}
204
71b3fa15
DX
205__weak_reference(__sigwait, sigwait);
206__weak_reference(__sigtimedwait, sigtimedwait);
207__weak_reference(__sigwaitinfo, sigwaitinfo);
208
209int
210__sigtimedwait(const sigset_t *set, siginfo_t *info,
211 const struct timespec * timeout)
212{
9e2ee207 213 struct pthread *curthread = tls_get_curthread();
71b3fa15
DX
214 sigset_t newset;
215 const sigset_t *pset;
216 int oldcancel;
217 int ret;
218
219 if (SIGISMEMBER(*set, SIGCANCEL)) {
220 newset = *set;
221 SIGDELSET(newset, SIGCANCEL);
222 pset = &newset;
223 } else
224 pset = set;
225 oldcancel = _thr_cancel_enter(curthread);
226 ret = __sys_sigtimedwait(pset, info, timeout);
227 _thr_cancel_leave(curthread, oldcancel);
228 return (ret);
229}
230
231int
232__sigwaitinfo(const sigset_t *set, siginfo_t *info)
233{
9e2ee207 234 struct pthread *curthread = tls_get_curthread();
71b3fa15
DX
235 sigset_t newset;
236 const sigset_t *pset;
237 int oldcancel;
238 int ret;
239
240 if (SIGISMEMBER(*set, SIGCANCEL)) {
241 newset = *set;
242 SIGDELSET(newset, SIGCANCEL);
243 pset = &newset;
244 } else
245 pset = set;
246
247 oldcancel = _thr_cancel_enter(curthread);
248 ret = __sys_sigwaitinfo(pset, info);
249 _thr_cancel_leave(curthread, oldcancel);
250 return (ret);
251}
252
253int
254__sigwait(const sigset_t *set, int *sig)
255{
7aca7007 256 int s;
71b3fa15 257
7aca7007
DX
258 s = __sigwaitinfo(set, NULL);
259 if (s > 0) {
260 *sig = s;
261 return (0);
262 }
263 return (errno);
71b3fa15 264}