2 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/signalvar.h>
44 #include "pthread_private.h"
47 static void thread_sig_add(struct pthread *pthread, int sig, int has_args);
48 static void thread_sig_check_state(struct pthread *pthread, int sig);
49 static struct pthread *thread_sig_find(int sig);
50 static void thread_sig_handle_special(int sig);
51 static void thread_sigframe_add(struct pthread *thread, int sig,
53 static void thread_sigframe_save(struct pthread *thread,
54 struct pthread_signal_frame *psf);
55 static void thread_sig_invoke_handler(int sig, siginfo_t *info,
56 ucontext_t *ucp, int unblock);
58 /*#define DEBUG_SIGNAL*/
60 #define DBG_MSG stdout_debug
65 #if defined(_PTHREADS_INVARIANTS)
66 #define SIG_SET_ACTIVE() _sig_in_handler = 1
67 #define SIG_SET_INACTIVE() _sig_in_handler = 0
69 #define SIG_SET_ACTIVE()
70 #define SIG_SET_INACTIVE()
74 _thread_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp)
76 struct pthread *curthread = _get_curthread();
77 struct pthread *pthread, *pthread_h;
78 int in_sched = _thread_kern_in_sched;
83 PANIC("Thread signal handler received null context");
84 DBG_MSG("Got signal %d, current thread %p\n", sig, curthread);
86 /* Check if an interval timer signal: */
87 if (sig == _SCHED_SIGNAL) {
88 /* Update the scheduling clock: */
89 gettimeofday((struct timeval *)&_sched_tod, NULL);
94 * The scheduler is already running; ignore this
99 * Check if the scheduler interrupt has come when
100 * the currently running thread has deferred thread
103 else if (curthread->sig_defer_count > 0)
104 curthread->yield_on_sig_undefer = 1;
106 /* Schedule the next thread: */
107 _thread_kern_sched(ucp);
110 * This point should not be reached, so abort the
113 PANIC("Returned to signal function from scheduler");
117 * Check if the kernel has been interrupted while the scheduler
118 * is accessing the scheduling queues or if there is a currently
119 * running thread that has deferred signals.
121 else if ((in_sched != 0) || (curthread->sig_defer_count > 0)) {
122 /* Cast the signal number to a character variable: */
126 * Write the signal number to the kernel pipe so that it will
127 * be ready to read when this signal handler returns.
129 if (_queue_signals != 0) {
132 wgot = __sys_write(_thread_kern_pipe[1], &c,
134 } while (wgot < 0 && errno == EINTR);
135 if (wgot < 0 && errno != EAGAIN) {
136 PANIC("Failed to queue signal");
138 DBG_MSG("Got signal %d, queueing to kernel pipe\n", sig);
140 if (_thread_sigq[sig - 1].blocked == 0) {
141 DBG_MSG("Got signal %d, adding to _thread_sigq\n", sig);
143 * Do not block this signal; it will be blocked
144 * when the pending signals are run down.
146 /* _thread_sigq[sig - 1].blocked = 1; */
149 * Queue the signal, saving siginfo and sigcontext
152 * XXX - Do we need to copy siginfo and ucp?
154 _thread_sigq[sig - 1].signo = sig;
156 memcpy(&_thread_sigq[sig - 1].siginfo, info,
158 memcpy(&_thread_sigq[sig - 1].uc, ucp, sizeof(*ucp));
160 /* Indicate that there are queued signals: */
161 _thread_sigq[sig - 1].pending = 1;
162 _sigq_check_reqd = 1;
164 /* These signals need special handling: */
165 else if (sig == SIGCHLD || sig == SIGTSTP ||
166 sig == SIGTTIN || sig == SIGTTOU) {
167 _thread_sigq[sig - 1].pending = 1;
168 _thread_sigq[sig - 1].signo = sig;
169 _sigq_check_reqd = 1;
172 DBG_MSG("Got signal %d, ignored.\n", sig);
175 * The signal handlers should have been installed so that they
176 * cannot be interrupted by other signals.
178 else if (_thread_sigq[sig - 1].blocked == 0) {
180 * The signal is not blocked; handle the signal.
182 * Ignore subsequent occurrences of this signal
183 * until the current signal is handled:
185 _thread_sigq[sig - 1].blocked = 1;
187 /* This signal will be handled; clear the pending flag: */
188 _thread_sigq[sig - 1].pending = 0;
191 * Save siginfo and sigcontext (ucontext).
193 * XXX - Do we need to copy siginfo and ucp?
195 _thread_sigq[sig - 1].signo = sig;
198 memcpy(&_thread_sigq[sig - 1].siginfo, info,
200 memcpy(&_thread_sigq[sig - 1].uc, ucp, sizeof(*ucp));
203 /* Handle special signals: */
204 thread_sig_handle_special(sig);
207 if ((pthread = thread_sig_find(sig)) == NULL)
208 DBG_MSG("No thread to handle signal %d\n", sig);
209 else if (pthread == curthread) {
210 /* Call the signal handler for the current thread: */
211 thread_sig_invoke_handler(sig, info, ucp, 2);
214 * Set the process signal mask in the context; it
215 * could have changed by the handler.
217 ucp->uc_sigmask = _process_sigmask;
220 * The signal mask was restored; check for any
223 sigset = curthread->sigpend;
224 SIGSETOR(sigset, _process_sigpending);
225 SIGSETNAND(sigset, curthread->sigmask);
226 if (SIGNOTEMPTY(sigset))
227 curthread->check_pending = 1;
229 /* Resume the interrupted thread: */
230 __sys_sigreturn(ucp);
232 DBG_MSG("Got signal %d, adding frame to thread %p\n",
235 /* Setup the target thread to receive the signal: */
236 thread_sig_add(pthread, sig, /*has_args*/ 1);
238 /* Take a peek at the next ready to run thread: */
239 pthread_h = PTHREAD_PRIOQ_FIRST();
240 DBG_MSG("Finished adding frame, head of prio list %p\n",
246 * Switch to a different context if the currently running
247 * thread takes a signal, or if another thread takes a
248 * signal and the currently running thread is not in a
251 if ((pthread_h != NULL) &&
252 (pthread_h->active_priority > curthread->active_priority)) {
253 /* Enter the kernel scheduler: */
254 _thread_kern_sched(ucp);
259 thread_sig_handle_special(sig);
265 thread_sig_invoke_handler(int sig, siginfo_t *info, ucontext_t *ucp,
268 struct pthread *curthread = _get_curthread();
269 void (*sigfunc)(int, siginfo_t *, void *);
271 sigset_t saved_sigmask;
273 /* Invoke the signal handler without going through the scheduler:
275 DBG_MSG("Got signal %d, calling handler for current thread %p\n",
278 /* Save the threads signal mask: */
279 saved_sigmask = curthread->sigmask;
280 saved_seqno = curthread->sigmask_seqno;
282 /* Setup the threads signal mask: */
283 SIGSETOR(curthread->sigmask, _thread_sigact[sig - 1].sa_mask);
284 sigaddset(&curthread->sigmask, sig);
288 * Unblock the signal and restore the process signal
289 * mask in case we don't return from the handler:
291 _thread_sigq[sig - 1].blocked = 0;
293 __sys_sigprocmask(SIG_SETMASK, &_process_sigmask,
298 * Check that a custom handler is installed and if
299 * the signal is not blocked:
301 sigfunc = _thread_sigact[sig - 1].sa_sigaction;
302 if (((__sighandler_t *)sigfunc != SIG_DFL) &&
303 ((__sighandler_t *)sigfunc != SIG_IGN)) {
304 if (((_thread_sigact[sig - 1].sa_flags & SA_SIGINFO) != 0) ||
306 (*(sigfunc))(sig, info, ucp);
308 (*(sigfunc))(sig, (void*)(intptr_t)info->si_code, ucp);
311 * Only restore the signal mask if it hasn't been changed by the
312 * application during invocation of the signal handler:
314 if (curthread->sigmask_seqno == saved_seqno)
315 curthread->sigmask = saved_sigmask;
319 * Find a thread that can handle the signal.
322 thread_sig_find(int sig)
324 struct pthread *curthread = _get_curthread();
325 int handler_installed;
326 struct pthread *pthread, *pthread_next;
327 struct pthread *suspended_thread, *signaled_thread;
329 DBG_MSG("Looking for thread to handle signal %d\n", sig);
330 /* Check if the signal requires a dump of thread information: */
331 if (sig == SIGINFO) {
332 /* Dump thread information to file: */
335 /* Unblock this signal to allow further dumps: */
336 _thread_sigq[sig - 1].blocked = 0;
338 /* Check if an interval timer signal: */
339 else if (sig == _SCHED_SIGNAL) {
341 * This shouldn't ever occur (should this panic?).
345 * Enter a loop to look for threads that have the signal
346 * unmasked. POSIX specifies that a thread in a sigwait
347 * will get the signal over any other threads. Second
348 * preference will be threads in in a sigsuspend. Third
349 * preference will be the current thread. If none of the
350 * above, then the signal is delivered to the first thread
351 * that is found. Note that if a custom handler is not
352 * installed, the signal only affects threads in sigwait.
354 suspended_thread = NULL;
355 if ((curthread != &_thread_kern_thread) &&
356 !sigismember(&curthread->sigmask, sig))
357 signaled_thread = curthread;
359 signaled_thread = NULL;
360 if ((_thread_sigact[sig - 1].sa_handler == SIG_IGN) ||
361 (_thread_sigact[sig - 1].sa_handler == SIG_DFL))
362 handler_installed = 0;
364 handler_installed = 1;
366 for (pthread = TAILQ_FIRST(&_waitingq);
367 pthread != NULL; pthread = pthread_next) {
369 * Grab the next thread before possibly destroying
372 pthread_next = TAILQ_NEXT(pthread, pqe);
374 if ((pthread->state == PS_SIGWAIT) &&
375 sigismember(pthread->data.sigwait, sig)) {
376 /* Change the state of the thread to run: */
377 PTHREAD_NEW_STATE(pthread,PS_RUNNING);
379 * A signal handler is not invoked for threads
380 * in sigwait. Clear the blocked and pending
383 _thread_sigq[sig - 1].blocked = 0;
384 _thread_sigq[sig - 1].pending = 0;
386 /* Return the signal number: */
387 pthread->signo = sig;
390 * POSIX doesn't doesn't specify which thread
391 * will get the signal if there are multiple
392 * waiters, so we give it to the first thread
395 * Do not attempt to deliver this signal
396 * to other threads and do not add the signal
397 * to the process pending set.
401 else if ((handler_installed != 0) &&
402 !sigismember(&pthread->sigmask, sig) &&
403 ((pthread->flags & PTHREAD_FLAGS_SUSPENDED) == 0)) {
404 if (pthread->state == PS_SIGSUSPEND) {
405 if (suspended_thread == NULL)
406 suspended_thread = pthread;
407 } else if (signaled_thread == NULL)
408 signaled_thread = pthread;
413 * Only perform wakeups and signal delivery if there is a
414 * custom handler installed:
416 if (handler_installed == 0) {
418 * There is no handler installed. Unblock the
419 * signal so that if a handler _is_ installed, any
420 * subsequent signals can be handled.
422 _thread_sigq[sig - 1].blocked = 0;
425 * If we didn't find a thread in the waiting queue,
426 * check the all threads queue:
428 if (suspended_thread == NULL &&
429 signaled_thread == NULL) {
431 * Enter a loop to look for other threads
432 * capable of receiving the signal:
434 TAILQ_FOREACH(pthread, &_thread_list, tle) {
435 if (!sigismember(&pthread->sigmask,
437 signaled_thread = pthread;
443 if (suspended_thread == NULL &&
444 signaled_thread == NULL)
446 * Add it to the set of signals pending
449 sigaddset(&_process_sigpending, sig);
452 * We only deliver the signal to one thread;
453 * give preference to the suspended thread:
455 if (suspended_thread != NULL)
456 pthread = suspended_thread;
458 pthread = signaled_thread;
464 /* Returns nothing. */
469 _thread_sig_check_pending(struct pthread *pthread)
475 * Check if there are pending signals for the running
476 * thread or process that aren't blocked:
478 sigset = pthread->sigpend;
479 SIGSETOR(sigset, _process_sigpending);
480 SIGSETNAND(sigset, pthread->sigmask);
481 if (SIGNOTEMPTY(sigset)) {
482 for (i = 1; i < NSIG; i++) {
483 if (sigismember(&sigset, i) != 0) {
484 if (sigismember(&pthread->sigpend, i) != 0)
485 thread_sig_add(pthread, i,
488 thread_sig_add(pthread, i,
490 sigdelset(&_process_sigpending, i);
498 * This can only be called from the kernel scheduler. It assumes that
499 * all thread contexts are saved and that a signal frame can safely be
500 * added to any user thread.
503 _thread_sig_handle_pending(void)
505 struct pthread *pthread;
508 PTHREAD_ASSERT(_thread_kern_in_sched != 0,
509 "_thread_sig_handle_pending called from outside kernel schedule");
511 * Check the array of pending signals:
513 for (i = 0; i < NSIG; i++) {
514 if (_thread_sigq[i].pending != 0) {
515 /* This signal is no longer pending. */
516 _thread_sigq[i].pending = 0;
518 sig = _thread_sigq[i].signo;
520 /* Some signals need special handling: */
521 thread_sig_handle_special(sig);
523 if (_thread_sigq[i].blocked == 0) {
525 * Block future signals until this one
528 _thread_sigq[i].blocked = 1;
530 if ((pthread = thread_sig_find(sig)) != NULL) {
532 * Setup the target thread to receive
535 thread_sig_add(pthread, sig,
544 thread_sig_handle_special(int sig)
546 struct pthread *pthread, *pthread_next;
552 * Go through the file list and set all files
553 * to non-blocking again in case the child
554 * set some of them to block. Sigh.
556 for (i = 0; i < _thread_dtablesize; i++) {
557 /* Check if this file is used: */
558 if (_thread_fd_table[i] != NULL) {
560 * Set the file descriptor to non-blocking:
562 __sys_fcntl(i, F_SETFL,
563 _thread_fd_getflags(i) | O_NONBLOCK);
567 * Enter a loop to wake up all threads waiting
568 * for a process to complete:
570 for (pthread = TAILQ_FIRST(&_waitingq);
571 pthread != NULL; pthread = pthread_next) {
573 * Grab the next thread before possibly
574 * destroying the link entry:
576 pthread_next = TAILQ_NEXT(pthread, pqe);
579 * If this thread is waiting for a child
580 * process to complete, wake it up:
582 if (pthread->state == PS_WAIT_WAIT) {
583 /* Make the thread runnable: */
584 PTHREAD_NEW_STATE(pthread,PS_RUNNING);
586 /* Return the signal number: */
587 pthread->signo = sig;
593 * POSIX says that pending SIGCONT signals are
594 * discarded when one of these signals occurs.
600 * Enter a loop to discard pending SIGCONT
603 TAILQ_FOREACH(pthread, &_thread_list, tle) {
604 sigdelset(&pthread->sigpend, SIGCONT);
614 * Perform thread specific actions in response to a signal.
615 * This function is only called if there is a handler installed
616 * for the signal, and if the target thread has the signal
620 thread_sig_add(struct pthread *pthread, int sig, int has_args)
623 int suppress_handler = 0;
624 int thread_is_active = 0;
626 restart = _thread_sigact[sig - 1].sa_flags & SA_RESTART;
628 /* Make sure this signal isn't still in the pending set: */
629 sigdelset(&pthread->sigpend, sig);
632 * Process according to thread state:
634 switch (pthread->state) {
636 * States which do not change when a signal is trapped:
643 * You can't call a signal handler for threads in these
646 suppress_handler = 1;
650 * States which do not need any cleanup handling when signals
655 * Remove the thread from the queue before changing its
658 if ((pthread->flags & PTHREAD_FLAGS_IN_PRIOQ) != 0)
659 PTHREAD_PRIOQ_REMOVE(pthread);
662 * This thread is running; avoid placing it in
665 thread_is_active = 1;
672 /* Remove the thread from the workq and waitq: */
673 PTHREAD_WORKQ_REMOVE(pthread);
674 PTHREAD_WAITQ_REMOVE(pthread);
675 /* Make the thread runnable: */
676 PTHREAD_SET_STATE(pthread, PS_RUNNING);
680 /* The signal handler is not called for threads in SIGWAIT. */
681 suppress_handler = 1;
682 /* Wake up the thread if the signal is blocked. */
683 if (sigismember(pthread->data.sigwait, sig)) {
684 /* Change the state of the thread to run: */
685 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
687 /* Return the signal number: */
688 pthread->signo = sig;
690 /* Increment the pending signal count. */
691 sigaddset(&pthread->sigpend, sig);
695 * The wait state is a special case due to the handling of
699 if (sig == SIGCHLD) {
700 /* Change the state of the thread to run: */
701 PTHREAD_WAITQ_REMOVE(pthread);
702 PTHREAD_SET_STATE(pthread, PS_RUNNING);
704 /* Return the signal number: */
705 pthread->signo = sig;
709 * Mark the thread as interrupted only if the
710 * restart flag is not set on the signal action:
713 pthread->interrupted = 1;
714 PTHREAD_WAITQ_REMOVE(pthread);
715 PTHREAD_SET_STATE(pthread, PS_RUNNING);
720 * States which cannot be interrupted but still require the
721 * signal handler to run:
726 * Remove the thread from the wait queue. It will
727 * be added back to the wait queue once all signal
728 * handlers have been invoked.
730 PTHREAD_WAITQ_REMOVE(pthread);
735 * Remove the thread from the wait queue. It will
736 * be added back to the wait queue once all signal
737 * handlers have been invoked.
739 PTHREAD_WAITQ_REMOVE(pthread);
740 /* Make the thread runnable: */
741 PTHREAD_SET_STATE(pthread, PS_RUNNING);
745 * States which are interruptible but may need to be removed
746 * from queues before any signal handler is called.
748 * XXX - We may not need to handle this condition, but will
749 * mark it as a potential problem.
755 pthread->interrupted = 1;
757 * Remove the thread from the wait queue. Our
758 * signal handler hook will remove this thread
759 * from the fd or file queue before invoking
760 * the actual handler.
762 PTHREAD_WAITQ_REMOVE(pthread);
766 * States which are interruptible:
772 * Flag the operation as interrupted and
773 * set the state to running:
775 pthread->interrupted = 1;
776 PTHREAD_SET_STATE(pthread, PS_RUNNING);
778 PTHREAD_WORKQ_REMOVE(pthread);
779 PTHREAD_WAITQ_REMOVE(pthread);
786 * Unmasked signals always cause poll, select, and sleep
787 * to terminate early, regardless of SA_RESTART:
789 pthread->interrupted = 1;
790 /* Remove threads in poll and select from the workq: */
791 if ((pthread->flags & PTHREAD_FLAGS_IN_WORKQ) != 0)
792 PTHREAD_WORKQ_REMOVE(pthread);
793 PTHREAD_WAITQ_REMOVE(pthread);
794 PTHREAD_SET_STATE(pthread, PS_RUNNING);
798 PTHREAD_WAITQ_REMOVE(pthread);
799 PTHREAD_SET_STATE(pthread, PS_RUNNING);
803 if (suppress_handler == 0) {
804 /* Setup a signal frame and save the current threads state: */
805 thread_sigframe_add(pthread, sig, has_args);
808 * Signals are deferred until just before the threads
809 * signal handler is invoked:
811 pthread->sig_defer_count = 1;
813 /* Make sure the thread is runnable: */
814 if (pthread->state != PS_RUNNING)
815 PTHREAD_SET_STATE(pthread, PS_RUNNING);
817 * The thread should be removed from all scheduling
818 * queues at this point. Raise the priority and place
819 * the thread in the run queue. It is also possible
820 * for a signal to be sent to a suspended thread,
821 * mostly via pthread_kill(). If a thread is suspended,
822 * don't insert it into the priority queue; just set
823 * its state to suspended and it will run the signal
824 * handler when it is resumed.
826 pthread->active_priority |= PTHREAD_SIGNAL_PRIORITY;
827 if ((pthread->flags & PTHREAD_FLAGS_SUSPENDED) != 0)
828 PTHREAD_SET_STATE(pthread, PS_SUSPENDED);
829 else if (thread_is_active == 0)
830 PTHREAD_PRIOQ_INSERT_TAIL(pthread);
835 thread_sig_check_state(struct pthread *pthread, int sig)
838 * Process according to thread state:
840 switch (pthread->state) {
842 * States which do not change when a signal is trapped:
857 /* Wake up the thread if the signal is blocked. */
858 if (sigismember(pthread->data.sigwait, sig)) {
859 /* Change the state of the thread to run: */
860 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
862 /* Return the signal number: */
863 pthread->signo = sig;
865 /* Increment the pending signal count. */
866 sigaddset(&pthread->sigpend, sig);
870 * The wait state is a special case due to the handling of
874 if (sig == SIGCHLD) {
876 * Remove the thread from the wait queue and
879 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
881 /* Return the signal number: */
882 pthread->signo = sig;
891 * Remove the thread from the wait queue and make it
894 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
896 /* Flag the operation as interrupted: */
897 pthread->interrupted = 1;
901 * These states are additionally in the work queue:
909 * Remove the thread from the wait and work queues, and
912 PTHREAD_WORKQ_REMOVE(pthread);
913 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
915 /* Flag the operation as interrupted: */
916 pthread->interrupted = 1;
922 * Send a signal to a specific thread (ala pthread_kill):
925 _thread_sig_send(struct pthread *pthread, int sig)
927 struct pthread *curthread = _get_curthread();
929 /* Check for signals whose actions are SIG_DFL: */
930 if (_thread_sigact[sig - 1].sa_handler == SIG_DFL) {
932 * Check to see if a temporary signal handler is
933 * installed for sigwaiters:
935 if (_thread_dfl_count[sig] == 0)
937 * Deliver the signal to the process if a handler
942 * Assuming we're still running after the above kill(),
943 * make any necessary state changes to the thread:
945 thread_sig_check_state(pthread, sig);
948 * Check that the signal is not being ignored:
950 else if (_thread_sigact[sig - 1].sa_handler != SIG_IGN) {
951 if (pthread->state == PS_SIGWAIT &&
952 sigismember(pthread->data.sigwait, sig)) {
953 /* Change the state of the thread to run: */
954 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
956 /* Return the signal number: */
957 pthread->signo = sig;
958 } else if (sigismember(&pthread->sigmask, sig))
959 /* Add the signal to the pending set: */
960 sigaddset(&pthread->sigpend, sig);
961 else if (pthread == curthread)
962 /* Call the signal handler for the current thread: */
963 thread_sig_invoke_handler(sig, NULL, NULL, 0);
965 /* Protect the scheduling queues: */
966 _thread_kern_sig_defer();
968 * Perform any state changes due to signal
971 thread_sig_add(pthread, sig, /* has args */ 0);
972 /* Unprotect the scheduling queues: */
973 _thread_kern_sig_undefer();
979 * User thread signal handler wrapper.
981 * thread - current running thread
984 _thread_sig_wrapper(void)
986 struct pthread_signal_frame *psf;
987 struct pthread *thread = _get_curthread();
989 /* Get the current frame and state: */
990 psf = thread->curframe;
991 thread->curframe = NULL;
992 PTHREAD_ASSERT(psf != NULL, "Invalid signal frame in signal handler");
995 * We're coming from the kernel scheduler; clear the in
998 _thread_kern_in_sched = 0;
1000 /* Check the threads previous state: */
1001 if (psf->saved_state.psd_state != PS_RUNNING) {
1003 * Do a little cleanup handling for those threads in
1004 * queues before calling the signal handler. Signals
1005 * for these threads are temporarily blocked until
1006 * after cleanup handling.
1008 switch (psf->saved_state.psd_state) {
1011 _fd_lock_backout(thread);
1012 psf->saved_state.psd_state = PS_RUNNING;
1016 _cond_wait_backout(thread);
1017 psf->saved_state.psd_state = PS_RUNNING;
1021 _mutex_lock_backout(thread);
1022 psf->saved_state.psd_state = PS_RUNNING;
1031 * Lower the priority before calling the handler in case
1032 * it never returns (longjmps back):
1034 thread->active_priority &= ~PTHREAD_SIGNAL_PRIORITY;
1037 * Reenable interruptions without checking for the need to
1040 thread->sig_defer_count = 0;
1043 * Dispatch the signal via the custom signal handler:
1045 if (psf->sig_has_args == 0)
1046 thread_sig_invoke_handler(psf->signo, NULL, NULL, 1);
1048 thread_sig_invoke_handler(psf->signo, &psf->siginfo, &psf->uc,
1052 * Call the kernel scheduler to safely restore the frame and
1053 * schedule the next thread:
1055 _thread_kern_sched_frame(psf);
1059 thread_sigframe_add(struct pthread *thread, int sig, int has_args)
1061 struct pthread_signal_frame *psf = NULL;
1062 unsigned long stackp;
1064 /* Get the top of the threads stack: */
1065 stackp = GET_STACK_JB(thread->ctx.jb);
1067 #if !defined(__ia64__)
1069 * Leave a little space on the stack and round down to the
1070 * nearest aligned word:
1072 #if defined(__amd64__)
1073 stackp -= 128; /* Skip over 128 byte red-zone */
1075 stackp -= sizeof(double);
1076 #if defined(__amd64__)
1083 /* Allocate room on top of the stack for a new signal frame: */
1084 stackp -= sizeof(struct pthread_signal_frame);
1085 #if defined(__ia64__) || defined(__amd64__)
1089 psf = (struct pthread_signal_frame *) stackp;
1091 /* Save the current context in the signal frame: */
1092 thread_sigframe_save(thread, psf);
1094 /* Set handler specific information: */
1095 psf->sig_has_args = has_args;
1098 /* Copy the signal handler arguments to the signal frame: */
1099 memcpy(&psf->uc, &_thread_sigq[psf->signo - 1].uc,
1101 memcpy(&psf->siginfo, &_thread_sigq[psf->signo - 1].siginfo,
1102 sizeof(psf->siginfo));
1105 /* Setup the signal mask: */
1106 SIGSETOR(thread->sigmask, _thread_sigact[sig - 1].sa_mask);
1107 sigaddset(&thread->sigmask, sig);
1109 /* Set up the new frame: */
1110 thread->curframe = psf;
1111 thread->flags &= PTHREAD_FLAGS_PRIVATE | PTHREAD_FLAGS_TRACE |
1112 PTHREAD_FLAGS_IN_SYNCQ;
1114 * Set up the context:
1116 #if !defined(__ia64__)
1117 stackp -= sizeof(double);
1118 #if defined(__amd64__)
1122 _setjmp(thread->ctx.jb);
1123 #if !defined(__ia64__)
1124 SET_STACK_JB(thread->ctx.jb, stackp);
1126 UPD_STACK_JB(thread->ctx.jb, stackp - 16);
1128 SET_RETURN_ADDR_JB(thread->ctx.jb, _thread_sig_wrapper);
1132 _thread_sigframe_restore(struct pthread *thread,
1133 struct pthread_signal_frame *psf)
1135 memcpy(&thread->ctx, &psf->ctx, sizeof(thread->ctx));
1137 * Only restore the signal mask if it hasn't been changed
1138 * by the application during invocation of the signal handler:
1140 if (thread->sigmask_seqno == psf->saved_state.psd_sigmask_seqno)
1141 thread->sigmask = psf->saved_state.psd_sigmask;
1142 thread->curframe = psf->saved_state.psd_curframe;
1143 thread->wakeup_time = psf->saved_state.psd_wakeup_time;
1144 thread->data = psf->saved_state.psd_wait_data;
1145 thread->state = psf->saved_state.psd_state;
1146 thread->flags = psf->saved_state.psd_flags;
1147 thread->interrupted = psf->saved_state.psd_interrupted;
1148 thread->signo = psf->saved_state.psd_signo;
1149 thread->sig_defer_count = psf->saved_state.psd_sig_defer_count;
1153 thread_sigframe_save(struct pthread *thread, struct pthread_signal_frame *psf)
1155 memcpy(&psf->ctx, &thread->ctx, sizeof(thread->ctx));
1156 psf->saved_state.psd_sigmask = thread->sigmask;
1157 psf->saved_state.psd_curframe = thread->curframe;
1158 psf->saved_state.psd_wakeup_time = thread->wakeup_time;
1159 psf->saved_state.psd_wait_data = thread->data;
1160 psf->saved_state.psd_state = thread->state;
1161 psf->saved_state.psd_flags = thread->flags &
1162 (PTHREAD_FLAGS_PRIVATE | PTHREAD_FLAGS_TRACE);
1163 psf->saved_state.psd_interrupted = thread->interrupted;
1164 psf->saved_state.psd_sigmask_seqno = thread->sigmask_seqno;
1165 psf->saved_state.psd_signo = thread->signo;
1166 psf->saved_state.psd_sig_defer_count = thread->sig_defer_count;