-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.go
173 lines (154 loc) · 4.87 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Manipulate timers on POSIX clocks.
// Copyright (C) 2017 Laboratory of ACM/ICPC, Xidian University
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Author: Xi Ruoyao <[email protected]>
package posixtime
import (
"errors"
"sync/atomic"
"time"
"golang.org/x/sys/unix"
)
// The TimerEvent type represents a timer expiration event.
type TimerEvent struct {
// It contains the clock time of timer expiration.
Time time.Time
}
// The Timer type represents a single event. When the Timer expires, a
// TimerEvent will be sent on C, unless the Timer was created by
// AfterFunc. A Timer must be created with ClockID.NewTimer or
// ClockID.AfterFunc.
type Timer struct {
C <-chan TimerEvent
clock ClockID
armed int32
stopch chan<- struct{}
handle func()
}
// Stop prevents the Timer from firing. It returns true if the call stops
// the timer, false if the timer has already expired or been stopped. Stop
// does not close the channel, to prevent a read from the channel succeeding
// incorrectly.
//
// To prevent a timer created with ClockID.NewTimer from firing after a call
// to Stop, check the return value and drain the channel. Foe example,
// assuming the program has not received from t.C already:
//
// if !t.Stop() {
// <-t.C
// }
//
// This cannot be done concurrent to other receives from the Timer's
// channel.
//
// For a timer created with ClockID.AfterFunc(d, f), it t.Stop returns
// false, then the timer has already expired and the function f has been
// started in its own goroutine; Stop does not wait for f to complete
// before returning. If the caller needs to know whether f is completed,
// it must coordinate with f explicitly.
func (t *Timer) Stop() bool {
active := atomic.SwapInt32(&t.armed, 0)
if active == 1 {
close(t.stopch)
return true
}
return false
}
// This internal function set _unarmed_ t with empty C to expire after
// duration d. If there is an error setting t, this function returns and
// do nothing.
func (t *Timer) arm(d time.Duration) error {
stopch := make(chan struct{})
t.stopch = stopch
t.armed = 1
err := timerRoutine(t.clock,
unix.NsecToTimespec(d.Nanoseconds()),
stopch,
func() {
active := atomic.SwapInt32(&t.armed, 0)
if active == 1 {
t.handle()
}
})
if err != nil {
t.armed = 0
}
return err
}
// Reset changes the timer to expire after duration d. It must be used on
// an inactive timer with a drained t.C. If a program has already received
// a value from t.C, the timer is know to have expired, then t.Reset can be
// used directly. If not, the timer must be stopped and-if Stop reports
// that the timer expired before being stopped-the channel explicitly
// drained:
//
// if !t.Stop() {
// <-t.C
// }
// t.Reset(d)
//
// This should not be done concurrent to other receives from the timer's
// channel.
//
// If t is active, Reset would return an error indicating this issue and
// do nothing. However if t is inactive but t.C is not drained, Reset can
// not know this and there may be severe problems. Do NOT do that.
//
// If Reset can not arm the timer, it return an error and do nothing.
func (t *Timer) Reset(d time.Duration) error {
// Detect an obvious error and report it.
// But a t.C not drained can not be detected.
if atomic.LoadInt32(&t.armed) == 1 {
return errors.New("should not Reset an active timer")
}
return t.arm(d)
}
// NewTimer creates a new Timer on clock. The Timer would send a
// TimerEvent its channel after at least duration d.
func (clock ClockID) NewTimer(d time.Duration) (*Timer, error) {
ch := make(chan TimerEvent)
t := Timer{
C: ch,
armed: 0,
clock: clock,
handle: func() {
t, _ := clock.GetTime()
ch <- TimerEvent{*t}
},
}
err := t.arm(d)
if err != nil {
return nil, err
}
return &t, nil
}
// AfterFunc waits for the duration to elapse and then calls f in its own
// goroutine, with a TimerEvent as arugment. It returns a Timer that can
// be used to cancel the call using its Stop method.
func (clock ClockID) AfterFunc(d time.Duration, f func(TimerEvent)) (
*Timer, error) {
ch := make(chan TimerEvent)
t := Timer{
C: ch,
armed: 0,
clock: clock,
handle: func() {
t, _ := clock.GetTime()
f(TimerEvent{*t})
},
}
err := t.arm(d)
if err != nil {
return nil, err
}
return &t, nil
}