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,
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;
82 PANIC("Thread signal handler received null context");
83 DBG_MSG("Got signal %d, current thread %p\n", sig, curthread);
85 /* Check if an interval timer signal: */
86 if (sig == _SCHED_SIGNAL) {
87 /* Update the scheduling clock: */
88 gettimeofday((struct timeval *)&_sched_tod, NULL);
93 * The scheduler is already running; ignore this
98 * Check if the scheduler interrupt has come when
99 * the currently running thread has deferred thread
102 else if (curthread->sig_defer_count > 0)
103 curthread->yield_on_sig_undefer = 1;
105 /* Schedule the next thread: */
106 _thread_kern_sched(ucp);
109 * This point should not be reached, so abort the
112 PANIC("Returned to signal function from scheduler");
116 * Check if the kernel has been interrupted while the scheduler
117 * is accessing the scheduling queues or if there is a currently
118 * running thread that has deferred signals.
120 else if ((in_sched != 0) || (curthread->sig_defer_count > 0)) {
121 /* Cast the signal number to a character variable: */
125 * Write the signal number to the kernel pipe so that it will
126 * be ready to read when this signal handler returns.
128 if (_queue_signals != 0) {
129 __sys_write(_thread_kern_pipe[1], &c, 1);
130 DBG_MSG("Got signal %d, queueing to kernel pipe\n", sig);
132 if (_thread_sigq[sig - 1].blocked == 0) {
133 DBG_MSG("Got signal %d, adding to _thread_sigq\n", sig);
135 * Do not block this signal; it will be blocked
136 * when the pending signals are run down.
138 /* _thread_sigq[sig - 1].blocked = 1; */
141 * Queue the signal, saving siginfo and sigcontext
144 * XXX - Do we need to copy siginfo and ucp?
146 _thread_sigq[sig - 1].signo = sig;
148 memcpy(&_thread_sigq[sig - 1].siginfo, info,
150 memcpy(&_thread_sigq[sig - 1].uc, ucp, sizeof(*ucp));
152 /* Indicate that there are queued signals: */
153 _thread_sigq[sig - 1].pending = 1;
154 _sigq_check_reqd = 1;
156 /* These signals need special handling: */
157 else if (sig == SIGCHLD || sig == SIGTSTP ||
158 sig == SIGTTIN || sig == SIGTTOU) {
159 _thread_sigq[sig - 1].pending = 1;
160 _thread_sigq[sig - 1].signo = sig;
161 _sigq_check_reqd = 1;
164 DBG_MSG("Got signal %d, ignored.\n", sig);
167 * The signal handlers should have been installed so that they
168 * cannot be interrupted by other signals.
170 else if (_thread_sigq[sig - 1].blocked == 0) {
172 * The signal is not blocked; handle the signal.
174 * Ignore subsequent occurrences of this signal
175 * until the current signal is handled:
177 _thread_sigq[sig - 1].blocked = 1;
179 /* This signal will be handled; clear the pending flag: */
180 _thread_sigq[sig - 1].pending = 0;
183 * Save siginfo and sigcontext (ucontext).
185 * XXX - Do we need to copy siginfo and ucp?
187 _thread_sigq[sig - 1].signo = sig;
190 memcpy(&_thread_sigq[sig - 1].siginfo, info,
192 memcpy(&_thread_sigq[sig - 1].uc, ucp, sizeof(*ucp));
195 /* Handle special signals: */
196 thread_sig_handle_special(sig);
199 if ((pthread = thread_sig_find(sig)) == NULL)
200 DBG_MSG("No thread to handle signal %d\n", sig);
201 else if (pthread == curthread) {
203 * Unblock the signal and restore the process signal
204 * mask in case we don't return from the handler:
206 _thread_sigq[sig - 1].blocked = 0;
207 __sys_sigprocmask(SIG_SETMASK, &_process_sigmask, NULL);
209 /* Call the signal handler for the current thread: */
210 thread_sig_invoke_handler(sig, info, ucp);
213 * Set the process signal mask in the context; it
214 * could have changed by the handler.
216 ucp->uc_sigmask = _process_sigmask;
218 /* Resume the interrupted thread: */
219 __sys_sigreturn(ucp);
221 DBG_MSG("Got signal %d, adding frame to thread %p\n",
224 /* Setup the target thread to receive the signal: */
225 thread_sig_add(pthread, sig, /*has_args*/ 1);
227 /* Take a peek at the next ready to run thread: */
228 pthread_h = PTHREAD_PRIOQ_FIRST();
229 DBG_MSG("Finished adding frame, head of prio list %p\n",
235 * Switch to a different context if the currently running
236 * thread takes a signal, or if another thread takes a
237 * signal and the currently running thread is not in a
240 if ((pthread_h != NULL) &&
241 (pthread_h->active_priority > curthread->active_priority)) {
242 /* Enter the kernel scheduler: */
243 _thread_kern_sched(ucp);
248 thread_sig_handle_special(sig);
254 thread_sig_invoke_handler(int sig, siginfo_t *info, ucontext_t *ucp)
256 struct pthread *curthread = _get_curthread();
257 void (*sigfunc)(int, siginfo_t *, void *);
259 sigset_t saved_sigmask;
261 /* Invoke the signal handler without going through the scheduler:
263 DBG_MSG("Got signal %d, calling handler for current thread %p\n",
266 /* Save the threads signal mask: */
267 saved_sigmask = curthread->sigmask;
268 saved_seqno = curthread->sigmask_seqno;
270 /* Setup the threads signal mask: */
271 SIGSETOR(curthread->sigmask, _thread_sigact[sig - 1].sa_mask);
272 sigaddset(&curthread->sigmask, sig);
275 * Check that a custom handler is installed and if
276 * the signal is not blocked:
278 sigfunc = _thread_sigact[sig - 1].sa_sigaction;
279 if (((__sighandler_t *)sigfunc != SIG_DFL) &&
280 ((__sighandler_t *)sigfunc != SIG_IGN)) {
281 if (((_thread_sigact[sig - 1].sa_flags & SA_SIGINFO) != 0) ||
283 (*(sigfunc))(sig, info, ucp);
285 (*(sigfunc))(sig, (void*)(intptr_t)info->si_code, ucp);
288 * Only restore the signal mask if it hasn't been changed by the
289 * application during invocation of the signal handler:
291 if (curthread->sigmask_seqno == saved_seqno)
292 curthread->sigmask = saved_sigmask;
296 * Find a thread that can handle the signal.
299 thread_sig_find(int sig)
301 struct pthread *curthread = _get_curthread();
302 int handler_installed;
303 struct pthread *pthread, *pthread_next;
304 struct pthread *suspended_thread, *signaled_thread;
306 DBG_MSG("Looking for thread to handle signal %d\n", sig);
307 /* Check if the signal requires a dump of thread information: */
308 if (sig == SIGINFO) {
309 /* Dump thread information to file: */
312 /* Unblock this signal to allow further dumps: */
313 _thread_sigq[sig - 1].blocked = 0;
315 /* Check if an interval timer signal: */
316 else if (sig == _SCHED_SIGNAL) {
318 * This shouldn't ever occur (should this panic?).
322 * Enter a loop to look for threads that have the signal
323 * unmasked. POSIX specifies that a thread in a sigwait
324 * will get the signal over any other threads. Second
325 * preference will be threads in in a sigsuspend. Third
326 * preference will be the current thread. If none of the
327 * above, then the signal is delivered to the first thread
328 * that is found. Note that if a custom handler is not
329 * installed, the signal only affects threads in sigwait.
331 suspended_thread = NULL;
332 if ((curthread != &_thread_kern_thread) &&
333 !sigismember(&curthread->sigmask, sig))
334 signaled_thread = curthread;
336 signaled_thread = NULL;
337 if ((_thread_sigact[sig - 1].sa_handler == SIG_IGN) ||
338 (_thread_sigact[sig - 1].sa_handler == SIG_DFL))
339 handler_installed = 0;
341 handler_installed = 1;
343 for (pthread = TAILQ_FIRST(&_waitingq);
344 pthread != NULL; pthread = pthread_next) {
346 * Grab the next thread before possibly destroying
349 pthread_next = TAILQ_NEXT(pthread, pqe);
351 if ((pthread->state == PS_SIGWAIT) &&
352 sigismember(pthread->data.sigwait, sig)) {
353 /* Change the state of the thread to run: */
354 PTHREAD_NEW_STATE(pthread,PS_RUNNING);
356 * A signal handler is not invoked for threads
357 * in sigwait. Clear the blocked and pending
360 _thread_sigq[sig - 1].blocked = 0;
361 _thread_sigq[sig - 1].pending = 0;
363 /* Return the signal number: */
364 pthread->signo = sig;
367 * POSIX doesn't doesn't specify which thread
368 * will get the signal if there are multiple
369 * waiters, so we give it to the first thread
372 * Do not attempt to deliver this signal
373 * to other threads and do not add the signal
374 * to the process pending set.
378 else if ((handler_installed != 0) &&
379 !sigismember(&pthread->sigmask, sig) &&
380 ((pthread->flags & PTHREAD_FLAGS_SUSPENDED) == 0)) {
381 if (pthread->state == PS_SIGSUSPEND) {
382 if (suspended_thread == NULL)
383 suspended_thread = pthread;
384 } else if (signaled_thread == NULL)
385 signaled_thread = pthread;
390 * Only perform wakeups and signal delivery if there is a
391 * custom handler installed:
393 if (handler_installed == 0) {
395 * There is no handler installed. Unblock the
396 * signal so that if a handler _is_ installed, any
397 * subsequent signals can be handled.
399 _thread_sigq[sig - 1].blocked = 0;
402 * If we didn't find a thread in the waiting queue,
403 * check the all threads queue:
405 if (suspended_thread == NULL &&
406 signaled_thread == NULL) {
408 * Enter a loop to look for other threads
409 * capable of receiving the signal:
411 TAILQ_FOREACH(pthread, &_thread_list, tle) {
412 if (!sigismember(&pthread->sigmask,
414 signaled_thread = pthread;
420 if (suspended_thread == NULL &&
421 signaled_thread == NULL)
423 * Add it to the set of signals pending
426 sigaddset(&_process_sigpending, sig);
429 * We only deliver the signal to one thread;
430 * give preference to the suspended thread:
432 if (suspended_thread != NULL)
433 pthread = suspended_thread;
435 pthread = signaled_thread;
441 /* Returns nothing. */
446 _thread_sig_check_pending(struct pthread *pthread)
452 * Check if there are pending signals for the running
453 * thread or process that aren't blocked:
455 sigset = pthread->sigpend;
456 SIGSETOR(sigset, _process_sigpending);
457 SIGSETNAND(sigset, pthread->sigmask);
458 if (SIGNOTEMPTY(sigset)) {
459 for (i = 1; i < NSIG; i++) {
460 if (sigismember(&sigset, i) != 0) {
461 if (sigismember(&pthread->sigpend, i) != 0)
462 thread_sig_add(pthread, i,
465 thread_sig_add(pthread, i,
467 sigdelset(&_process_sigpending, i);
475 * This can only be called from the kernel scheduler. It assumes that
476 * all thread contexts are saved and that a signal frame can safely be
477 * added to any user thread.
480 _thread_sig_handle_pending(void)
482 struct pthread *pthread;
485 PTHREAD_ASSERT(_thread_kern_in_sched != 0,
486 "_thread_sig_handle_pending called from outside kernel schedule");
488 * Check the array of pending signals:
490 for (i = 0; i < NSIG; i++) {
491 if (_thread_sigq[i].pending != 0) {
492 /* This signal is no longer pending. */
493 _thread_sigq[i].pending = 0;
495 sig = _thread_sigq[i].signo;
497 /* Some signals need special handling: */
498 thread_sig_handle_special(sig);
500 if (_thread_sigq[i].blocked == 0) {
502 * Block future signals until this one
505 _thread_sigq[i].blocked = 1;
507 if ((pthread = thread_sig_find(sig)) != NULL) {
509 * Setup the target thread to receive
512 thread_sig_add(pthread, sig,
521 thread_sig_handle_special(int sig)
523 struct pthread *pthread, *pthread_next;
529 * Go through the file list and set all files
530 * to non-blocking again in case the child
531 * set some of them to block. Sigh.
533 for (i = 0; i < _thread_dtablesize; i++) {
534 /* Check if this file is used: */
535 if (_thread_fd_table[i] != NULL) {
537 * Set the file descriptor to non-blocking:
539 __sys_fcntl(i, F_SETFL,
540 _thread_fd_getflags(i) | O_NONBLOCK);
544 * Enter a loop to wake up all threads waiting
545 * for a process to complete:
547 for (pthread = TAILQ_FIRST(&_waitingq);
548 pthread != NULL; pthread = pthread_next) {
550 * Grab the next thread before possibly
551 * destroying the link entry:
553 pthread_next = TAILQ_NEXT(pthread, pqe);
556 * If this thread is waiting for a child
557 * process to complete, wake it up:
559 if (pthread->state == PS_WAIT_WAIT) {
560 /* Make the thread runnable: */
561 PTHREAD_NEW_STATE(pthread,PS_RUNNING);
563 /* Return the signal number: */
564 pthread->signo = sig;
570 * POSIX says that pending SIGCONT signals are
571 * discarded when one of these signals occurs.
577 * Enter a loop to discard pending SIGCONT
580 TAILQ_FOREACH(pthread, &_thread_list, tle) {
581 sigdelset(&pthread->sigpend, SIGCONT);
591 * Perform thread specific actions in response to a signal.
592 * This function is only called if there is a handler installed
593 * for the signal, and if the target thread has the signal
597 thread_sig_add(struct pthread *pthread, int sig, int has_args)
600 int suppress_handler = 0;
601 int thread_is_active = 0;
603 restart = _thread_sigact[sig - 1].sa_flags & SA_RESTART;
605 /* Make sure this signal isn't still in the pending set: */
606 sigdelset(&pthread->sigpend, sig);
609 * Process according to thread state:
611 switch (pthread->state) {
613 * States which do not change when a signal is trapped:
620 * You can't call a signal handler for threads in these
623 suppress_handler = 1;
627 * States which do not need any cleanup handling when signals
632 * Remove the thread from the queue before changing its
635 if ((pthread->flags & PTHREAD_FLAGS_IN_PRIOQ) != 0)
636 PTHREAD_PRIOQ_REMOVE(pthread);
639 * This thread is running; avoid placing it in
642 thread_is_active = 1;
649 /* Remove the thread from the workq and waitq: */
650 PTHREAD_WORKQ_REMOVE(pthread);
651 PTHREAD_WAITQ_REMOVE(pthread);
652 /* Make the thread runnable: */
653 PTHREAD_SET_STATE(pthread, PS_RUNNING);
657 /* The signal handler is not called for threads in SIGWAIT. */
658 suppress_handler = 1;
659 /* Wake up the thread if the signal is blocked. */
660 if (sigismember(pthread->data.sigwait, sig)) {
661 /* Change the state of the thread to run: */
662 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
664 /* Return the signal number: */
665 pthread->signo = sig;
667 /* Increment the pending signal count. */
668 sigaddset(&pthread->sigpend, sig);
672 * The wait state is a special case due to the handling of
676 if (sig == SIGCHLD) {
677 /* Change the state of the thread to run: */
678 PTHREAD_WAITQ_REMOVE(pthread);
679 PTHREAD_SET_STATE(pthread, PS_RUNNING);
681 /* Return the signal number: */
682 pthread->signo = sig;
686 * Mark the thread as interrupted only if the
687 * restart flag is not set on the signal action:
690 pthread->interrupted = 1;
691 PTHREAD_WAITQ_REMOVE(pthread);
692 PTHREAD_SET_STATE(pthread, PS_RUNNING);
697 * States which cannot be interrupted but still require the
698 * signal handler to run:
703 * Remove the thread from the wait queue. It will
704 * be added back to the wait queue once all signal
705 * handlers have been invoked.
707 PTHREAD_WAITQ_REMOVE(pthread);
712 * Remove the thread from the wait queue. It will
713 * be added back to the wait queue once all signal
714 * handlers have been invoked.
716 PTHREAD_WAITQ_REMOVE(pthread);
717 /* Make the thread runnable: */
718 PTHREAD_SET_STATE(pthread, PS_RUNNING);
722 * States which are interruptible but may need to be removed
723 * from queues before any signal handler is called.
725 * XXX - We may not need to handle this condition, but will
726 * mark it as a potential problem.
732 pthread->interrupted = 1;
734 * Remove the thread from the wait queue. Our
735 * signal handler hook will remove this thread
736 * from the fd or file queue before invoking
737 * the actual handler.
739 PTHREAD_WAITQ_REMOVE(pthread);
743 * States which are interruptible:
749 * Flag the operation as interrupted and
750 * set the state to running:
752 pthread->interrupted = 1;
753 PTHREAD_SET_STATE(pthread, PS_RUNNING);
755 PTHREAD_WORKQ_REMOVE(pthread);
756 PTHREAD_WAITQ_REMOVE(pthread);
763 * Unmasked signals always cause poll, select, and sleep
764 * to terminate early, regardless of SA_RESTART:
766 pthread->interrupted = 1;
767 /* Remove threads in poll and select from the workq: */
768 if ((pthread->flags & PTHREAD_FLAGS_IN_WORKQ) != 0)
769 PTHREAD_WORKQ_REMOVE(pthread);
770 PTHREAD_WAITQ_REMOVE(pthread);
771 PTHREAD_SET_STATE(pthread, PS_RUNNING);
775 PTHREAD_WAITQ_REMOVE(pthread);
776 PTHREAD_SET_STATE(pthread, PS_RUNNING);
780 if (suppress_handler == 0) {
781 /* Setup a signal frame and save the current threads state: */
782 thread_sigframe_add(pthread, sig, has_args);
785 * Signals are deferred until just before the threads
786 * signal handler is invoked:
788 pthread->sig_defer_count = 1;
790 /* Make sure the thread is runnable: */
791 if (pthread->state != PS_RUNNING)
792 PTHREAD_SET_STATE(pthread, PS_RUNNING);
794 * The thread should be removed from all scheduling
795 * queues at this point. Raise the priority and place
796 * the thread in the run queue. It is also possible
797 * for a signal to be sent to a suspended thread,
798 * mostly via pthread_kill(). If a thread is suspended,
799 * don't insert it into the priority queue; just set
800 * its state to suspended and it will run the signal
801 * handler when it is resumed.
803 pthread->active_priority |= PTHREAD_SIGNAL_PRIORITY;
804 if ((pthread->flags & PTHREAD_FLAGS_SUSPENDED) != 0)
805 PTHREAD_SET_STATE(pthread, PS_SUSPENDED);
806 else if (thread_is_active == 0)
807 PTHREAD_PRIOQ_INSERT_TAIL(pthread);
812 thread_sig_check_state(struct pthread *pthread, int sig)
815 * Process according to thread state:
817 switch (pthread->state) {
819 * States which do not change when a signal is trapped:
834 /* Wake up the thread if the signal is blocked. */
835 if (sigismember(pthread->data.sigwait, sig)) {
836 /* Change the state of the thread to run: */
837 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
839 /* Return the signal number: */
840 pthread->signo = sig;
842 /* Increment the pending signal count. */
843 sigaddset(&pthread->sigpend, sig);
847 * The wait state is a special case due to the handling of
851 if (sig == SIGCHLD) {
853 * Remove the thread from the wait queue and
856 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
858 /* Return the signal number: */
859 pthread->signo = sig;
868 * Remove the thread from the wait queue and make it
871 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
873 /* Flag the operation as interrupted: */
874 pthread->interrupted = 1;
878 * These states are additionally in the work queue:
886 * Remove the thread from the wait and work queues, and
889 PTHREAD_WORKQ_REMOVE(pthread);
890 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
892 /* Flag the operation as interrupted: */
893 pthread->interrupted = 1;
899 * Send a signal to a specific thread (ala pthread_kill):
902 _thread_sig_send(struct pthread *pthread, int sig)
904 struct pthread *curthread = _get_curthread();
906 /* Check for signals whose actions are SIG_DFL: */
907 if (_thread_sigact[sig - 1].sa_handler == SIG_DFL) {
909 * Check to see if a temporary signal handler is
910 * installed for sigwaiters:
912 if (_thread_dfl_count[sig] == 0)
914 * Deliver the signal to the process if a handler
919 * Assuming we're still running after the above kill(),
920 * make any necessary state changes to the thread:
922 thread_sig_check_state(pthread, sig);
925 * Check that the signal is not being ignored:
927 else if (_thread_sigact[sig - 1].sa_handler != SIG_IGN) {
928 if (pthread->state == PS_SIGWAIT &&
929 sigismember(pthread->data.sigwait, sig)) {
930 /* Change the state of the thread to run: */
931 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
933 /* Return the signal number: */
934 pthread->signo = sig;
935 } else if (sigismember(&pthread->sigmask, sig))
936 /* Add the signal to the pending set: */
937 sigaddset(&pthread->sigpend, sig);
938 else if (pthread == curthread)
939 /* Call the signal handler for the current thread: */
940 thread_sig_invoke_handler(sig, NULL, NULL);
942 /* Protect the scheduling queues: */
943 _thread_kern_sig_defer();
945 * Perform any state changes due to signal
948 thread_sig_add(pthread, sig, /* has args */ 0);
949 /* Unprotect the scheduling queues: */
950 _thread_kern_sig_undefer();
956 * User thread signal handler wrapper.
958 * thread - current running thread
961 _thread_sig_wrapper(void)
963 struct pthread_signal_frame *psf;
964 struct pthread *thread = _get_curthread();
966 /* Get the current frame and state: */
967 psf = thread->curframe;
968 thread->curframe = NULL;
969 PTHREAD_ASSERT(psf != NULL, "Invalid signal frame in signal handler");
972 * We're coming from the kernel scheduler; clear the in
975 _thread_kern_in_sched = 0;
977 /* Check the threads previous state: */
978 if (psf->saved_state.psd_state != PS_RUNNING) {
980 * Do a little cleanup handling for those threads in
981 * queues before calling the signal handler. Signals
982 * for these threads are temporarily blocked until
983 * after cleanup handling.
985 switch (psf->saved_state.psd_state) {
988 _fd_lock_backout(thread);
989 psf->saved_state.psd_state = PS_RUNNING;
993 _cond_wait_backout(thread);
994 psf->saved_state.psd_state = PS_RUNNING;
998 _mutex_lock_backout(thread);
999 psf->saved_state.psd_state = PS_RUNNING;
1007 /* Unblock the signal in case we don't return from the handler: */
1008 _thread_sigq[psf->signo - 1].blocked = 0;
1011 * Lower the priority before calling the handler in case
1012 * it never returns (longjmps back):
1014 thread->active_priority &= ~PTHREAD_SIGNAL_PRIORITY;
1017 * Reenable interruptions without checking for the need to
1020 thread->sig_defer_count = 0;
1023 * Dispatch the signal via the custom signal handler:
1025 if (psf->sig_has_args == 0)
1026 thread_sig_invoke_handler(psf->signo, NULL, NULL);
1028 thread_sig_invoke_handler(psf->signo, &psf->siginfo, &psf->uc);
1031 * Call the kernel scheduler to safely restore the frame and
1032 * schedule the next thread:
1034 _thread_kern_sched_frame(psf);
1038 thread_sigframe_add(struct pthread *thread, int sig, int has_args)
1040 struct pthread_signal_frame *psf = NULL;
1041 unsigned long stackp;
1043 /* Get the top of the threads stack: */
1044 stackp = GET_STACK_JB(thread->ctx.jb);
1046 #if !defined(__ia64__)
1048 * Leave a little space on the stack and round down to the
1049 * nearest aligned word:
1051 #if defined(__amd64__)
1052 stackp -= 128; /* Skip over 128 byte red-zone */
1054 stackp -= sizeof(double);
1055 #if defined(__amd64__)
1062 /* Allocate room on top of the stack for a new signal frame: */
1063 stackp -= sizeof(struct pthread_signal_frame);
1064 #if defined(__ia64__) || defined(__amd64__)
1068 psf = (struct pthread_signal_frame *) stackp;
1070 /* Save the current context in the signal frame: */
1071 thread_sigframe_save(thread, psf);
1073 /* Set handler specific information: */
1074 psf->sig_has_args = has_args;
1077 /* Copy the signal handler arguments to the signal frame: */
1078 memcpy(&psf->uc, &_thread_sigq[psf->signo - 1].uc,
1080 memcpy(&psf->siginfo, &_thread_sigq[psf->signo - 1].siginfo,
1081 sizeof(psf->siginfo));
1084 /* Setup the signal mask: */
1085 SIGSETOR(thread->sigmask, _thread_sigact[sig - 1].sa_mask);
1086 sigaddset(&thread->sigmask, sig);
1088 /* Set up the new frame: */
1089 thread->curframe = psf;
1090 thread->flags &= PTHREAD_FLAGS_PRIVATE | PTHREAD_FLAGS_TRACE |
1091 PTHREAD_FLAGS_IN_SYNCQ;
1093 * Set up the context:
1095 #if !defined(__ia64__)
1096 stackp -= sizeof(double);
1097 #if defined(__amd64__)
1101 _setjmp(thread->ctx.jb);
1102 #if !defined(__ia64__)
1103 SET_STACK_JB(thread->ctx.jb, stackp);
1105 UPD_STACK_JB(thread->ctx.jb, stackp - 16);
1107 SET_RETURN_ADDR_JB(thread->ctx.jb, _thread_sig_wrapper);
1111 _thread_sigframe_restore(struct pthread *thread,
1112 struct pthread_signal_frame *psf)
1114 memcpy(&thread->ctx, &psf->ctx, sizeof(thread->ctx));
1116 * Only restore the signal mask if it hasn't been changed
1117 * by the application during invocation of the signal handler:
1119 if (thread->sigmask_seqno == psf->saved_state.psd_sigmask_seqno)
1120 thread->sigmask = psf->saved_state.psd_sigmask;
1121 thread->curframe = psf->saved_state.psd_curframe;
1122 thread->wakeup_time = psf->saved_state.psd_wakeup_time;
1123 thread->data = psf->saved_state.psd_wait_data;
1124 thread->state = psf->saved_state.psd_state;
1125 thread->flags = psf->saved_state.psd_flags;
1126 thread->interrupted = psf->saved_state.psd_interrupted;
1127 thread->signo = psf->saved_state.psd_signo;
1128 thread->sig_defer_count = psf->saved_state.psd_sig_defer_count;
1132 thread_sigframe_save(struct pthread *thread, struct pthread_signal_frame *psf)
1134 memcpy(&psf->ctx, &thread->ctx, sizeof(thread->ctx));
1135 psf->saved_state.psd_sigmask = thread->sigmask;
1136 psf->saved_state.psd_curframe = thread->curframe;
1137 psf->saved_state.psd_wakeup_time = thread->wakeup_time;
1138 psf->saved_state.psd_wait_data = thread->data;
1139 psf->saved_state.psd_state = thread->state;
1140 psf->saved_state.psd_flags = thread->flags &
1141 (PTHREAD_FLAGS_PRIVATE | PTHREAD_FLAGS_TRACE);
1142 psf->saved_state.psd_interrupted = thread->interrupted;
1143 psf->saved_state.psd_sigmask_seqno = thread->sigmask_seqno;
1144 psf->saved_state.psd_signo = thread->signo;
1145 psf->saved_state.psd_sig_defer_count = thread->sig_defer_count;