github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/src/runtime/sigqueue.go (about) 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, allocate memory, or use locks) 9 // so the handler communicates with a processing goroutine 10 // via struct sig, below. 11 // 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: sigIdle, sigReceiving and sigSending. 16 // sigReceiving means that signal_recv is blocked on sig.Note and there are no 17 // new pending signals. 18 // sigSending means that sig.mask *may* contain new pending signals, 19 // signal_recv can't be blocked in this state. 20 // sigIdle 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 sigsends and signal_recv execute concurrently, it can lead to 24 // unnecessary rechecks of sig.mask, but it cannot lead to missed signals 25 // nor deadlocks. 26 27 // +build !plan9 28 29 package runtime 30 31 import "unsafe" 32 33 var sig struct { 34 note note 35 mask [(_NSIG + 31) / 32]uint32 36 wanted [(_NSIG + 31) / 32]uint32 37 recv [(_NSIG + 31) / 32]uint32 38 state uint32 39 inuse bool 40 } 41 42 const ( 43 sigIdle = iota 44 sigReceiving 45 sigSending 46 ) 47 48 // Called from sighandler to send a signal back out of the signal handling thread. 49 // Reports whether the signal was sent. If not, the caller typically crashes the program. 50 func sigsend(s uint32) bool { 51 bit := uint32(1) << uint(s&31) 52 if !sig.inuse || s >= uint32(32*len(sig.wanted)) || sig.wanted[s/32]&bit == 0 { 53 return false 54 } 55 56 // Add signal to outgoing queue. 57 for { 58 mask := sig.mask[s/32] 59 if mask&bit != 0 { 60 return true // signal already in queue 61 } 62 if cas(&sig.mask[s/32], mask, mask|bit) { 63 break 64 } 65 } 66 67 // Notify receiver that queue has new bit. 68 Send: 69 for { 70 switch atomicload(&sig.state) { 71 default: 72 throw("sigsend: inconsistent state") 73 case sigIdle: 74 if cas(&sig.state, sigIdle, sigSending) { 75 break Send 76 } 77 case sigSending: 78 // notification already pending 79 break Send 80 case sigReceiving: 81 if cas(&sig.state, sigReceiving, sigIdle) { 82 notewakeup(&sig.note) 83 break Send 84 } 85 } 86 } 87 88 return true 89 } 90 91 // Called to receive the next queued signal. 92 // Must only be called from a single goroutine at a time. 93 //go:linkname signal_recv os/signal.signal_recv 94 func signal_recv() uint32 { 95 for { 96 // Serve any signals from local copy. 97 for i := uint32(0); i < _NSIG; i++ { 98 if sig.recv[i/32]&(1<<(i&31)) != 0 { 99 sig.recv[i/32] &^= 1 << (i & 31) 100 return i 101 } 102 } 103 104 // Wait for updates to be available from signal sender. 105 Receive: 106 for { 107 switch atomicload(&sig.state) { 108 default: 109 throw("signal_recv: inconsistent state") 110 case sigIdle: 111 if cas(&sig.state, sigIdle, sigReceiving) { 112 notetsleepg(&sig.note, -1) 113 noteclear(&sig.note) 114 break Receive 115 } 116 case sigSending: 117 if cas(&sig.state, sigSending, sigIdle) { 118 break Receive 119 } 120 } 121 } 122 123 // Incorporate updates from sender into local copy. 124 for i := range sig.mask { 125 sig.recv[i] = xchg(&sig.mask[i], 0) 126 } 127 } 128 } 129 130 // Must only be called from a single goroutine at a time. 131 //go:linkname signal_enable os/signal.signal_enable 132 func signal_enable(s uint32) { 133 if !sig.inuse { 134 // The first call to signal_enable is for us 135 // to use for initialization. It does not pass 136 // signal information in m. 137 sig.inuse = true // enable reception of signals; cannot disable 138 noteclear(&sig.note) 139 return 140 } 141 142 if s >= uint32(len(sig.wanted)*32) { 143 return 144 } 145 sig.wanted[s/32] |= 1 << (s & 31) 146 sigenable(s) 147 } 148 149 // Must only be called from a single goroutine at a time. 150 //go:linkname signal_disable os/signal.signal_disable 151 func signal_disable(s uint32) { 152 if s >= uint32(len(sig.wanted)*32) { 153 return 154 } 155 sig.wanted[s/32] &^= 1 << (s & 31) 156 sigdisable(s) 157 } 158 159 // Must only be called from a single goroutine at a time. 160 //go:linkname signal_ignore os/signal.signal_ignore 161 func signal_ignore(s uint32) { 162 if s >= uint32(len(sig.wanted)*32) { 163 return 164 } 165 sig.wanted[s/32] &^= 1 << (s & 31) 166 sigignore(s) 167 } 168 169 // This runs on a foreign stack, without an m or a g. No stack split. 170 //go:nosplit 171 //go:norace 172 func badsignal(sig uintptr) { 173 cgocallback(unsafe.Pointer(funcPC(badsignalgo)), noescape(unsafe.Pointer(&sig)), unsafe.Sizeof(sig)) 174 } 175 176 func badsignalgo(sig uintptr) { 177 if !sigsend(uint32(sig)) { 178 // A foreign thread received the signal sig, and the 179 // Go code does not want to handle it. 180 raisebadsignal(int32(sig)) 181 } 182 }