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 siginit).
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.
57 // Called from sighandler to send a signal back out of the signal handling thread.
69 break; // signal already in queue
70 if(runtime_cas(&sig.mask, mask, mask|bit)) {
72 // Only send a wakeup for the first signal in each round.
81 // Called to receive a bitmask of queued signals.
82 func Sigrecv() (m uint32) {
83 // runtime·entersyscall();
85 // runtime·exitsyscall();
89 if(runtime_cas(&sig.mask, m, 0))
94 func Signame(sig int32) (name String) {
97 #if defined(HAVE_STRSIGNAL)
101 snprintf(buf, sizeof buf, "signal %d", sig);
104 int32 len = __builtin_strlen(s);
105 unsigned char *data = runtime_mallocgc(len, RefNoPointers, 0, 0);
106 __builtin_memcpy(data, s, len);
112 sig.inuse = true; // enable reception of signals; cannot disable