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.7 2006/04/06 13:03:09 davidxu Exp $ 33 */ 34 35 #include "namespace.h" 36 #include <sys/signalvar.h> 37 38 #include <machine/tls.h> 39 40 #include <signal.h> 41 #include <errno.h> 42 #include <string.h> 43 #include <pthread.h> 44 #include "un-namespace.h" 45 46 #include "thr_private.h" 47 48 /* #define DEBUG_SIGNAL */ 49 #ifdef DEBUG_SIGNAL 50 #define DBG_MSG stdout_debug 51 #else 52 #define DBG_MSG(x...) 53 #endif 54 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); 59 60 static void 61 sigcancel_handler(int sig __unused, siginfo_t *info __unused, 62 ucontext_t *ucp __unused) 63 { 64 struct pthread *curthread = tls_get_curthread(); 65 66 if (curthread->cancelflags & THR_CANCEL_AT_POINT) 67 _pthread_testcancel(); 68 _thr_ast(curthread); 69 } 70 71 void 72 _thr_ast(struct pthread *curthread) 73 { 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); 79 } 80 } 81 82 void 83 _thr_suspend_check(struct pthread *curthread) 84 { 85 umtx_t cycle; 86 87 /* 88 * Blocks SIGCANCEL which other threads must send. 89 */ 90 _thr_signal_block(curthread); 91 92 /* 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. 96 */ 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; 103 104 /* Wake the thread suspending us. */ 105 _thr_umtx_wake(&curthread->cycle, INT_MAX); 106 107 /* 108 * if we are from pthread_exit, we don't want to 109 * suspend, just go and die. 110 */ 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; 118 } 119 THR_UMTX_UNLOCK(curthread, &(curthread)->lock); 120 curthread->critical_count--; 121 122 _thr_signal_unblock(curthread); 123 } 124 125 void 126 _thr_signal_init(void) 127 { 128 struct sigaction act; 129 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); 135 } 136 137 void 138 _thr_signal_deinit(void) 139 { 140 } 141 142 int 143 _sigaction(int sig, const struct sigaction * act, struct sigaction * oact) 144 { 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); 150 } 151 152 return __sys_sigaction(sig, act, oact); 153 } 154 155 __strong_reference(_sigaction, sigaction); 156 157 int 158 _sigprocmask(int how, const sigset_t *set, sigset_t *oset) 159 { 160 const sigset_t *p = set; 161 sigset_t newset; 162 163 if (how != SIG_UNBLOCK) { 164 if (set != NULL) { 165 newset = *set; 166 SIGDELSET(newset, SIGCANCEL); 167 p = &newset; 168 } 169 } 170 return (__sys_sigprocmask(how, p, oset)); 171 } 172 173 __strong_reference(_sigprocmask, sigprocmask); 174 175 int 176 _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) 177 { 178 if (_sigprocmask(how, set, oset)) 179 return (errno); 180 return (0); 181 } 182 183 __strong_reference(_pthread_sigmask, pthread_sigmask); 184 185 int 186 _sigsuspend(const sigset_t * set) 187 { 188 struct pthread *curthread = tls_get_curthread(); 189 sigset_t newset; 190 const sigset_t *pset; 191 int oldcancel; 192 int ret; 193 194 if (SIGISMEMBER(*set, SIGCANCEL)) { 195 newset = *set; 196 SIGDELSET(newset, SIGCANCEL); 197 pset = &newset; 198 } else 199 pset = set; 200 201 oldcancel = _thr_cancel_enter(curthread); 202 ret = __sys_sigsuspend(pset); 203 _thr_cancel_leave(curthread, oldcancel); 204 205 return (ret); 206 } 207 208 __strong_reference(_sigsuspend, sigsuspend); 209 210 int 211 __sigtimedwait(const sigset_t *set, siginfo_t *info, 212 const struct timespec * timeout) 213 { 214 struct pthread *curthread = tls_get_curthread(); 215 sigset_t newset; 216 const sigset_t *pset; 217 int oldcancel; 218 int ret; 219 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); 230 } 231 232 __strong_reference(__sigtimedwait, sigtimedwait); 233 234 int 235 __sigwaitinfo(const sigset_t *set, siginfo_t *info) 236 { 237 struct pthread *curthread = tls_get_curthread(); 238 sigset_t newset; 239 const sigset_t *pset; 240 int oldcancel; 241 int ret; 242 243 if (SIGISMEMBER(*set, SIGCANCEL)) { 244 newset = *set; 245 SIGDELSET(newset, SIGCANCEL); 246 pset = &newset; 247 } else 248 pset = set; 249 250 oldcancel = _thr_cancel_enter(curthread); 251 ret = __sys_sigwaitinfo(pset, info); 252 _thr_cancel_leave(curthread, oldcancel); 253 return (ret); 254 } 255 256 __strong_reference(__sigwaitinfo, sigwaitinfo); 257 258 int 259 __sigwait(const sigset_t *set, int *sig) 260 { 261 int s; 262 263 s = __sigwaitinfo(set, NULL); 264 if (s > 0) { 265 *sig = s; 266 return (0); 267 } 268 return (errno); 269 } 270 271 __strong_reference(__sigwait, sigwait); 272 273