5
5
// This file implements runtime support for signal handling.
7
7
// Most synchronization primitives are not available from
8
// the signal handler (it cannot block and cannot use locks)
8
// the signal handler (it cannot block, allocate memory, or use locks)
9
9
// so the handler communicates with a processing goroutine
10
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.
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
40
28
#include "runtime.h"
46
34
uint32 mask[(NSIG+31)/32];
47
35
uint32 wanted[(NSIG+31)/32];
52
45
// Called from sighandler to send a signal back out of the signal handling thread.
54
47
runtime·sigsend(int32 s)
49
uint32 bit, mask, old, new;
58
51
if(!sig.inuse || s < 0 || s >= 32*nelem(sig.wanted) || !(sig.wanted[s/32]&(1U<<(s&31))))
65
58
if(runtime·cas(&sig.mask[s/32], mask, mask|bit)) {
67
60
// Only send a wakeup if the receiver needs a kick.
68
if(runtime·cas(&sig.kick, 1, 0))
69
runtime·notewakeup(&sig);
62
old = runtime·atomicload(&sig.state);
69
if(runtime·cas(&sig.state, old, new)) {
71
runtime·notewakeup(&sig);
97
// Check and update sig.state.
99
old = runtime·atomicload(&sig.state);
101
runtime·throw("inconsistent state in signal_recv");
106
if(runtime·cas(&sig.state, old, new)) {
107
if (new == HASWAITER) {
108
runtime·entersyscallblock();
109
runtime·notesleep(&sig);
110
runtime·exitsyscall();
111
runtime·noteclear(&sig);
92
117
// Get a new local copy.
93
// Ask for a kick if more signals come in
94
// during or after our check (before the sleep).
96
runtime·noteclear(&sig);
97
runtime·cas(&sig.kick, 0, 1);
101
118
for(i=0; i<nelem(sig.mask); i++) {
140
// Special case: want everything.
141
for(i=0; i<nelem(sig.wanted); i++)
142
sig.wanted[i] = ~(uint32)0;
143
runtime·sigenable(s);
147
145
if(s >= nelem(sig.wanted)*32)
149
147
sig.wanted[s/32] |= 1U<<(s&31);
150
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);