1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
5 /* This program is free software; you can redistribute it and/or */
6 /* modify it under the terms of the GNU Library General Public License */
7 /* as published by the Free Software Foundation; either version 2 */
8 /* of the License, or (at your option) any later version. */
10 /* This program is distributed in the hope that it will be useful, */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
13 /* GNU Library General Public License for more details. */
15 /* Handling of signals */
20 #include "internals.h"
23 int pthread_sigmask(int how, const sigset_t * newmask, sigset_t * oldmask)
27 if (newmask != NULL) {
29 /* Don't allow PTHREAD_SIG_RESTART to be unmasked.
30 Don't allow PTHREAD_SIG_CANCEL to be masked. */
33 sigaddset(&mask, PTHREAD_SIG_RESTART);
34 sigdelset(&mask, PTHREAD_SIG_CANCEL);
37 sigdelset(&mask, PTHREAD_SIG_CANCEL);
40 sigdelset(&mask, PTHREAD_SIG_RESTART);
45 if (sigprocmask(how, newmask, oldmask) == -1)
51 int pthread_kill(pthread_t thread, int signo)
53 pthread_handle handle = thread_handle(thread);
56 acquire(&handle->h_spinlock);
57 if (invalid_handle(handle, thread)) {
58 release(&handle->h_spinlock);
61 pid = handle->h_descr->p_pid;
62 release(&handle->h_spinlock);
63 if (kill(pid, signo) == -1)
69 /* The set of signals on which some thread is doing a sigwait */
70 static sigset_t sigwaited;
71 static pthread_mutex_t sigwaited_mut = PTHREAD_MUTEX_INITIALIZER;
72 static pthread_cond_t sigwaited_changed = PTHREAD_COND_INITIALIZER;
74 int sigwait(const sigset_t * set, int * sig)
76 volatile pthread_descr self = thread_self();
79 struct sigaction action, saved_signals[NSIG];
82 pthread_mutex_lock(&sigwaited_mut);
83 /* Make sure no other thread is waiting on our signals */
85 for (s = 1; s < NSIG; s++) {
86 if (sigismember(set, s) && sigismember(&sigwaited, s)) {
87 pthread_cond_wait(&sigwaited_changed, &sigwaited_mut);
91 /* Get ready to block all signals except those in set
92 and the cancellation signal */
94 sigdelset(&mask, PTHREAD_SIG_CANCEL);
95 /* Signals in set are assumed blocked on entrance */
96 /* Install our signal handler on all signals in set,
97 and unblock them in mask.
98 Also mark those signals as being sigwaited on */
99 for (s = 1; s < NSIG; s++) {
100 if (sigismember(set, s) && s != PTHREAD_SIG_CANCEL) {
102 action.sa_handler = __pthread_sighandler;
103 sigemptyset(&action.sa_mask);
105 sigaction(s, &action, &(saved_signals[s]));
106 sigaddset(&sigwaited, s);
109 pthread_mutex_unlock(&sigwaited_mut);
111 /* Test for cancellation */
112 if (sigsetjmp(jmpbuf, 1) == 0) {
113 self->p_cancel_jmp = &jmpbuf;
114 if (! (self->p_canceled && self->p_cancelstate == PTHREAD_CANCEL_ENABLE)) {
115 /* Reset the signal count */
117 /* Unblock the signals and wait for them */
121 self->p_cancel_jmp = NULL;
122 /* The signals are now reblocked. Restore the sighandlers. */
123 pthread_mutex_lock(&sigwaited_mut);
124 for (s = 1; s < NSIG; s++) {
125 if (sigismember(set, s) && s != PTHREAD_SIG_CANCEL) {
126 sigaction(s, &(saved_signals[s]), NULL);
127 sigdelset(&sigwaited, s);
130 pthread_cond_broadcast(&sigwaited_changed);
131 pthread_mutex_unlock(&sigwaited_mut);
132 /* Check for cancellation */
133 pthread_testcancel();
134 /* We should have self->p_signal != 0 and equal to the signal received */
135 *sig = self->p_signal;
141 int retcode = pthread_kill(pthread_self(), sig);