blob: 12f7f1a1039d907c864f7fb1858c9e7f01120234 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
package runners
import (
"context"
"time"
)
// closedctx is an always closed context.
var closedctx = func() context.Context {
ctx := make(chan struct{})
close(ctx)
return CancelCtx(ctx)
}()
// Closed returns an always closed context.
func Closed() context.Context {
return closedctx
}
// CtxWithCancel returns a new context.Context impl with cancel.
func CtxWithCancel() (context.Context, context.CancelFunc) {
ctx := make(chan struct{})
cncl := func() { close(ctx) }
return CancelCtx(ctx), cncl
}
// CancelCtx is the simplest possible cancellable context.
type CancelCtx (<-chan struct{})
func (CancelCtx) Deadline() (time.Time, bool) {
return time.Time{}, false
}
func (ctx CancelCtx) Done() <-chan struct{} {
return ctx
}
func (ctx CancelCtx) Err() error {
select {
case <-ctx:
return context.Canceled
default:
return nil
}
}
func (CancelCtx) Value(key interface{}) interface{} {
return nil
}
func (ctx CancelCtx) String() string {
var state string
select {
case <-ctx:
state = "closed"
default:
state = "open"
}
return "CancelCtx{state:" + state + "}"
}
func (ctx CancelCtx) GoString() string {
return "runners." + ctx.String()
}
|