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
|
|
|
|
|
2012-01-12 02:31:45 +01:00
|
|
|
// Sleep pauses the current goroutine for the duration d.
|
|
|
|
func Sleep(d Duration)
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
func nano() int64 {
|
|
|
|
sec, nsec := now()
|
|
|
|
return sec*1e9 + int64(nsec)
|
|
|
|
}
|
|
|
|
|
2011-12-13 00:40:51 +01:00
|
|
|
// Interface to timers implemented in package runtime.
|
|
|
|
// Must be in sync with ../runtime/runtime.h:/^struct.Timer$
|
|
|
|
type runtimeTimer struct {
|
|
|
|
i int32
|
|
|
|
when int64
|
|
|
|
period int64
|
|
|
|
f func(int64, interface{})
|
|
|
|
arg interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func startTimer(*runtimeTimer)
|
|
|
|
func stopTimer(*runtimeTimer) bool
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
// The Timer type represents a single event.
|
2011-12-13 00:40:51 +01:00
|
|
|
// When the Timer expires, the current time will be sent on C,
|
|
|
|
// unless the Timer was created by AfterFunc.
|
2011-03-17 00:05:44 +01:00
|
|
|
type Timer struct {
|
2011-12-13 20:16:27 +01:00
|
|
|
C <-chan Time
|
2011-12-13 00:40:51 +01:00
|
|
|
r runtimeTimer
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
|
|
|
|
2011-12-13 00:40:51 +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 (t *Timer) Stop() (ok bool) {
|
|
|
|
return stopTimer(&t.r)
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
// NewTimer creates a new Timer that will send
|
2012-01-25 21:56:26 +01:00
|
|
|
// the current time on its channel after at least duration d.
|
2011-12-13 20:16:27 +01:00
|
|
|
func NewTimer(d Duration) *Timer {
|
|
|
|
c := make(chan Time, 1)
|
2011-12-13 00:40:51 +01:00
|
|
|
t := &Timer{
|
|
|
|
C: c,
|
|
|
|
r: runtimeTimer{
|
2011-12-13 20:16:27 +01:00
|
|
|
when: nano() + int64(d),
|
2011-12-13 00:40:51 +01:00
|
|
|
f: sendTime,
|
|
|
|
arg: c,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
startTimer(&t.r)
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendTime(now int64, c interface{}) {
|
|
|
|
// Non-blocking send of time on c.
|
|
|
|
// Used in NewTimer, it cannot block anyway (buffer).
|
|
|
|
// Used in NewTicker, dropping sends on the floor is
|
|
|
|
// the desired behavior when the reader gets behind,
|
|
|
|
// because the sends are periodic.
|
|
|
|
select {
|
2011-12-13 20:16:27 +01:00
|
|
|
case c.(chan Time) <- Unix(0, now):
|
2011-12-13 00:40:51 +01:00
|
|
|
default:
|
|
|
|
}
|
2011-03-17 00:05:44 +01:00
|
|
|
}
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
// After waits for the duration to elapse and then sends the current time
|
2011-01-21 19:19:03 +01:00
|
|
|
// on the returned channel.
|
2012-01-25 21:56:26 +01:00
|
|
|
// It is equivalent to NewTimer(d).C.
|
2011-12-13 20:16:27 +01:00
|
|
|
func After(d Duration) <-chan Time {
|
|
|
|
return NewTimer(d).C
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
|
|
|
|
2012-01-12 02:31:45 +01:00
|
|
|
// AfterFunc waits for the duration to elapse and then calls 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.
|
2012-01-12 02:31:45 +01:00
|
|
|
func AfterFunc(d Duration, f func()) *Timer {
|
2011-12-13 00:40:51 +01:00
|
|
|
t := &Timer{
|
|
|
|
r: runtimeTimer{
|
2012-01-12 02:31:45 +01:00
|
|
|
when: nano() + int64(d),
|
2011-12-13 00:40:51 +01:00
|
|
|
f: goFunc,
|
|
|
|
arg: f,
|
|
|
|
},
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
2011-12-13 00:40:51 +01:00
|
|
|
startTimer(&t.r)
|
|
|
|
return t
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
|
|
|
|
2011-12-13 00:40:51 +01:00
|
|
|
func goFunc(now int64, arg interface{}) {
|
|
|
|
go arg.(func())()
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|