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 and cannot use locks)
9 // so the handler communicates with a processing goroutine
10 // via struct sig, below.
12 // Ownership for sig.Note passes back and forth between
13 // the signal handler and the signal goroutine in rounds.
14 // The initial state is that sig.note is cleared (setup by signal_enable).
15 // At the beginning of each round, mask == 0.
16 // The round goes through three stages:
19 // 1a) One or more signals arrive and are handled
20 // by sigsend using cas to set bits in sig.mask.
21 // The handler that changes sig.mask from zero to non-zero
22 // calls notewakeup(&sig).
23 // 1b) Sigrecv calls notesleep(&sig) to wait for the wakeup.
25 // 2) Having received the wakeup, sigrecv knows that sigsend
26 // will not send another wakeup, so it can noteclear(&sig)
27 // to prepare for the next round. (Sigsend may still be adding
28 // signals to sig.mask at this point, which is fine.)
30 // 3) Sigrecv uses cas to grab the current sig.mask and zero it,
31 // triggering the next round.
33 // The signal handler takes ownership of the note by atomically
34 // changing mask from a zero to non-zero value. It gives up
35 // ownership by calling notewakeup. The signal goroutine takes
36 // ownership by returning from notesleep (caused by the notewakeup)
37 // and gives up ownership by clearing mask.
48 uint32 mask[(NSIG+31)/32];
49 uint32 wanted[(NSIG+31)/32];
54 // Called from sighandler to send a signal back out of the signal handling thread.
60 if(!sig.inuse || s < 0 || (size_t)s >= 32*nelem(sig.wanted) || !(sig.wanted[s/32]&(1U<<(s&31))))
64 mask = sig.mask[s/32];
66 break; // signal already in queue
67 if(runtime_cas(&sig.mask[s/32], mask, mask|bit)) {
69 // Only send a wakeup if the receiver needs a kick.
70 if(runtime_cas(&sig.kick, 1, 0))
71 runtime_notewakeup(&sig);
78 // Called to receive the next queued signal.
79 // Must only be called from a single goroutine at a time.
80 func signal_recv() (m uint32) {
81 static uint32 recv[nelem(sig.mask)];
85 // Serve from local copy if there are bits left.
86 for(i=0; i<NSIG; i++) {
87 if(recv[i/32]&(1U<<(i&31))) {
88 recv[i/32] ^= 1U<<(i&31);
94 // Get a new local copy.
95 // Ask for a kick if more signals come in
96 // during or after our check (before the sleep).
98 runtime_noteclear(&sig);
99 runtime_cas(&sig.kick, 0, 1);
103 for(i=0; (size_t)i<nelem(sig.mask); i++) {
106 if(runtime_cas(&sig.mask[i], m, 0))
116 // Sleep waiting for more.
117 runtime_entersyscall();
118 runtime_notesleep(&sig);
119 runtime_exitsyscall();
123 // goc requires that we fall off the end of functions
124 // that return values instead of using our own return
128 // Must only be called from a single goroutine at a time.
129 func signal_enable(s uint32) {
133 // The first call to signal_enable is for us
134 // to use for initialization. It does not pass
135 // signal information in m.
136 sig.inuse = true; // enable reception of signals; cannot disable
137 runtime_noteclear(&sig);
142 // Special case: want everything.
143 for(i=0; (size_t)i<nelem(sig.wanted); i++)
144 sig.wanted[i] = ~(uint32)0;
145 runtime_sigenable(s);
149 if(s >= nelem(sig.wanted)*32)
151 sig.wanted[s/32] |= 1U<<(s&31);
152 runtime_sigenable(s);