cubefs
1// Copyright 2014 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//go:build !go1.7
6// +build !go1.7
7
8package context9
10import (11"errors"12"fmt"13"sync"14"time"15)
16
17// An emptyCtx is never canceled, has no values, and has no deadline. It is not
18// struct{}, since vars of this type must have distinct addresses.
19type emptyCtx int20
21func (*emptyCtx) Deadline() (deadline time.Time, ok bool) {22return23}
24
25func (*emptyCtx) Done() <-chan struct{} {26return nil27}
28
29func (*emptyCtx) Err() error {30return nil31}
32
33func (*emptyCtx) Value(key interface{}) interface{} {34return nil35}
36
37func (e *emptyCtx) String() string {38switch e {39case background:40return "context.Background"41case todo:42return "context.TODO"43}44return "unknown empty Context"45}
46
47var (48background = new(emptyCtx)49todo = new(emptyCtx)50)
51
52// Canceled is the error returned by Context.Err when the context is canceled.
53var Canceled = errors.New("context canceled")54
55// DeadlineExceeded is the error returned by Context.Err when the context's
56// deadline passes.
57var DeadlineExceeded = errors.New("context deadline exceeded")58
59// WithCancel returns a copy of parent with a new Done channel. The returned
60// context's Done channel is closed when the returned cancel function is called
61// or when the parent context's Done channel is closed, whichever happens first.
62//
63// Canceling this context releases resources associated with it, so code should
64// call cancel as soon as the operations running in this Context complete.
65func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {66c := newCancelCtx(parent)67propagateCancel(parent, c)68return c, func() { c.cancel(true, Canceled) }69}
70
71// newCancelCtx returns an initialized cancelCtx.
72func newCancelCtx(parent Context) *cancelCtx {73return &cancelCtx{74Context: parent,75done: make(chan struct{}),76}77}
78
79// propagateCancel arranges for child to be canceled when parent is.
80func propagateCancel(parent Context, child canceler) {81if parent.Done() == nil {82return // parent is never canceled83}84if p, ok := parentCancelCtx(parent); ok {85p.mu.Lock()86if p.err != nil {87// parent has already been canceled88child.cancel(false, p.err)89} else {90if p.children == nil {91p.children = make(map[canceler]bool)92}93p.children[child] = true94}95p.mu.Unlock()96} else {97go func() {98select {99case <-parent.Done():100child.cancel(false, parent.Err())101case <-child.Done():102}103}()104}105}
106
107// parentCancelCtx follows a chain of parent references until it finds a
108// *cancelCtx. This function understands how each of the concrete types in this
109// package represents its parent.
110func parentCancelCtx(parent Context) (*cancelCtx, bool) {111for {112switch c := parent.(type) {113case *cancelCtx:114return c, true115case *timerCtx:116return c.cancelCtx, true117case *valueCtx:118parent = c.Context119default:120return nil, false121}122}123}
124
125// removeChild removes a context from its parent.
126func removeChild(parent Context, child canceler) {127p, ok := parentCancelCtx(parent)128if !ok {129return130}131p.mu.Lock()132if p.children != nil {133delete(p.children, child)134}135p.mu.Unlock()136}
137
138// A canceler is a context type that can be canceled directly. The
139// implementations are *cancelCtx and *timerCtx.
140type canceler interface {141cancel(removeFromParent bool, err error)142Done() <-chan struct{}143}
144
145// A cancelCtx can be canceled. When canceled, it also cancels any children
146// that implement canceler.
147type cancelCtx struct {148Context
149
150done chan struct{} // closed by the first cancel call.151
152mu sync.Mutex153children map[canceler]bool // set to nil by the first cancel call154err error // set to non-nil by the first cancel call155}
156
157func (c *cancelCtx) Done() <-chan struct{} {158return c.done159}
160
161func (c *cancelCtx) Err() error {162c.mu.Lock()163defer c.mu.Unlock()164return c.err165}
166
167func (c *cancelCtx) String() string {168return fmt.Sprintf("%v.WithCancel", c.Context)169}
170
171// cancel closes c.done, cancels each of c's children, and, if
172// removeFromParent is true, removes c from its parent's children.
173func (c *cancelCtx) cancel(removeFromParent bool, err error) {174if err == nil {175panic("context: internal error: missing cancel error")176}177c.mu.Lock()178if c.err != nil {179c.mu.Unlock()180return // already canceled181}182c.err = err183close(c.done)184for child := range c.children {185// NOTE: acquiring the child's lock while holding parent's lock.186child.cancel(false, err)187}188c.children = nil189c.mu.Unlock()190
191if removeFromParent {192removeChild(c.Context, c)193}194}
195
196// WithDeadline returns a copy of the parent context with the deadline adjusted
197// to be no later than d. If the parent's deadline is already earlier than d,
198// WithDeadline(parent, d) is semantically equivalent to parent. The returned
199// context's Done channel is closed when the deadline expires, when the returned
200// cancel function is called, or when the parent context's Done channel is
201// closed, whichever happens first.
202//
203// Canceling this context releases resources associated with it, so code should
204// call cancel as soon as the operations running in this Context complete.
205func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) {206if cur, ok := parent.Deadline(); ok && cur.Before(deadline) {207// The current deadline is already sooner than the new one.208return WithCancel(parent)209}210c := &timerCtx{211cancelCtx: newCancelCtx(parent),212deadline: deadline,213}214propagateCancel(parent, c)215d := deadline.Sub(time.Now())216if d <= 0 {217c.cancel(true, DeadlineExceeded) // deadline has already passed218return c, func() { c.cancel(true, Canceled) }219}220c.mu.Lock()221defer c.mu.Unlock()222if c.err == nil {223c.timer = time.AfterFunc(d, func() {224c.cancel(true, DeadlineExceeded)225})226}227return c, func() { c.cancel(true, Canceled) }228}
229
230// A timerCtx carries a timer and a deadline. It embeds a cancelCtx to
231// implement Done and Err. It implements cancel by stopping its timer then
232// delegating to cancelCtx.cancel.
233type timerCtx struct {234*cancelCtx235timer *time.Timer // Under cancelCtx.mu.236
237deadline time.Time238}
239
240func (c *timerCtx) Deadline() (deadline time.Time, ok bool) {241return c.deadline, true242}
243
244func (c *timerCtx) String() string {245return fmt.Sprintf("%v.WithDeadline(%s [%s])", c.cancelCtx.Context, c.deadline, c.deadline.Sub(time.Now()))246}
247
248func (c *timerCtx) cancel(removeFromParent bool, err error) {249c.cancelCtx.cancel(false, err)250if removeFromParent {251// Remove this timerCtx from its parent cancelCtx's children.252removeChild(c.cancelCtx.Context, c)253}254c.mu.Lock()255if c.timer != nil {256c.timer.Stop()257c.timer = nil258}259c.mu.Unlock()260}
261
262// WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)).
263//
264// Canceling this context releases resources associated with it, so code should
265// call cancel as soon as the operations running in this Context complete:
266//
267// func slowOperationWithTimeout(ctx context.Context) (Result, error) {
268// ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
269// defer cancel() // releases resources if slowOperation completes before timeout elapses
270// return slowOperation(ctx)
271// }
272func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {273return WithDeadline(parent, time.Now().Add(timeout))274}
275
276// WithValue returns a copy of parent in which the value associated with key is
277// val.
278//
279// Use context Values only for request-scoped data that transits processes and
280// APIs, not for passing optional parameters to functions.
281func WithValue(parent Context, key interface{}, val interface{}) Context {282return &valueCtx{parent, key, val}283}
284
285// A valueCtx carries a key-value pair. It implements Value for that key and
286// delegates all other calls to the embedded Context.
287type valueCtx struct {288Context
289key, val interface{}290}
291
292func (c *valueCtx) String() string {293return fmt.Sprintf("%v.WithValue(%#v, %#v)", c.Context, c.key, c.val)294}
295
296func (c *valueCtx) Value(key interface{}) interface{} {297if c.key == key {298return c.val299}300return c.Context.Value(key)301}
302