OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / libgo / runtime / sigqueue.goc
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.
4
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.
11 //
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:
17 //
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.
24 //
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.)
29 //
30 // 3) Sigrecv uses cas to grab the current sig.mask and zero it,
31 // triggering the next round.
32 //
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.
38
39 package signal
40 #include "config.h"
41 #include "runtime.h"
42 #include "arch.h"
43 #include "malloc.h"
44 #include "defs.h"
45
46 static struct {
47         Note;
48         uint32 mask[(NSIG+31)/32];
49         uint32 wanted[(NSIG+31)/32];
50         uint32 kick;
51         bool inuse;
52 } sig;
53
54 // Called from sighandler to send a signal back out of the signal handling thread.
55 bool
56 __go_sigsend(int32 s)
57 {
58         uint32 bit, mask;
59
60         if(!sig.inuse || s < 0 || (size_t)s >= 32*nelem(sig.wanted) || !(sig.wanted[s/32]&(1U<<(s&31))))
61                 return false;
62         bit = 1 << (s&31);
63         for(;;) {
64                 mask = sig.mask[s/32];
65                 if(mask & bit)
66                         break;          // signal already in queue
67                 if(runtime_cas(&sig.mask[s/32], mask, mask|bit)) {
68                         // Added to queue.
69                         // Only send a wakeup if the receiver needs a kick.
70                         if(runtime_cas(&sig.kick, 1, 0))
71                                 runtime_notewakeup(&sig);
72                         break;
73                 }
74         }
75         return true;
76 }
77
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)];
82         int32 i, more;
83         
84         for(;;) {
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);
89                                 m = i;
90                                 goto done;
91                         }
92                 }
93
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).
97                 if(sig.kick == 0) {
98                         runtime_noteclear(&sig);
99                         runtime_cas(&sig.kick, 0, 1);
100                 }
101
102                 more = 0;
103                 for(i=0; (size_t)i<nelem(sig.mask); i++) {
104                         for(;;) {
105                                 m = sig.mask[i];
106                                 if(runtime_cas(&sig.mask[i], m, 0))
107                                         break;
108                         }
109                         recv[i] = m;
110                         if(m != 0)
111                                 more = 1;
112                 }
113                 if(more)
114                         continue;
115
116                 // Sleep waiting for more.
117                 runtime_entersyscall();
118                 runtime_notesleep(&sig);
119                 runtime_exitsyscall();
120         }
121
122 done:;
123         // goc requires that we fall off the end of functions
124         // that return values instead of using our own return
125         // statements.
126 }
127
128 // Must only be called from a single goroutine at a time.
129 func signal_enable(s uint32) {
130         int32 i;
131
132         if(!sig.inuse) {
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);
138                 return;
139         }
140         
141         if(~s == 0) {
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);
146                 return;
147         }
148
149         if(s >= nelem(sig.wanted)*32)
150                 return;
151         sig.wanted[s/32] |= 1U<<(s&31);
152         runtime_sigenable(s);
153 }