Merge from vendor branch BSDTAR:
[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.4 2005/03/29 19:26:20 joerg 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         if (curthread->flags & THR_FLAGS_NEED_SUSPEND) {
64                 __sys_sigprocmask(SIG_SETMASK, &ucp->uc_sigmask, NULL);
65                 _thr_suspend_check(curthread);
66         }
67 }
68
69 void
70 _thr_suspend_check(struct pthread *curthread)
71 {
72         long cycle;
73
74         /* Async suspend. */
75         _thr_signal_block(curthread);
76         THR_LOCK(curthread);
77         if ((curthread->flags & (THR_FLAGS_NEED_SUSPEND | THR_FLAGS_SUSPENDED))
78                 == THR_FLAGS_NEED_SUSPEND) {
79                 curthread->flags |= THR_FLAGS_SUSPENDED;
80                 while (curthread->flags & THR_FLAGS_NEED_SUSPEND) {
81                         cycle = curthread->cycle;
82                         THR_UNLOCK(curthread);
83                         _thr_signal_unblock(curthread);
84                         _thr_umtx_wait(&curthread->cycle, cycle, NULL, 0);
85                         _thr_signal_block(curthread);
86                         THR_LOCK(curthread);
87                 }
88                 curthread->flags &= ~THR_FLAGS_SUSPENDED;
89         }
90         THR_UNLOCK(curthread);
91         _thr_signal_unblock(curthread);
92 }
93
94 void
95 _thr_signal_init(void)
96 {
97         struct sigaction act;
98
99         /* Install cancel handler. */
100         SIGEMPTYSET(act.sa_mask);
101         act.sa_flags = SA_SIGINFO | SA_RESTART;
102         act.sa_sigaction = (__siginfohandler_t *)&sigcancel_handler;
103         __sys_sigaction(SIGCANCEL, &act, NULL);
104 }
105
106 void
107 _thr_signal_deinit(void)
108 {
109 }
110
111 __weak_reference(_sigaction, sigaction);
112
113 int
114 _sigaction(int sig, const struct sigaction * act, struct sigaction * oact)
115 {
116         /* Check if the signal number is out of range: */
117         if (sig < 1 || sig > _SIG_MAXSIG || sig == SIGCANCEL) {
118                 /* Return an invalid argument: */
119                 errno = EINVAL;
120                 return (-1);
121         }
122
123         return __sys_sigaction(sig, act, oact);
124 }
125
126 __weak_reference(_sigprocmask, sigprocmask);
127
128 int
129 _sigprocmask(int how, const sigset_t *set, sigset_t *oset)
130 {
131         const sigset_t *p = set;
132         sigset_t newset;
133
134         if (how != SIG_UNBLOCK) {
135                 if (set != NULL) {
136                         newset = *set;
137                         SIGDELSET(newset, SIGCANCEL);
138                         p = &newset;
139                 }
140         }
141         return (__sys_sigprocmask(how, p, oset));
142 }
143
144 __weak_reference(_pthread_sigmask, pthread_sigmask);
145
146 int
147 _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
148 {
149         if (_sigprocmask(how, set, oset))
150                 return (errno);
151         return (0);
152 }
153
154 __weak_reference(_sigsuspend, sigsuspend);
155
156 int
157 _sigsuspend(const sigset_t * set)
158 {
159         struct pthread *curthread = tls_get_curthread();
160         sigset_t newset;
161         const sigset_t *pset;
162         int oldcancel;
163         int ret;
164
165         if (SIGISMEMBER(*set, SIGCANCEL)) {
166                 newset = *set;
167                 SIGDELSET(newset, SIGCANCEL);
168                 pset = &newset;
169         } else
170                 pset = set;
171
172         oldcancel = _thr_cancel_enter(curthread);
173         ret = __sys_sigsuspend(pset);
174         _thr_cancel_leave(curthread, oldcancel);
175
176         return (ret);
177 }
178
179 __weak_reference(__sigwait, sigwait);
180 __weak_reference(__sigtimedwait, sigtimedwait);
181 __weak_reference(__sigwaitinfo, sigwaitinfo);
182
183 int
184 __sigtimedwait(const sigset_t *set, siginfo_t *info,
185         const struct timespec * timeout)
186 {
187         struct pthread  *curthread = tls_get_curthread();
188         sigset_t newset;
189         const sigset_t *pset;
190         int oldcancel;
191         int ret;
192
193         if (SIGISMEMBER(*set, SIGCANCEL)) {
194                 newset = *set;
195                 SIGDELSET(newset, SIGCANCEL);
196                 pset = &newset;
197         } else
198                 pset = set;
199         oldcancel = _thr_cancel_enter(curthread);
200         ret = __sys_sigtimedwait(pset, info, timeout);
201         _thr_cancel_leave(curthread, oldcancel);
202         return (ret);
203 }
204
205 int
206 __sigwaitinfo(const sigset_t *set, siginfo_t *info)
207 {
208         struct pthread  *curthread = tls_get_curthread();
209         sigset_t newset;
210         const sigset_t *pset;
211         int oldcancel;
212         int ret;
213
214         if (SIGISMEMBER(*set, SIGCANCEL)) {
215                 newset = *set;
216                 SIGDELSET(newset, SIGCANCEL);
217                 pset = &newset;
218         } else
219                 pset = set;
220
221         oldcancel = _thr_cancel_enter(curthread);
222         ret = __sys_sigwaitinfo(pset, info);
223         _thr_cancel_leave(curthread, oldcancel);
224         return (ret);
225 }
226
227 int
228 __sigwait(const sigset_t *set, int *sig)
229 {
230         int s;
231
232         s = __sigwaitinfo(set, NULL);
233         if (s > 0) {
234                 *sig = s;
235                 return (0);
236         }
237         return (errno);
238 }