2013-02-11 Sebastian Huber <sebastian.huber@embedded-brains.de>
[official-gcc.git] / libgo / runtime / sigqueue.goc
blob82b0400d6498c8a237076c741256bada490be3ea
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, 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
25 // nor deadlocks.
27 package signal
28 #include "config.h"
29 #include "runtime.h"
30 #include "arch.h"
31 #include "malloc.h"
32 #include "defs.h"
34 static struct {
35         Note;
36         uint32 mask[(NSIG+31)/32];
37         uint32 wanted[(NSIG+31)/32];
38         uint32 state;
39         bool inuse;
40 } sig;
42 enum {
43         HASWAITER = 1,
44         HASSIGNAL = 2,
47 // Called from sighandler to send a signal back out of the signal handling thread.
48 bool
49 __go_sigsend(int32 s)
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))))
54                 return false;
55         bit = 1 << (s&31);
56         for(;;) {
57                 mask = sig.mask[s/32];
58                 if(mask & bit)
59                         break;          // signal already in queue
60                 if(runtime_cas(&sig.mask[s/32], mask, mask|bit)) {
61                         // Added to queue.
62                         // Only send a wakeup if the receiver needs a kick.
63                         for(;;) {
64                                 old = runtime_atomicload(&sig.state);
65                                 if(old == HASSIGNAL)
66                                         break;
67                                 if(old == HASWAITER)
68                                         new = 0;
69                                 else  // if(old == 0)
70                                         new = HASSIGNAL;
71                                 if(runtime_cas(&sig.state, old, new)) {
72                                         if (old == HASWAITER)
73                                                 runtime_notewakeup(&sig);
74                                         break;
75                                 }
76                         }
77                         break;
78                 }
79         }
80         return true;
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)];
87         uint32 i, old, new;
88         
89         for(;;) {
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);
94                                 m = i;
95                                 goto done;
96                         }
97                 }
99                 // Check and update sig.state.
100                 for(;;) {
101                         old = runtime_atomicload(&sig.state);
102                         if(old == HASWAITER)
103                                 runtime_throw("inconsistent state in signal_recv");
104                         if(old == HASSIGNAL)
105                                 new = 0;
106                         else  // if(old == 0)
107                                 new = HASWAITER;
108                         if(runtime_cas(&sig.state, old, new)) {
109                                 if (new == HASWAITER) {
110                                         runtime_entersyscall();
111                                         runtime_notesleep(&sig);
112                                         runtime_exitsyscall();
113                                         runtime_noteclear(&sig);
114                                 }
115                                 break;
116                         }
117                 }
119                 // Get a new local copy.
120                 for(i=0; (size_t)i<nelem(sig.mask); i++) {
121                         for(;;) {
122                                 m = sig.mask[i];
123                                 if(runtime_cas(&sig.mask[i], m, 0))
124                                         break;
125                         }
126                         recv[i] = m;
127                 }
128         }
130 done:;
131         // goc requires that we fall off the end of functions
132         // that return values instead of using our own return
133         // statements.
136 // Must only be called from a single goroutine at a time.
137 func signal_enable(s uint32) {
138         int32 i;
140         if(!sig.inuse) {
141                 // The first call to signal_enable is for us
142                 // to use for initialization.  It does not pass
143                 // signal information in m.
144                 sig.inuse = true;       // enable reception of signals; cannot disable
145                 runtime_noteclear(&sig);
146                 return;
147         }
148         
149         if(~s == 0) {
150                 // Special case: want everything.
151                 for(i=0; (size_t)i<nelem(sig.wanted); i++)
152                         sig.wanted[i] = ~(uint32)0;
153                 runtime_sigenable(s);
154                 return;
155         }
157         if(s >= nelem(sig.wanted)*32)
158                 return;
159         sig.wanted[s/32] |= 1U<<(s&31);
160         runtime_sigenable(s);