[5/77] Small tweak to array_value_type
[official-gcc.git] / libgo / go / context / context.go
blob0aa7c24df9a0343b6fa983ed1efe92bcc4f5a808
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.
5 // Package context defines the Context type, which carries deadlines,
6 // cancelation signals, and other request-scoped values across API boundaries
7 // and between processes.
8 //
9 // Incoming requests to a server should create a Context, and outgoing
10 // calls to servers should accept a Context. The chain of function
11 // calls between them must propagate the Context, optionally replacing
12 // it with a derived Context created using WithCancel, WithDeadline,
13 // WithTimeout, or WithValue. When a Context is canceled, all
14 // Contexts derived from it are also canceled.
16 // The WithCancel, WithDeadline, and WithTimeout functions take a
17 // Context (the parent) and return a derived Context (the child) and a
18 // CancelFunc. Calling the CancelFunc cancels the child and its
19 // children, removes the parent's reference to the child, and stops
20 // any associated timers. Failing to call the CancelFunc leaks the
21 // child and its children until the parent is canceled or the timer
22 // fires. The go vet tool checks that CancelFuncs are used on all
23 // control-flow paths.
25 // Programs that use Contexts should follow these rules to keep interfaces
26 // consistent across packages and enable static analysis tools to check context
27 // propagation:
29 // Do not store Contexts inside a struct type; instead, pass a Context
30 // explicitly to each function that needs it. The Context should be the first
31 // parameter, typically named ctx:
33 // func DoSomething(ctx context.Context, arg Arg) error {
34 // // ... use ctx ...
35 // }
37 // Do not pass a nil Context, even if a function permits it. Pass context.TODO
38 // if you are unsure about which Context to use.
40 // Use context Values only for request-scoped data that transits processes and
41 // APIs, not for passing optional parameters to functions.
43 // The same Context may be passed to functions running in different goroutines;
44 // Contexts are safe for simultaneous use by multiple goroutines.
46 // See https://blog.golang.org/context for example code for a server that uses
47 // Contexts.
48 package context
50 import (
51 "errors"
52 "fmt"
53 "reflect"
54 "sync"
55 "time"
58 // A Context carries a deadline, a cancelation signal, and other values across
59 // API boundaries.
61 // Context's methods may be called by multiple goroutines simultaneously.
62 type Context interface {
63 // Deadline returns the time when work done on behalf of this context
64 // should be canceled. Deadline returns ok==false when no deadline is
65 // set. Successive calls to Deadline return the same results.
66 Deadline() (deadline time.Time, ok bool)
68 // Done returns a channel that's closed when work done on behalf of this
69 // context should be canceled. Done may return nil if this context can
70 // never be canceled. Successive calls to Done return the same value.
72 // WithCancel arranges for Done to be closed when cancel is called;
73 // WithDeadline arranges for Done to be closed when the deadline
74 // expires; WithTimeout arranges for Done to be closed when the timeout
75 // elapses.
77 // Done is provided for use in select statements:
79 // // Stream generates values with DoSomething and sends them to out
80 // // until DoSomething returns an error or ctx.Done is closed.
81 // func Stream(ctx context.Context, out chan<- Value) error {
82 // for {
83 // v, err := DoSomething(ctx)
84 // if err != nil {
85 // return err
86 // }
87 // select {
88 // case <-ctx.Done():
89 // return ctx.Err()
90 // case out <- v:
91 // }
92 // }
93 // }
95 // See https://blog.golang.org/pipelines for more examples of how to use
96 // a Done channel for cancelation.
97 Done() <-chan struct{}
99 // Err returns a non-nil error value after Done is closed. Err returns
100 // Canceled if the context was canceled or DeadlineExceeded if the
101 // context's deadline passed. No other values for Err are defined.
102 // After Done is closed, successive calls to Err return the same value.
103 Err() error
105 // Value returns the value associated with this context for key, or nil
106 // if no value is associated with key. Successive calls to Value with
107 // the same key returns the same result.
109 // Use context values only for request-scoped data that transits
110 // processes and API boundaries, not for passing optional parameters to
111 // functions.
113 // A key identifies a specific value in a Context. Functions that wish
114 // to store values in Context typically allocate a key in a global
115 // variable then use that key as the argument to context.WithValue and
116 // Context.Value. A key can be any type that supports equality;
117 // packages should define keys as an unexported type to avoid
118 // collisions.
120 // Packages that define a Context key should provide type-safe accessors
121 // for the values stored using that key:
123 // // Package user defines a User type that's stored in Contexts.
124 // package user
126 // import "context"
128 // // User is the type of value stored in the Contexts.
129 // type User struct {...}
131 // // key is an unexported type for keys defined in this package.
132 // // This prevents collisions with keys defined in other packages.
133 // type key int
135 // // userKey is the key for user.User values in Contexts. It is
136 // // unexported; clients use user.NewContext and user.FromContext
137 // // instead of using this key directly.
138 // var userKey key = 0
140 // // NewContext returns a new Context that carries value u.
141 // func NewContext(ctx context.Context, u *User) context.Context {
142 // return context.WithValue(ctx, userKey, u)
143 // }
145 // // FromContext returns the User value stored in ctx, if any.
146 // func FromContext(ctx context.Context) (*User, bool) {
147 // u, ok := ctx.Value(userKey).(*User)
148 // return u, ok
149 // }
150 Value(key interface{}) interface{}
153 // Canceled is the error returned by Context.Err when the context is canceled.
154 var Canceled = errors.New("context canceled")
156 // DeadlineExceeded is the error returned by Context.Err when the context's
157 // deadline passes.
158 var DeadlineExceeded error = deadlineExceededError{}
160 type deadlineExceededError struct{}
162 func (deadlineExceededError) Error() string { return "context deadline exceeded" }
163 func (deadlineExceededError) Timeout() bool { return true }
164 func (deadlineExceededError) Temporary() bool { return true }
166 // An emptyCtx is never canceled, has no values, and has no deadline. It is not
167 // struct{}, since vars of this type must have distinct addresses.
168 type emptyCtx int
170 func (*emptyCtx) Deadline() (deadline time.Time, ok bool) {
171 return
174 func (*emptyCtx) Done() <-chan struct{} {
175 return nil
178 func (*emptyCtx) Err() error {
179 return nil
182 func (*emptyCtx) Value(key interface{}) interface{} {
183 return nil
186 func (e *emptyCtx) String() string {
187 switch e {
188 case background:
189 return "context.Background"
190 case todo:
191 return "context.TODO"
193 return "unknown empty Context"
196 var (
197 background = new(emptyCtx)
198 todo = new(emptyCtx)
201 // Background returns a non-nil, empty Context. It is never canceled, has no
202 // values, and has no deadline. It is typically used by the main function,
203 // initialization, and tests, and as the top-level Context for incoming
204 // requests.
205 func Background() Context {
206 return background
209 // TODO returns a non-nil, empty Context. Code should use context.TODO when
210 // it's unclear which Context to use or it is not yet available (because the
211 // surrounding function has not yet been extended to accept a Context
212 // parameter). TODO is recognized by static analysis tools that determine
213 // whether Contexts are propagated correctly in a program.
214 func TODO() Context {
215 return todo
218 // A CancelFunc tells an operation to abandon its work.
219 // A CancelFunc does not wait for the work to stop.
220 // After the first call, subsequent calls to a CancelFunc do nothing.
221 type CancelFunc func()
223 // WithCancel returns a copy of parent with a new Done channel. The returned
224 // context's Done channel is closed when the returned cancel function is called
225 // or when the parent context's Done channel is closed, whichever happens first.
227 // Canceling this context releases resources associated with it, so code should
228 // call cancel as soon as the operations running in this Context complete.
229 func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
230 c := newCancelCtx(parent)
231 propagateCancel(parent, &c)
232 return &c, func() { c.cancel(true, Canceled) }
235 // newCancelCtx returns an initialized cancelCtx.
236 func newCancelCtx(parent Context) cancelCtx {
237 return cancelCtx{
238 Context: parent,
239 done: make(chan struct{}),
243 // propagateCancel arranges for child to be canceled when parent is.
244 func propagateCancel(parent Context, child canceler) {
245 if parent.Done() == nil {
246 return // parent is never canceled
248 if p, ok := parentCancelCtx(parent); ok {
249 p.mu.Lock()
250 if p.err != nil {
251 // parent has already been canceled
252 child.cancel(false, p.err)
253 } else {
254 if p.children == nil {
255 p.children = make(map[canceler]struct{})
257 p.children[child] = struct{}{}
259 p.mu.Unlock()
260 } else {
261 go func() {
262 select {
263 case <-parent.Done():
264 child.cancel(false, parent.Err())
265 case <-child.Done():
271 // parentCancelCtx follows a chain of parent references until it finds a
272 // *cancelCtx. This function understands how each of the concrete types in this
273 // package represents its parent.
274 func parentCancelCtx(parent Context) (*cancelCtx, bool) {
275 for {
276 switch c := parent.(type) {
277 case *cancelCtx:
278 return c, true
279 case *timerCtx:
280 return &c.cancelCtx, true
281 case *valueCtx:
282 parent = c.Context
283 default:
284 return nil, false
289 // removeChild removes a context from its parent.
290 func removeChild(parent Context, child canceler) {
291 p, ok := parentCancelCtx(parent)
292 if !ok {
293 return
295 p.mu.Lock()
296 if p.children != nil {
297 delete(p.children, child)
299 p.mu.Unlock()
302 // A canceler is a context type that can be canceled directly. The
303 // implementations are *cancelCtx and *timerCtx.
304 type canceler interface {
305 cancel(removeFromParent bool, err error)
306 Done() <-chan struct{}
309 // A cancelCtx can be canceled. When canceled, it also cancels any children
310 // that implement canceler.
311 type cancelCtx struct {
312 Context
314 done chan struct{} // closed by the first cancel call.
316 mu sync.Mutex
317 children map[canceler]struct{} // set to nil by the first cancel call
318 err error // set to non-nil by the first cancel call
321 func (c *cancelCtx) Done() <-chan struct{} {
322 return c.done
325 func (c *cancelCtx) Err() error {
326 c.mu.Lock()
327 defer c.mu.Unlock()
328 return c.err
331 func (c *cancelCtx) String() string {
332 return fmt.Sprintf("%v.WithCancel", c.Context)
335 // cancel closes c.done, cancels each of c's children, and, if
336 // removeFromParent is true, removes c from its parent's children.
337 func (c *cancelCtx) cancel(removeFromParent bool, err error) {
338 if err == nil {
339 panic("context: internal error: missing cancel error")
341 c.mu.Lock()
342 if c.err != nil {
343 c.mu.Unlock()
344 return // already canceled
346 c.err = err
347 close(c.done)
348 for child := range c.children {
349 // NOTE: acquiring the child's lock while holding parent's lock.
350 child.cancel(false, err)
352 c.children = nil
353 c.mu.Unlock()
355 if removeFromParent {
356 removeChild(c.Context, c)
360 // WithDeadline returns a copy of the parent context with the deadline adjusted
361 // to be no later than d. If the parent's deadline is already earlier than d,
362 // WithDeadline(parent, d) is semantically equivalent to parent. The returned
363 // context's Done channel is closed when the deadline expires, when the returned
364 // cancel function is called, or when the parent context's Done channel is
365 // closed, whichever happens first.
367 // Canceling this context releases resources associated with it, so code should
368 // call cancel as soon as the operations running in this Context complete.
369 func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) {
370 if cur, ok := parent.Deadline(); ok && cur.Before(deadline) {
371 // The current deadline is already sooner than the new one.
372 return WithCancel(parent)
374 c := &timerCtx{
375 cancelCtx: newCancelCtx(parent),
376 deadline: deadline,
378 propagateCancel(parent, c)
379 d := time.Until(deadline)
380 if d <= 0 {
381 c.cancel(true, DeadlineExceeded) // deadline has already passed
382 return c, func() { c.cancel(true, Canceled) }
384 c.mu.Lock()
385 defer c.mu.Unlock()
386 if c.err == nil {
387 c.timer = time.AfterFunc(d, func() {
388 c.cancel(true, DeadlineExceeded)
391 return c, func() { c.cancel(true, Canceled) }
394 // A timerCtx carries a timer and a deadline. It embeds a cancelCtx to
395 // implement Done and Err. It implements cancel by stopping its timer then
396 // delegating to cancelCtx.cancel.
397 type timerCtx struct {
398 cancelCtx
399 timer *time.Timer // Under cancelCtx.mu.
401 deadline time.Time
404 func (c *timerCtx) Deadline() (deadline time.Time, ok bool) {
405 return c.deadline, true
408 func (c *timerCtx) String() string {
409 return fmt.Sprintf("%v.WithDeadline(%s [%s])", c.cancelCtx.Context, c.deadline, time.Until(c.deadline))
412 func (c *timerCtx) cancel(removeFromParent bool, err error) {
413 c.cancelCtx.cancel(false, err)
414 if removeFromParent {
415 // Remove this timerCtx from its parent cancelCtx's children.
416 removeChild(c.cancelCtx.Context, c)
418 c.mu.Lock()
419 if c.timer != nil {
420 c.timer.Stop()
421 c.timer = nil
423 c.mu.Unlock()
426 // WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)).
428 // Canceling this context releases resources associated with it, so code should
429 // call cancel as soon as the operations running in this Context complete:
431 // func slowOperationWithTimeout(ctx context.Context) (Result, error) {
432 // ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
433 // defer cancel() // releases resources if slowOperation completes before timeout elapses
434 // return slowOperation(ctx)
435 // }
436 func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
437 return WithDeadline(parent, time.Now().Add(timeout))
440 // WithValue returns a copy of parent in which the value associated with key is
441 // val.
443 // Use context Values only for request-scoped data that transits processes and
444 // APIs, not for passing optional parameters to functions.
446 // The provided key must be comparable and should not be of type
447 // string or any other built-in type to avoid collisions between
448 // packages using context. Users of WithValue should define their own
449 // types for keys. To avoid allocating when assigning to an
450 // interface{}, context keys often have concrete type
451 // struct{}. Alternatively, exported context key variables' static
452 // type should be a pointer or interface.
453 func WithValue(parent Context, key, val interface{}) Context {
454 if key == nil {
455 panic("nil key")
457 if !reflect.TypeOf(key).Comparable() {
458 panic("key is not comparable")
460 return &valueCtx{parent, key, val}
463 // A valueCtx carries a key-value pair. It implements Value for that key and
464 // delegates all other calls to the embedded Context.
465 type valueCtx struct {
466 Context
467 key, val interface{}
470 func (c *valueCtx) String() string {
471 return fmt.Sprintf("%v.WithValue(%#v, %#v)", c.Context, c.key, c.val)
474 func (c *valueCtx) Value(key interface{}) interface{} {
475 if c.key == key {
476 return c.val
478 return c.Context.Value(key)