thr_sigwait.c revision 49439
1/* 2 * Copyright (c) 1997 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 * $Id: uthread_sigwait.c,v 1.6 1999/06/20 08:28:47 jb Exp $ 33 */ 34#include <signal.h> 35#include <errno.h> 36#ifdef _THREAD_SAFE 37#include <pthread.h> 38#include "pthread_private.h" 39 40int 41sigwait(const sigset_t * set, int *sig) 42{ 43 int ret = 0; 44 int i; 45 sigset_t tempset, waitset; 46 struct sigaction act; 47 48 /* 49 * Specify the thread kernel signal handler. 50 */ 51 act.sa_handler = (void (*) ()) _thread_sig_handler; 52 act.sa_flags = SA_RESTART; 53 act.sa_mask = *set; 54 55 /* Ensure the scheduling signal is masked: */ 56 sigaddset(&act.sa_mask, _SCHED_SIGNAL); 57 58 /* 59 * Initialize the set of signals that will be waited on: 60 */ 61 waitset = *set; 62 63 /* These signals can't be waited on. */ 64 sigdelset(&waitset, SIGKILL); 65 sigdelset(&waitset, SIGSTOP); 66 sigdelset(&waitset, _SCHED_SIGNAL); 67 sigdelset(&waitset, SIGCHLD); 68 sigdelset(&waitset, SIGINFO); 69 70 /* Check to see if a pending signal is in the wait mask. */ 71 if (tempset = (_thread_run->sigpend & waitset)) { 72 /* Enter a loop to find a pending signal: */ 73 for (i = 1; i < NSIG; i++) { 74 if (sigismember (&tempset, i)) 75 break; 76 } 77 78 /* Clear the pending signal: */ 79 sigdelset(&_thread_run->sigpend,i); 80 81 /* Return the signal number to the caller: */ 82 *sig = i; 83 84 return (0); 85 } 86 87 /* 88 * Enter a loop to find the signals that are SIG_DFL. For 89 * these signals we must install a dummy signal handler in 90 * order for the kernel to pass them in to us. POSIX says 91 * that the _application_ must explicitly install a dummy 92 * handler for signals that are SIG_IGN in order to sigwait 93 * on them. Note that SIG_IGN signals are left in the 94 * mask because a subsequent sigaction could enable an 95 * ignored signal. 96 */ 97 for (i = 1; i < NSIG; i++) { 98 if (sigismember(&waitset, i) && 99 (_thread_sigact[i - 1].sa_handler == SIG_DFL)) { 100 if (_thread_sys_sigaction(i,&act,NULL) != 0) 101 ret = -1; 102 } 103 } 104 if (ret == 0) { 105 106 /* 107 * Save the wait signal mask. The wait signal 108 * mask is independent of the threads signal mask 109 * and requires separate storage. 110 */ 111 _thread_run->data.sigwait = &waitset; 112 113 /* Wait for a signal: */ 114 _thread_kern_sched_state(PS_SIGWAIT, __FILE__, __LINE__); 115 116 /* Return the signal number to the caller: */ 117 *sig = _thread_run->signo; 118 119 /* 120 * Probably unnecessary, but since it's in a union struct 121 * we don't know how it could be used in the future. 122 */ 123 _thread_run->data.sigwait = NULL; 124 } 125 126 /* Restore the sigactions: */ 127 act.sa_handler = SIG_DFL; 128 for (i = 1; i < NSIG; i++) { 129 if (sigismember(&waitset, i) && 130 (_thread_sigact[i - 1].sa_handler == SIG_DFL)) { 131 if (_thread_sys_sigaction(i,&act,NULL) != 0) 132 ret = -1; 133 } 134 } 135 136 /* Return the completion status: */ 137 return (ret); 138} 139#endif 140