X-Git-Url: https://git.korewanetadesu.com/?p=go-signalcontext.git;a=blobdiff_plain;f=context.go;fp=context.go;h=36814173c766818da86475eb314b84f450eaf99d;hp=1bd24d317d105d42a87fa8e043ae677540631e92;hb=6d43b73af18d23a4cab88dccf8d93a8eb423d2ed;hpb=3ee8799a0bb3e7ba3628b83832fb14462241643d diff --git a/context.go b/context.go index 1bd24d3..3681417 100644 --- 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 - ctx.done = make(chan struct{}) if err := parent.Err(); err != nil { - close(ctx.done) + ctx.done = alreadyclosed 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...) @@ -103,3 +103,11 @@ func (s *Context) Err() error { 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) +}