-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcancel.go
63 lines (50 loc) · 940 Bytes
/
cancel.go
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
package task
import (
"context"
"sync/atomic"
"time"
)
type Cancel struct {
done chan struct{}
c_once uint32
}
func NewCancel() *Cancel {
return &Cancel{
c_once: 1,
done: make(chan struct{}),
}
}
func (c *Cancel) Cancel() {
c.do_cancel()
}
func (c *Cancel) RecvCancel() <-chan struct{} {
return c.done
}
func (c *Cancel) do_cancel() {
win := atomic.SwapUint32(&c.c_once, 0)
if win == 0 {
return
}
close(c.done)
}
func (c *Cancel) AsContext() context.Context {
return (*CancelContext)(c)
}
type CancelContext Cancel
func (cc *CancelContext) Deadline() (time.Time, bool) {
return time.Time{}, false
}
func (cc *CancelContext) Done() <-chan struct{} {
return (*Cancel)(cc).RecvCancel()
}
func (cc *CancelContext) Err() error {
select {
case <-(*Cancel)(cc).RecvCancel():
return context.Canceled
default:
}
return nil
}
func (*CancelContext) Value(key interface{}) interface{} {
return nil
}