Improved timer state machine

This commit is contained in:
Mathias Hall-Andersen
2017-07-08 23:51:26 +02:00
parent 5c1ccbddf0
commit 4ad62aaa6a
10 changed files with 418 additions and 222 deletions
+19 -4
View File
@@ -4,6 +4,14 @@ import (
"time"
)
/* We use int32 as atomic bools
* (since booleans are not natively supported by sync/atomic)
*/
const (
AtomicFalse = iota
AtomicTrue
)
func min(a uint, b uint) uint {
if a > b {
return b
@@ -11,14 +19,21 @@ func min(a uint, b uint) uint {
return a
}
func sendSignal(c chan struct{}) {
func signalSend(c chan struct{}) {
select {
case c <- struct{}{}:
default:
}
}
func stopTimer(timer *time.Timer) {
func signalClear(c chan struct{}) {
select {
case <-c:
default:
}
}
func timerStop(timer *time.Timer) {
if !timer.Stop() {
select {
case <-timer.C:
@@ -27,8 +42,8 @@ func stopTimer(timer *time.Timer) {
}
}
func stoppedTimer() *time.Timer {
func NewStoppedTimer() *time.Timer {
timer := time.NewTimer(time.Hour)
stopTimer(timer)
timerStop(timer)
return timer
}