-
Notifications
You must be signed in to change notification settings - Fork 664
/
timer.go
108 lines (91 loc) · 2 KB
/
timer.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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package timer
import (
"sync"
"time"
)
// Timer wraps a timer object. This allows a user to specify a handler. Once
// specifying the handler, the dispatch thread can be called. The dispatcher
// will only return after calling Stop. SetTimeoutIn will result in calling the
// handler in the specified amount of time.
type Timer struct {
handler func()
timeout chan struct{}
lock sync.Mutex
wg sync.WaitGroup
finished, shouldExecute bool
duration time.Duration
}
// NewTimer creates a new timer object
func NewTimer(handler func()) *Timer {
timer := &Timer{
handler: handler,
timeout: make(chan struct{}, 1),
}
timer.wg.Add(1)
return timer
}
// SetTimeoutIn will set the timer to fire the handler in [duration]
func (t *Timer) SetTimeoutIn(duration time.Duration) {
t.lock.Lock()
defer t.lock.Unlock()
t.duration = duration
t.shouldExecute = true
t.reset()
}
// Cancel the currently scheduled event
func (t *Timer) Cancel() {
t.lock.Lock()
defer t.lock.Unlock()
t.shouldExecute = false
t.reset()
}
// Stop this timer from executing any more.
func (t *Timer) Stop() {
t.lock.Lock()
if !t.finished {
defer t.wg.Wait()
}
defer t.lock.Unlock()
t.finished = true
t.reset()
}
func (t *Timer) Dispatch() {
t.lock.Lock()
defer t.lock.Unlock()
defer t.wg.Done()
timer := time.NewTimer(0)
cleared := false
reset := false
for !t.finished { // t.finished needs to be thread safe
if !reset && !timer.Stop() && !cleared {
<-timer.C
}
if cleared && t.shouldExecute {
t.lock.Unlock()
t.handler()
} else {
t.lock.Unlock()
}
cleared = false
reset = false
select {
case <-t.timeout:
t.lock.Lock()
if t.shouldExecute {
timer.Reset(t.duration)
}
reset = true
case <-timer.C:
t.lock.Lock()
cleared = true
}
}
}
func (t *Timer) reset() {
select {
case t.timeout <- struct{}{}:
default:
}
}