2017-05-30 20:36:49 +00:00
|
|
|
package main
|
|
|
|
|
2017-07-01 21:29:22 +00:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2017-07-08 21:51:26 +00:00
|
|
|
/* We use int32 as atomic bools
|
|
|
|
* (since booleans are not natively supported by sync/atomic)
|
|
|
|
*/
|
|
|
|
const (
|
|
|
|
AtomicFalse = iota
|
|
|
|
AtomicTrue
|
|
|
|
)
|
|
|
|
|
2017-05-30 20:36:49 +00:00
|
|
|
func min(a uint, b uint) uint {
|
|
|
|
if a > b {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
2017-06-30 12:41:08 +00:00
|
|
|
|
2017-07-10 10:09:19 +00:00
|
|
|
func minUint64(a uint64, b uint64) uint64 {
|
|
|
|
if a > b {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2017-07-08 21:51:26 +00:00
|
|
|
func signalSend(c chan struct{}) {
|
2017-06-30 12:41:08 +00:00
|
|
|
select {
|
|
|
|
case c <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
2017-07-01 21:29:22 +00:00
|
|
|
|
2017-07-08 21:51:26 +00:00
|
|
|
func signalClear(c chan struct{}) {
|
|
|
|
select {
|
|
|
|
case <-c:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func timerStop(timer *time.Timer) {
|
2017-07-01 21:29:22 +00:00
|
|
|
if !timer.Stop() {
|
|
|
|
select {
|
|
|
|
case <-timer.C:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-08 21:51:26 +00:00
|
|
|
func NewStoppedTimer() *time.Timer {
|
2017-07-01 21:29:22 +00:00
|
|
|
timer := time.NewTimer(time.Hour)
|
2017-07-08 21:51:26 +00:00
|
|
|
timerStop(timer)
|
2017-07-01 21:29:22 +00:00
|
|
|
return timer
|
|
|
|
}
|