e13f91ddf6571a934ad45ff76a23c4982f04f4d6
[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.1 2005/02/01 12:38:27 davidxu Exp $
33  */
34
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/signalvar.h>
38 #include <signal.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <unistd.h>
42 #include <string.h>
43 #include <pthread.h>
44
45 #include "thr_private.h"
46
47 /* #define DEBUG_SIGNAL */
48 #ifdef DEBUG_SIGNAL
49 #define DBG_MSG         stdout_debug
50 #else
51 #define DBG_MSG(x...)
52 #endif
53
54 static void
55 sigcancel_handler(int sig, siginfo_t *info, ucontext_t *ucp)
56 {
57         struct pthread *curthread = _get_curthread();
58
59         if (curthread->cancelflags & THR_CANCEL_AT_POINT)
60                 pthread_testcancel();
61         if (curthread->flags & THR_FLAGS_NEED_SUSPEND) {
62                 __sys_sigprocmask(SIG_SETMASK, &ucp->uc_sigmask, NULL);
63                 _thr_suspend_check(curthread);
64         }
65 }
66
67 void
68 _thr_suspend_check(struct pthread *curthread)
69 {
70         long cycle;
71
72         /* Async suspend. */
73         _thr_signal_block(curthread);
74         THR_LOCK(curthread);
75         if ((curthread->flags & (THR_FLAGS_NEED_SUSPEND | THR_FLAGS_SUSPENDED))
76                 == THR_FLAGS_NEED_SUSPEND) {
77                 curthread->flags |= THR_FLAGS_SUSPENDED;
78                 while (curthread->flags & THR_FLAGS_NEED_SUSPEND) {
79                         cycle = curthread->cycle;
80                         THR_UNLOCK(curthread);
81                         _thr_signal_unblock(curthread);
82                         _thr_umtx_wait(&curthread->cycle, cycle, NULL);
83                         _thr_signal_block(curthread);
84                         THR_LOCK(curthread);
85                 }
86                 curthread->flags &= ~THR_FLAGS_SUSPENDED;
87         }
88         THR_UNLOCK(curthread);
89         _thr_signal_unblock(curthread);
90 }
91
92 void
93 _thr_signal_init(void)
94 {
95         struct sigaction act;
96
97         /* Install cancel handler. */
98         SIGEMPTYSET(act.sa_mask);
99         act.sa_flags = SA_SIGINFO | SA_RESTART;
100         act.sa_sigaction = (__siginfohandler_t *)&sigcancel_handler;
101         __sys_sigaction(SIGCANCEL, &act, NULL);
102 }
103
104 void
105 _thr_signal_deinit(void)
106 {
107 }
108
109 __weak_reference(_sigaction, sigaction);
110
111 int
112 _sigaction(int sig, const struct sigaction * act, struct sigaction * oact)
113 {
114         /* Check if the signal number is out of range: */
115         if (sig < 1 || sig > _SIG_MAXSIG || sig == SIGCANCEL) {
116                 /* Return an invalid argument: */
117                 errno = EINVAL;
118                 return (-1);
119         }
120
121         return __sys_sigaction(sig, act, oact);
122 }
123
124 __weak_reference(_sigprocmask, sigprocmask);
125
126 int
127 _sigprocmask(int how, const sigset_t *set, sigset_t *oset)
128 {
129         const sigset_t *p = set;
130         sigset_t newset;
131
132         if (how != SIG_UNBLOCK) {
133                 if (set != NULL) {
134                         newset = *set;
135                         SIGDELSET(newset, SIGCANCEL);
136                         p = &newset;
137                 }
138         }
139         return (__sys_sigprocmask(how, p, oset));
140 }
141
142 __weak_reference(_pthread_sigmask, pthread_sigmask);
143
144 int
145 _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
146 {
147         if (_sigprocmask(how, set, oset))
148                 return (errno);
149         return (0);
150 }
151
152 __weak_reference(_sigsuspend, sigsuspend);
153
154 int
155 _sigsuspend(const sigset_t * set)
156 {
157         struct pthread *curthread = _get_curthread();
158         sigset_t newset;
159         const sigset_t *pset;
160         int oldcancel;
161         int ret;
162
163         if (SIGISMEMBER(*set, SIGCANCEL)) {
164                 newset = *set;
165                 SIGDELSET(newset, SIGCANCEL);
166                 pset = &newset;
167         } else
168                 pset = set;
169
170         oldcancel = _thr_cancel_enter(curthread);
171         ret = __sys_sigsuspend(pset);
172         _thr_cancel_leave(curthread, oldcancel);
173
174         return (ret);
175 }
176
177 #if 0
178 __weak_reference(__sigwait, sigwait);
179 __weak_reference(__sigtimedwait, sigtimedwait);
180 __weak_reference(__sigwaitinfo, sigwaitinfo);
181
182 int
183 __sigtimedwait(const sigset_t *set, siginfo_t *info,
184         const struct timespec * timeout)
185 {
186         struct pthread  *curthread = _get_curthread();
187         sigset_t newset;
188         const sigset_t *pset;
189         int oldcancel;
190         int ret;
191
192         if (SIGISMEMBER(*set, SIGCANCEL)) {
193                 newset = *set;
194                 SIGDELSET(newset, SIGCANCEL);
195                 pset = &newset;
196         } else
197                 pset = set;
198         oldcancel = _thr_cancel_enter(curthread);
199         ret = __sys_sigtimedwait(pset, info, timeout);
200         _thr_cancel_leave(curthread, oldcancel);
201         return (ret);
202 }
203
204 int
205 __sigwaitinfo(const sigset_t *set, siginfo_t *info)
206 {
207         struct pthread  *curthread = _get_curthread();
208         sigset_t newset;
209         const sigset_t *pset;
210         int oldcancel;
211         int ret;
212
213         if (SIGISMEMBER(*set, SIGCANCEL)) {
214                 newset = *set;
215                 SIGDELSET(newset, SIGCANCEL);
216                 pset = &newset;
217         } else
218                 pset = set;
219
220         oldcancel = _thr_cancel_enter(curthread);
221         ret = __sys_sigwaitinfo(pset, info);
222         _thr_cancel_leave(curthread, oldcancel);
223         return (ret);
224 }
225
226 int
227 __sigwait(const sigset_t *set, int *sig)
228 {
229         struct pthread  *curthread = _get_curthread();
230         sigset_t newset;
231         const sigset_t *pset;
232         int oldcancel;
233         int ret;
234
235         if (SIGISMEMBER(*set, SIGCANCEL)) {
236                 newset = *set;
237                 SIGDELSET(newset, SIGCANCEL);
238                 pset = &newset;
239         } else 
240                 pset = set;
241
242         oldcancel = _thr_cancel_enter(curthread);
243         ret = __sys_sigwait(pset, sig);
244         _thr_cancel_leave(curthread, oldcancel);
245         return (ret);
246 }
247 #endif