Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
os/signal: selective signal handling
Restore package os/signal, with new API: Notify replaces Incoming, allowing clients to ask for certain signals only. Also, signals go to everyone who asks, not just one client. This could plausibly move into package os now that there are no magic side effects as a result of the import. Update runtime for new API: move common Unix signal handling code into signal_unix.c. (It's so easy to do this now that we don't have to edit Makefiles!) Tested on darwin,linux 386,amd64. Fixes #1266. R=r, dsymonds, bradfitz, iant, borman CC=golang-dev https://golang.org/cl/3749041
- Loading branch information
Showing
61 changed files
with
1,216 additions
and
1,266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2012 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// Assembly to get into package runtime without using exported symbols. | ||
|
||
#ifdef GOARCH_arm | ||
#define JMP B | ||
#endif | ||
|
||
TEXT ·signal_enable(SB),7,$0 | ||
JMP runtime·signal_enable(SB) | ||
|
||
TEXT ·signal_recv(SB),7,$0 | ||
JMP runtime·signal_recv(SB) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2012 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// Package signal implements access to incoming signals. | ||
package signal | ||
|
||
// BUG(rsc): This package is not yet implemented on Plan 9 and Windows. | ||
|
||
import ( | ||
"os" | ||
"sync" | ||
) | ||
|
||
var handlers struct { | ||
sync.Mutex | ||
list []handler | ||
} | ||
|
||
type handler struct { | ||
c chan<- os.Signal | ||
sig os.Signal | ||
all bool | ||
} | ||
|
||
// Notify causes package signal to relay incoming signals to c. | ||
// If no signals are listed, all incoming signals will be relayed to c. | ||
// Otherwise, just the listed signals will. | ||
// | ||
// Package signal will not block sending to c: the caller must ensure | ||
// that c has sufficient buffer space to keep up with the expected | ||
// signal rate. For a channel used for notification of just one signal value, | ||
// a buffer of size 1 is sufficient. | ||
// | ||
func Notify(c chan<- os.Signal, sig ...os.Signal) { | ||
if c == nil { | ||
panic("os/signal: Notify using nil channel") | ||
} | ||
|
||
handlers.Lock() | ||
defer handlers.Unlock() | ||
if len(sig) == 0 { | ||
enableSignal(nil) | ||
handlers.list = append(handlers.list, handler{c: c, all: true}) | ||
} else { | ||
for _, s := range sig { | ||
// We use nil as a special wildcard value for enableSignal, | ||
// so filter it out of the list of arguments. This is safe because | ||
// we will never get an incoming nil signal, so discarding the | ||
// registration cannot affect the observed behavior. | ||
if s != nil { | ||
enableSignal(s) | ||
handlers.list = append(handlers.list, handler{c: c, sig: s}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func process(sig os.Signal) { | ||
handlers.Lock() | ||
defer handlers.Unlock() | ||
|
||
for _, h := range handlers.list { | ||
if h.all || h.sig == sig { | ||
// send but do not block for it | ||
select { | ||
case h.c <- sig: | ||
default: | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.