2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package time
|
|
|
|
|
|
|
|
import (
|
2011-01-21 19:19:03 +01:00
|
|
|
"container/heap"
|
2011-03-25 00:46:17 +01:00
|
|
|
"sync"
|
2010-12-03 05:34:57 +01:00
|
|
|
)
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
// The Timer type represents a single event.
|
|
|
|
// When the Timer expires, the current time will be sent on C
|
|
|
|
// unless the Timer represents an AfterFunc event.
|
|
|
|
type Timer struct {
|
|
|
|
C <-chan int64
|
|
|
|
t int64 // The absolute time that the event should fire.
|
|
|
|
f func(int64) // The function to call when the event fires.
|
|
|
|
i int // The event's index inside eventHeap.
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
type timerHeap []*Timer
|
2011-01-21 19:19:03 +01:00
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
// forever is the absolute time (in ns) of an event that is forever away.
|
|
|
|
const forever = 1 << 62
|
|
|
|
|
|
|
|
// maxSleepTime is the maximum length of time that a sleeper
|
|
|
|
// sleeps for before checking if it is defunct.
|
|
|
|
const maxSleepTime = 1e9
|
|
|
|
|
|
|
|
var (
|
|
|
|
// timerMutex guards the variables inside this var group.
|
|
|
|
timerMutex sync.Mutex
|
|
|
|
|
|
|
|
// timers holds a binary heap of pending events, terminated with a sentinel.
|
|
|
|
timers timerHeap
|
|
|
|
|
|
|
|
// currentSleeper is an ever-incrementing counter which represents
|
|
|
|
// the current sleeper. It allows older sleepers to detect that they are
|
|
|
|
// defunct and exit.
|
|
|
|
currentSleeper int64
|
|
|
|
)
|
2011-01-21 19:19:03 +01:00
|
|
|
|
|
|
|
func init() {
|
2011-03-17 00:05:44 +01:00
|
|
|
timers.Push(&Timer{t: forever}) // sentinel
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
// NewTimer creates a new Timer that will send
|
|
|
|
// the current time on its channel after at least ns nanoseconds.
|
|
|
|
func NewTimer(ns int64) *Timer {
|
|
|
|
c := make(chan int64, 1)
|
|
|
|
e := after(ns, func(t int64) { c <- t })
|
|
|
|
e.C = c
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2011-01-21 19:19:03 +01:00
|
|
|
// After waits at least ns nanoseconds before sending the current time
|
|
|
|
// on the returned channel.
|
2011-03-17 00:05:44 +01:00
|
|
|
// It is equivalent to NewTimer(ns).C.
|
2011-01-21 19:19:03 +01:00
|
|
|
func After(ns int64) <-chan int64 {
|
2011-03-17 00:05:44 +01:00
|
|
|
return NewTimer(ns).C
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// AfterFunc waits at least ns nanoseconds before calling f
|
2011-03-17 00:05:44 +01:00
|
|
|
// in its own goroutine. It returns a Timer that can
|
|
|
|
// be used to cancel the call using its Stop method.
|
|
|
|
func AfterFunc(ns int64, f func()) *Timer {
|
|
|
|
return after(ns, func(_ int64) {
|
2011-01-21 19:19:03 +01:00
|
|
|
go f()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
// Stop prevents the Timer from firing.
|
|
|
|
// It returns true if the call stops the timer, false if the timer has already
|
|
|
|
// expired or stopped.
|
|
|
|
func (e *Timer) Stop() (ok bool) {
|
|
|
|
timerMutex.Lock()
|
|
|
|
// Avoid removing the first event in the queue so that
|
|
|
|
// we don't start a new sleeper unnecessarily.
|
|
|
|
if e.i > 0 {
|
|
|
|
heap.Remove(timers, e.i)
|
|
|
|
}
|
|
|
|
ok = e.f != nil
|
|
|
|
e.f = nil
|
|
|
|
timerMutex.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-01-21 19:19:03 +01:00
|
|
|
// after is the implementation of After and AfterFunc.
|
|
|
|
// When the current time is after ns, it calls f with the current time.
|
|
|
|
// It assumes that f will not block.
|
2011-03-17 00:05:44 +01:00
|
|
|
func after(ns int64, f func(int64)) (e *Timer) {
|
|
|
|
now := Nanoseconds()
|
2011-01-21 19:19:03 +01:00
|
|
|
t := Nanoseconds() + ns
|
2011-03-17 00:05:44 +01:00
|
|
|
if ns > 0 && t < now {
|
|
|
|
panic("time: time overflow")
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
2011-03-17 00:05:44 +01:00
|
|
|
timerMutex.Lock()
|
|
|
|
t0 := timers[0].t
|
|
|
|
e = &Timer{nil, t, f, -1}
|
|
|
|
heap.Push(timers, e)
|
|
|
|
// Start a new sleeper if the new event is before
|
|
|
|
// the first event in the queue. If the length of time
|
|
|
|
// until the new event is at least maxSleepTime,
|
|
|
|
// then we're guaranteed that the sleeper will wake up
|
|
|
|
// in time to service it, so no new sleeper is needed.
|
|
|
|
if t0 > t && (t0 == forever || ns < maxSleepTime) {
|
|
|
|
currentSleeper++
|
|
|
|
go sleeper(currentSleeper)
|
|
|
|
}
|
|
|
|
timerMutex.Unlock()
|
|
|
|
return
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
// sleeper continually looks at the earliest event in the queue, waits until it happens,
|
|
|
|
// then removes any events in the queue that are due. It stops when the queue
|
|
|
|
// is empty or when another sleeper has been started.
|
|
|
|
func sleeper(sleeperId int64) {
|
|
|
|
timerMutex.Lock()
|
|
|
|
e := timers[0]
|
|
|
|
t := Nanoseconds()
|
|
|
|
for e.t != forever {
|
2011-01-21 19:19:03 +01:00
|
|
|
if dt := e.t - t; dt > 0 {
|
2011-03-17 00:05:44 +01:00
|
|
|
if dt > maxSleepTime {
|
|
|
|
dt = maxSleepTime
|
|
|
|
}
|
|
|
|
timerMutex.Unlock()
|
2011-03-25 00:46:17 +01:00
|
|
|
sysSleep(dt)
|
2011-03-17 00:05:44 +01:00
|
|
|
timerMutex.Lock()
|
|
|
|
if currentSleeper != sleeperId {
|
|
|
|
// Another sleeper has been started, making this one redundant.
|
|
|
|
break
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
|
|
|
}
|
2011-03-17 00:05:44 +01:00
|
|
|
e = timers[0]
|
|
|
|
t = Nanoseconds()
|
2011-01-21 19:19:03 +01:00
|
|
|
for t >= e.t {
|
2011-03-17 00:05:44 +01:00
|
|
|
if e.f != nil {
|
|
|
|
e.f(t)
|
|
|
|
e.f = nil
|
|
|
|
}
|
|
|
|
heap.Pop(timers)
|
|
|
|
e = timers[0]
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
|
|
|
}
|
2011-03-17 00:05:44 +01:00
|
|
|
timerMutex.Unlock()
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
func (timerHeap) Len() int {
|
|
|
|
return len(timers)
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
func (timerHeap) Less(i, j int) bool {
|
|
|
|
return timers[i].t < timers[j].t
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
func (timerHeap) Swap(i, j int) {
|
|
|
|
timers[i], timers[j] = timers[j], timers[i]
|
|
|
|
timers[i].i = i
|
|
|
|
timers[j].i = j
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
func (timerHeap) Push(x interface{}) {
|
|
|
|
e := x.(*Timer)
|
|
|
|
e.i = len(timers)
|
|
|
|
timers = append(timers, e)
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
func (timerHeap) Pop() interface{} {
|
2011-01-21 19:19:03 +01:00
|
|
|
// TODO: possibly shrink array.
|
2011-03-17 00:05:44 +01:00
|
|
|
n := len(timers) - 1
|
|
|
|
e := timers[n]
|
|
|
|
timers[n] = nil
|
|
|
|
timers = timers[0:n]
|
|
|
|
e.i = -1
|
2011-01-21 19:19:03 +01:00
|
|
|
return e
|
|
|
|
}
|