-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsafe_multi.go
95 lines (72 loc) · 1.37 KB
/
safe_multi.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package batch
import "context"
type (
MultiBatch[Res any] struct {
c *Multi[Res]
coach int
noCopy noCopy //nolint:unused
state byte
}
)
func ByMulti[Res any](c *Multi[Res]) MultiBatch[Res] {
return MultiBatch[Res]{
c: c,
}
}
func (b *MultiBatch[Res]) QueueIn() int {
if b.state != stateNew {
panic(usage)
}
b.state = stateQueued
return b.c.queue.In()
}
func (b *MultiBatch[Res]) Enter(blocking bool) (coach, idx int) {
switch b.state {
case stateNew:
b.QueueIn()
case stateQueued:
default:
panic(usage)
}
coach, idx = b.c.Enter(blocking)
if idx >= 0 {
b.state = stateEntered
} else {
b.state = stateNew
}
b.coach = coach
return coach, idx
}
func (b *MultiBatch[Res]) Trigger() {
b.c.Trigger(b.coach)
}
func (b *MultiBatch[Res]) Cancel(ctx context.Context, err error) (Res, error) {
if b.state != stateEntered {
panic(usage)
}
b.state = stateCommitted
return b.c.Cancel(ctx, b.coach, err)
}
func (b *MultiBatch[Res]) Commit(ctx context.Context) (Res, error) {
if b.state != stateEntered {
panic(usage)
}
b.state = stateCommitted
return b.c.Commit(ctx, b.coach)
}
func (b *MultiBatch[Res]) Exit() int {
idx := -1
switch b.state {
case stateNew:
case stateQueued:
b.c.queue.Out()
b.c.Notify()
case stateEntered,
stateCommitted:
idx = b.c.Exit(b.coach)
default:
panic(usage)
}
b.state = stateExited
return idx
}