projects
/
go-signalcontext.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Reuse the same channel for all precompleted contexts
[go-signalcontext.git]
/
context.go
diff --git
a/context.go
b/context.go
index
1bd24d3
..
3681417
100644
(file)
--- a/
context.go
+++ b/
context.go
@@
-28,14
+28,14
@@
type Context struct {
func UntilSignal(parent context.Context, sig ...os.Signal) *Context {
ctx := new(Context)
ctx.parent = parent
func UntilSignal(parent context.Context, sig ...os.Signal) *Context {
ctx := new(Context)
ctx.parent = parent
- ctx.done = make(chan struct{})
if err := parent.Err(); err != nil {
if err := parent.Err(); err != nil {
- c
lose(ctx.done)
+ c
tx.done = alreadyclosed
ctx.err = err
return ctx
}
ctx.err = err
return ctx
}
+ ctx.done = make(chan struct{})
ctx.c = make(chan os.Signal, 1)
signal.Notify(ctx.c, sig...)
go ctx.wait(sig...)
ctx.c = make(chan os.Signal, 1)
signal.Notify(ctx.c, sig...)
go ctx.wait(sig...)
@@
-103,3
+103,11
@@
func (s *Context) Err() error {
s.m.Unlock()
return err
}
s.m.Unlock()
return err
}
+
+// Reuse the same channel for all contexts which begin life already
+// completed.
+var alreadyclosed = make(chan struct{})
+
+func init() {
+ close(alreadyclosed)
+}