diff options
author | Ian Lance Taylor <iant@golang.org> | 2017-09-14 17:11:35 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2017-09-14 17:11:35 +0000 |
commit | bc998d034f45d1828a8663b2eed928faf22a7d01 (patch) | |
tree | 8d262a22ca7318f4bcd64269fe8fe9e45bcf8d0f /libgo/go/context | |
parent | a41a6142df74219f596e612d3a7775f68ca6e96f (diff) | |
download | gcc-bc998d034f45d1828a8663b2eed928faf22a7d01.zip gcc-bc998d034f45d1828a8663b2eed928faf22a7d01.tar.gz gcc-bc998d034f45d1828a8663b2eed928faf22a7d01.tar.bz2 |
libgo: update to go1.9
Reviewed-on: https://go-review.googlesource.com/63753
From-SVN: r252767
Diffstat (limited to 'libgo/go/context')
-rw-r--r-- | libgo/go/context/context.go | 40 | ||||
-rw-r--r-- | libgo/go/context/context_test.go | 2 |
2 files changed, 28 insertions, 14 deletions
diff --git a/libgo/go/context/context.go b/libgo/go/context/context.go index 0aa7c24..892ff27 100644 --- a/libgo/go/context/context.go +++ b/libgo/go/context/context.go @@ -96,10 +96,11 @@ type Context interface { // a Done channel for cancelation. Done() <-chan struct{} - // Err returns a non-nil error value after Done is closed. Err returns - // Canceled if the context was canceled or DeadlineExceeded if the - // context's deadline passed. No other values for Err are defined. - // After Done is closed, successive calls to Err return the same value. + // If Done is not yet closed, Err returns nil. + // If Done is closed, Err returns a non-nil error explaining why: + // Canceled if the context was canceled + // or DeadlineExceeded if the context's deadline passed. + // After Err returns a non-nil error, successive calls to Err return the same error. Err() error // Value returns the value associated with this context for key, or nil @@ -234,10 +235,7 @@ func WithCancel(parent Context) (ctx Context, cancel CancelFunc) { // newCancelCtx returns an initialized cancelCtx. func newCancelCtx(parent Context) cancelCtx { - return cancelCtx{ - Context: parent, - done: make(chan struct{}), - } + return cancelCtx{Context: parent} } // propagateCancel arranges for child to be canceled when parent is. @@ -306,20 +304,32 @@ type canceler interface { Done() <-chan struct{} } +// closedchan is a reusable closed channel. +var closedchan = make(chan struct{}) + +func init() { + close(closedchan) +} + // A cancelCtx can be canceled. When canceled, it also cancels any children // that implement canceler. type cancelCtx struct { Context - done chan struct{} // closed by the first cancel call. - - mu sync.Mutex + mu sync.Mutex // protects following fields + done chan struct{} // created lazily, closed by first cancel call children map[canceler]struct{} // set to nil by the first cancel call err error // set to non-nil by the first cancel call } func (c *cancelCtx) Done() <-chan struct{} { - return c.done + c.mu.Lock() + if c.done == nil { + c.done = make(chan struct{}) + } + d := c.done + c.mu.Unlock() + return d } func (c *cancelCtx) Err() error { @@ -344,7 +354,11 @@ func (c *cancelCtx) cancel(removeFromParent bool, err error) { return // already canceled } c.err = err - close(c.done) + if c.done == nil { + c.done = closedchan + } else { + close(c.done) + } for child := range c.children { // NOTE: acquiring the child's lock while holding parent's lock. child.cancel(false, err) diff --git a/libgo/go/context/context_test.go b/libgo/go/context/context_test.go index b5e599f..548476f 100644 --- a/libgo/go/context/context_test.go +++ b/libgo/go/context/context_test.go @@ -428,7 +428,7 @@ func XTestAllocs(t testingT, testingShort func() bool, testingAllocsPerRun func( limit := test.limit if runtime.Compiler == "gccgo" { // gccgo does not yet do escape analysis. - // TOOD(iant): Remove this when gccgo does do escape analysis. + // TODO(iant): Remove this when gccgo does do escape analysis. limit = test.gccgoLimit } numRuns := 100 |