-
Notifications
You must be signed in to change notification settings - Fork 1
/
win-or-waiter-core.go
109 lines (92 loc) · 3.15 KB
/
win-or-waiter-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
/*
© 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
import (
"sync"
"time"
)
// WinOrWaiter picks a winner thread to carry out some task used by many threads.
// - threads arriving to an idle WinorWaiter are winners that complete the task
// - After a winning thread completes the task, it invokes WinnerDone
// - threads arriving to a WinOrWait in progress are held waiting until WinnerDone
// - the task is completed on demand, but only by the first thread requesting it
type WinOrWaiterCore struct {
sync.Cond
// data version: the time scanning of the current version of data began.
// zero-time if no data version has been completed
tVersion time.Time
// the time of starting the last initiated scan
tStart time.Time
mustBeLater bool
g0 Go
}
// WinOrWaiter returns a semaphore used for completing an on-demand task by
// the first thread requesting it, and that result shared by subsequent threads held
// waiting for the result.
func NewWinOrWaiterCore(mustBeLater bool, g0 ...Go) (winOrWaiter *WinOrWaiterCore) {
var g00 Go
if len(g0) > 0 {
g00 = g0[0]
}
return &WinOrWaiterCore{
Cond: *sync.NewCond(&sync.Mutex{}),
mustBeLater: mustBeLater,
g0: g00,
}
}
// WinOrWaiter picks a winner thread to carry out some task used by many threads.
// - threads arriving to an idle WinorWaiter are winners that complete the task
// - After a winning thread completes the task, it invokes WinnerDone
// - threads arriving to a WinOrWait in progress are held waiting until WinnerDone
// - the task is completed on demand, but only by the first thread requesting it
func (ww *WinOrWaiterCore) WinOrWait() (isWinner bool) {
ww.Cond.L.Lock()
defer ww.Cond.L.Unlock()
// the time this thread arrived
var tThis = time.Now()
// the data version available when this thread arrived
var tVersion = ww.tVersion
// wait for a data update
for {
// check context
if ww.g0 != nil && ww.g0.Context().Err() != nil {
return // context canceled return
}
// if there has been a data update since this thread arrived
if !tVersion.Equal(ww.tVersion) {
if !ww.mustBeLater {
return // data change occurred and time does not matter return
}
if !tThis.Before(ww.tVersion) {
return // the data version is of a later time than when this thread arrived return
}
}
// ensure data processing is in progress
if isWinner = tVersion.Equal(ww.tStart); isWinner {
ww.tStart = time.Now()
return // this thread is a winner: do task return
}
// wait for any updates
ww.Cond.Wait()
}
}
// WinOrWait allows a winning thread to announce completion of the task.
// Deferrable.
func (ww *WinOrWaiterCore) WinnerDone(errp *error) {
ww.Cond.L.Lock()
defer ww.Cond.Broadcast()
defer ww.Cond.L.Unlock()
isError := errp != nil && *errp != nil
// if no error, record the new data version
if !isError {
ww.tVersion = ww.tStart
} else {
// on error, indicate that data scanning is no longer in progress
ww.tStart = ww.tVersion
}
}
func (ww *WinOrWaiterCore) IsCancel() (isCancel bool) {
return ww.g0 != nil && ww.g0.Context().Err() != nil
}