2017-11-30 23:22:40 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Timer struct {
|
|
|
|
pending AtomicBool
|
|
|
|
timer *time.Timer
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Starts the timer if not already pending
|
|
|
|
*/
|
|
|
|
func (t *Timer) Start(dur time.Duration) bool {
|
2018-02-04 19:18:44 +01:00
|
|
|
if !t.pending.Swap(true) {
|
2017-11-30 23:22:40 +01:00
|
|
|
t.timer.Reset(dur)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Stops the timer
|
|
|
|
*/
|
|
|
|
func (t *Timer) Stop() {
|
2018-02-04 19:18:44 +01:00
|
|
|
if t.pending.Swap(true) {
|
2017-11-30 23:22:40 +01:00
|
|
|
t.timer.Stop()
|
|
|
|
select {
|
|
|
|
case <-t.timer.C:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.pending.Set(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Timer) Pending() bool {
|
|
|
|
return t.pending.Get()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Timer) Reset(dur time.Duration) {
|
|
|
|
t.pending.Set(false)
|
|
|
|
t.Start(dur)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Timer) Wait() <-chan time.Time {
|
|
|
|
return t.timer.C
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTimer() (t Timer) {
|
|
|
|
t.pending.Set(false)
|
|
|
|
t.timer = time.NewTimer(0)
|
|
|
|
t.timer.Stop()
|
|
|
|
select {
|
|
|
|
case <-t.timer.C:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|