Convert weak reference to strong reference so that static library
[dragonfly.git] / lib / libthread_xu / thread / thr_sig.c
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  *
32  * $DragonFly: src/lib/libthread_xu/thread/thr_sig.c,v 1.6 2006/04/05 00:24:36 davidxu Exp $
33  */
34
35 #include <sys/param.h>
36 #include <sys/signalvar.h>
37
38 #include <machine/tls.h>
39
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
56 static void
57 sigcancel_handler(int sig, siginfo_t *info, ucontext_t *ucp)
58 {
59         struct pthread *curthread = tls_get_curthread();
60
61         if (curthread->cancelflags & THR_CANCEL_AT_POINT)
62                 pthread_testcancel();
63         _thr_ast(curthread);
64 }
65
66 void
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);
74         }
75 }
76
77 void
78 _thr_suspend_check(struct pthread *curthread)
79 {
80         umtx_t cycle;
81
82         /*
83          * Blocks SIGCANCEL which other threads must send.
84          */
85         _thr_signal_block(curthread);
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;
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;
113         }
114         THR_UMTX_UNLOCK(curthread, &(curthread)->lock);
115         curthread->critical_count--;
116
117         _thr_signal_unblock(curthread);
118 }
119
120 void
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
132 void
133 _thr_signal_deinit(void)
134 {
135 }
136
137 int
138 _sigaction(int sig, const struct sigaction * act, struct sigaction * oact)
139 {
140         /* Check if the signal number is out of range: */
141         if (sig < 1 || sig > _SIG_MAXSIG || sig == SIGCANCEL) {
142                 /* Return an invalid argument: */
143                 errno = EINVAL;
144                 return (-1);
145         }
146
147         return __sys_sigaction(sig, act, oact);
148 }
149
150 __strong_reference(_sigaction, sigaction);
151
152 int
153 _sigprocmask(int how, const sigset_t *set, sigset_t *oset)
154 {
155         const sigset_t *p = set;
156         sigset_t newset;
157
158         if (how != SIG_UNBLOCK) {
159                 if (set != NULL) {
160                         newset = *set;
161                         SIGDELSET(newset, SIGCANCEL);
162                         p = &newset;
163                 }
164         }
165         return (__sys_sigprocmask(how, p, oset));
166 }
167
168 __strong_reference(_sigprocmask, sigprocmask);
169
170 int
171 _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
172 {
173         if (_sigprocmask(how, set, oset))
174                 return (errno);
175         return (0);
176 }
177
178 __strong_reference(_pthread_sigmask, pthread_sigmask);
179
180 int
181 _sigsuspend(const sigset_t * set)
182 {
183         struct pthread *curthread = tls_get_curthread();
184         sigset_t newset;
185         const sigset_t *pset;
186         int oldcancel;
187         int ret;
188
189         if (SIGISMEMBER(*set, SIGCANCEL)) {
190                 newset = *set;
191                 SIGDELSET(newset, SIGCANCEL);
192                 pset = &newset;
193         } else
194                 pset = set;
195
196         oldcancel = _thr_cancel_enter(curthread);
197         ret = __sys_sigsuspend(pset);
198         _thr_cancel_leave(curthread, oldcancel);
199
200         return (ret);
201 }
202
203 __strong_reference(_sigsuspend, sigsuspend);
204
205 int
206 __sigtimedwait(const sigset_t *set, siginfo_t *info,
207         const struct timespec * timeout)
208 {
209         struct pthread  *curthread = tls_get_curthread();
210         sigset_t newset;
211         const sigset_t *pset;
212         int oldcancel;
213         int ret;
214
215         if (SIGISMEMBER(*set, SIGCANCEL)) {
216                 newset = *set;
217                 SIGDELSET(newset, SIGCANCEL);
218                 pset = &newset;
219         } else
220                 pset = set;
221         oldcancel = _thr_cancel_enter(curthread);
222         ret = __sys_sigtimedwait(pset, info, timeout);
223         _thr_cancel_leave(curthread, oldcancel);
224         return (ret);
225 }
226
227 __strong_reference(__sigtimedwait, sigtimedwait);
228
229 int
230 __sigwaitinfo(const sigset_t *set, siginfo_t *info)
231 {
232         struct pthread  *curthread = tls_get_curthread();
233         sigset_t newset;
234         const sigset_t *pset;
235         int oldcancel;
236         int ret;
237
238         if (SIGISMEMBER(*set, SIGCANCEL)) {
239                 newset = *set;
240                 SIGDELSET(newset, SIGCANCEL);
241                 pset = &newset;
242         } else
243                 pset = set;
244
245         oldcancel = _thr_cancel_enter(curthread);
246         ret = __sys_sigwaitinfo(pset, info);
247         _thr_cancel_leave(curthread, oldcancel);
248         return (ret);
249 }
250
251 __strong_reference(__sigwaitinfo, sigwaitinfo);
252
253 int
254 __sigwait(const sigset_t *set, int *sig)
255 {
256         int s;
257
258         s = __sigwaitinfo(set, NULL);
259         if (s > 0) {
260                 *sig = s;
261                 return (0);
262         }
263         return (errno);
264 }
265
266 __strong_reference(__sigwait, sigwait);
267