2012-01-13 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libgo / runtime / sigqueue.goc
blobe91571902c09e373e15a9380dea865bf3b42e8bc
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.
6 //
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:
18 // (In parallel)
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.
39 package runtime
40 #include "config.h"
41 #include "runtime.h"
42 #include "arch.h"
43 #include "malloc.h"
44 #include "defs.h"
46 static struct {
47         Note;
48         uint32 mask;
49         bool inuse;
50 } sig;
52 void
53 siginit(void)
55         runtime_noteclear(&sig);
58 // Called from sighandler to send a signal back out of the signal handling thread.
59 bool
60 __go_sigsend(int32 s)
62         uint32 bit, mask;
64         if(!sig.inuse)
65                 return false;
66         bit = 1 << s;
67         for(;;) {
68                 mask = sig.mask;
69                 if(mask & bit)
70                         break;          // signal already in queue
71                 if(runtime_cas(&sig.mask, mask, mask|bit)) {
72                         // Added to queue.
73                         // Only send a wakeup for the first signal in each round.
74                         if(mask == 0)
75                                 runtime_notewakeup(&sig);
76                         break;
77                 }
78         }
79         return true;
82 // Called to receive a bitmask of queued signals.
83 func Sigrecv() (m uint32) {
84         runtime_entersyscall();
85         runtime_notesleep(&sig);
86         runtime_exitsyscall();
87         runtime_noteclear(&sig);
88         for(;;) {
89                 m = sig.mask;
90                 if(runtime_cas(&sig.mask, m, 0))
91                         break;
92         }
95 func Signame(sig int32) (name String) {
96         const char* s = NULL;
97         char buf[100];
98 #if defined(HAVE_STRSIGNAL)
99         s = strsignal(sig);
100 #endif
101         if (s == NULL) {
102                 snprintf(buf, sizeof buf, "signal %d", sig);
103                 s = buf;
104         }
105         int32 len = __builtin_strlen(s);
106         unsigned char *data = runtime_mallocgc(len, FlagNoPointers, 0, 0);
107         __builtin_memcpy(data, s, len);
108         name.__data = data;
109         name.__length = len;
112 func Siginit() {
113         runtime_initsig(SigQueue);
114         sig.inuse = true;       // enable reception of signals; cannot disable