OSDN Git Service

Rework locking code to split stack much less.
[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 siginit).
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 runtime
40 #include "config.h"
41 #include "runtime.h"
42 #include "malloc.h"
43 #include "defs.h"
44
45 static struct {
46         Note;
47         uint32 mask;
48         bool inuse;
49 } sig;
50
51 void
52 siginit(void)
53 {
54         noteclear(&sig);
55 }
56
57 // Called from sighandler to send a signal back out of the signal handling thread.
58 bool
59 __go_sigsend(int32 s)
60 {
61         uint32 bit, mask;
62
63         if(!sig.inuse)
64                 return false;
65         bit = 1 << s;
66         for(;;) {
67                 mask = sig.mask;
68                 if(mask & bit)
69                         break;          // signal already in queue
70                 if(runtime_cas(&sig.mask, mask, mask|bit)) {
71                         // Added to queue.
72                         // Only send a wakeup for the first signal in each round.
73                         if(mask == 0)
74                                 notewakeup(&sig);
75                         break;
76                 }
77         }
78         return true;
79 }
80
81 // Called to receive a bitmask of queued signals.
82 func Sigrecv() (m uint32) {
83         // runtime·entersyscall();
84         notesleep(&sig);
85         // runtime·exitsyscall();
86         noteclear(&sig);
87         for(;;) {
88                 m = sig.mask;
89                 if(runtime_cas(&sig.mask, m, 0))
90                         break;
91         }
92 }
93
94 func Signame(sig int32) (name String) {
95         const char* s = NULL;
96         char buf[100];
97 #if defined(HAVE_STRSIGNAL)
98         s = strsignal(sig);
99 #endif
100         if (s == NULL) {
101                 snprintf(buf, sizeof buf, "signal %d", sig);
102                 s = buf;
103         }
104         int32 len = __builtin_strlen(s);
105         unsigned char *data = runtime_mallocgc(len, RefNoPointers, 0, 0);
106         __builtin_memcpy(data, s, len);
107         name.__data = data;
108         name.__length = len;
109 }
110
111 func Siginit() {
112         sig.inuse = true;       // enable reception of signals; cannot disable
113 }