1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // This file implements runtime support for signal handling.
7 // Most synchronization primitives are not available from
8 // the signal handler (it cannot block, allocate memory, or use locks)
9 // so the handler communicates with a processing goroutine
10 // via struct sig, below.
12 // sigsend() is called by the signal handler to queue a new signal.
13 // signal_recv() is called by the Go program to receive a newly queued signal.
14 // Synchronization between sigsend() and signal_recv() is based on the sig.state
15 // variable. It can be in 3 states: 0, HASWAITER and HASSIGNAL.
16 // HASWAITER means that signal_recv() is blocked on sig.Note and there are no
17 // new pending signals.
18 // HASSIGNAL means that sig.mask *may* contain new pending signals,
19 // signal_recv() can't be blocked in this state.
20 // 0 means that there are no new pending signals and signal_recv() is not blocked.
21 // Transitions between states are done atomically with CAS.
22 // When signal_recv() is unblocked, it resets sig.Note and rechecks sig.mask.
23 // If several sigsend()'s and signal_recv() execute concurrently, it can lead to
24 // unnecessary rechecks of sig.mask, but must not lead to missed signals
36 uint32 mask[(NSIG+31)/32];
37 uint32 wanted[(NSIG+31)/32];
47 // Called from sighandler to send a signal back out of the signal handling thread.
51 uint32 bit, mask, old, new;
53 if(!sig.inuse || s < 0 || (size_t)s >= 32*nelem(sig.wanted) || !(sig.wanted[s/32]&(1U<<(s&31))))
57 mask = sig.mask[s/32];
59 break; // signal already in queue
60 if(runtime_cas(&sig.mask[s/32], mask, mask|bit)) {
62 // Only send a wakeup if the receiver needs a kick.
64 old = runtime_atomicload(&sig.state);
71 if(runtime_cas(&sig.state, old, new)) {
73 runtime_notewakeup(&sig);
83 // Called to receive the next queued signal.
84 // Must only be called from a single goroutine at a time.
85 func signal_recv() (m uint32) {
86 static uint32 recv[nelem(sig.mask)];
90 // Serve from local copy if there are bits left.
91 for(i=0; i<NSIG; i++) {
92 if(recv[i/32]&(1U<<(i&31))) {
93 recv[i/32] ^= 1U<<(i&31);
99 // Check and update sig.state.
101 old = runtime_atomicload(&sig.state);
103 runtime_throw("inconsistent state in signal_recv");
108 if(runtime_cas(&sig.state, old, new)) {
109 if (new == HASWAITER) {
110 runtime_entersyscall();
111 runtime_notesleep(&sig);
112 runtime_exitsyscall();
113 runtime_noteclear(&sig);
119 // Get a new local copy.
120 for(i=0; (size_t)i<nelem(sig.mask); i++) {
123 if(runtime_cas(&sig.mask[i], m, 0))
131 // goc requires that we fall off the end of functions
132 // that return values instead of using our own return
136 // Must only be called from a single goroutine at a time.
137 func signal_enable(s uint32) {
141 // The first call to signal_enable is for us
142 // to use for initialization. It does not pass
143 // signal information in m.
144 sig.inuse = true; // enable reception of signals; cannot disable
145 runtime_noteclear(&sig);
150 // Special case: want everything.
151 for(i=0; (size_t)i<nelem(sig.wanted); i++)
152 sig.wanted[i] = ~(uint32)0;
153 runtime_sigenable(s);
157 if(s >= nelem(sig.wanted)*32)
159 sig.wanted[s/32] |= 1U<<(s&31);
160 runtime_sigenable(s);