-
Notifications
You must be signed in to change notification settings - Fork 1
/
serial-do-core.go
128 lines (102 loc) · 2.21 KB
/
serial-do-core.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
© 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
import (
"context"
"sync"
"time"
)
type SerialDoCore struct {
thunk func(at time.Time)
cond *sync.Cond
pendingSince time.Time // behind lock
busySince time.Time // behind lock
errFn func(err error)
ctx context.Context
}
func NewSerialDoCore(thunk func(at time.Time), errFn func(err error), ctx context.Context) (serialDoCore *SerialDoCore) {
return &SerialDoCore{
thunk: thunk,
cond: sync.NewCond(&sync.Mutex{}),
errFn: errFn,
ctx: ctx,
}
}
func (sdo *SerialDoCore) Do(now ...time.Time) (isPending bool, isShutdown bool) {
// check state
if sdo.ctx.Err() != nil {
isShutdown = true
return // isShutdown
}
// obtain request time in now0
var now0 time.Time
if len(now) > 0 {
now0 = now[0]
}
if now0.IsZero() {
now0 = time.Now()
}
sdo.cond.L.Lock()
defer sdo.cond.L.Unlock()
// if already pending, ignore
if !sdo.pendingSince.IsZero() {
isPending = true
return // no-op: already pending
}
// if busy, mark pending
if !sdo.busySince.IsZero() {
sdo.pendingSince = now0
isPending = true
return
}
// launch thread
sdo.busySince = now0
go sdo.doThread(now0)
return
}
func (sdo *SerialDoCore) Wait(at time.Time) {
<-sdo.ctx.Done()
sdo.cond.L.Lock()
defer sdo.cond.L.Unlock()
for {
if sdo.busySince.IsZero() {
return // cancel and no thread: done
}
sdo.cond.Wait()
}
}
func (sdo *SerialDoCore) doThread(at time.Time) {
defer Recover(Annotation(), nil, sdo.errFn)
for {
sdo.invokeThunk(at)
if at = sdo.checkForMoreDo(); at.IsZero() {
return
}
}
}
func (sdo *SerialDoCore) checkForMoreDo() (at time.Time) {
sdo.cond.L.Lock()
defer sdo.cond.L.Unlock()
// check for cancel
if sdo.ctx.Err() != nil {
sdo.busySince = time.Time{}
sdo.pendingSince = time.Time{}
return
}
// if pending, relaunch thunk
if !sdo.pendingSince.IsZero() {
at = sdo.pendingSince
sdo.pendingSince = time.Time{}
return
}
// idle: thread should exit
sdo.busySince = time.Time{}
sdo.cond.Signal()
return
}
func (sdo *SerialDoCore) invokeThunk(at time.Time) {
defer Recover(Annotation(), nil, sdo.errFn)
sdo.thunk(at)
}