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_notetsleepg(&sig, -1);
111 runtime_noteclear(&sig);
117 // Get a new local copy.
118 for(i=0; (size_t)i<nelem(sig.mask); i++) {
121 if(runtime_cas(&sig.mask[i], m, 0))
129 // goc requires that we fall off the end of functions
130 // that return values instead of using our own return
134 // Must only be called from a single goroutine at a time.
135 func signal_enable(s uint32) {
137 // The first call to signal_enable is for us
138 // to use for initialization. It does not pass
139 // signal information in m.
140 sig.inuse = true; // enable reception of signals; cannot disable
141 runtime_noteclear(&sig);
145 if(s >= nelem(sig.wanted)*32)
147 sig.wanted[s/32] |= 1U<<(s&31);
148 runtime_sigenable(s);
151 // Must only be called from a single goroutine at a time.
152 func signal_disable(s uint32) {
153 if(s >= nelem(sig.wanted)*32)
155 sig.wanted[s/32] &= ~(1U<<(s&31));
156 runtime_sigdisable(s);
159 // Must only be called from a single goroutine at a time.
160 func signal_ignore(s uint32) {
161 if (s >= nelem(sig.wanted)*32)
163 sig.wanted[s/32] &= ~(1U<<(s&31));
164 runtime_sigignore(s);
167 // This runs on a foreign stack, without an m or a g. No stack split.
169 runtime_badsignal(int sig)