1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 // Sleep pauses the current goroutine for at least the duration d.
8 // A negative or zero duration causes Sleep to return immediately.
11 // runtimeNano returns the current value of the runtime clock in nanoseconds.
12 func runtimeNano() int64
14 // Interface to timers implemented in package runtime.
15 // Must be in sync with ../runtime/runtime.h:/^struct.Timer$
16 type runtimeTimer
struct {
20 f
func(interface{}, uintptr) // NOTE: must not be closure
25 // when is a helper function for setting the 'when' field of a runtimeTimer.
26 // It returns what the time will be, in nanoseconds, Duration d in the future.
27 // If d is negative, it is ignored. If the returned value would be less than
28 // zero because of an overflow, MaxInt64 is returned.
29 func when(d Duration
) int64 {
33 t
:= runtimeNano() + int64(d
)
35 t
= 1<<63 - 1 // math.MaxInt64
40 func startTimer(*runtimeTimer
)
41 func stopTimer(*runtimeTimer
) bool
43 // The Timer type represents a single event.
44 // When the Timer expires, the current time will be sent on C,
45 // unless the Timer was created by AfterFunc.
46 // A Timer must be created with NewTimer or AfterFunc.
52 // Stop prevents the Timer from firing.
53 // It returns true if the call stops the timer, false if the timer has already
54 // expired or been stopped.
55 // Stop does not close the channel, to prevent a read from the channel succeeding
58 // To prevent the timer firing after a call to Stop,
59 // check the return value and drain the channel. For example:
63 // This cannot be done concurrent to other receives from the Timer's
65 func (t
*Timer
) Stop() bool {
67 panic("time: Stop called on uninitialized Timer")
69 return stopTimer(&t
.r
)
72 // NewTimer creates a new Timer that will send
73 // the current time on its channel after at least duration d.
74 func NewTimer(d Duration
) *Timer
{
75 c
:= make(chan Time
, 1)
88 // Reset changes the timer to expire after duration d.
89 // It returns true if the timer had been active, false if the timer had
90 // expired or been stopped.
92 // To reuse an active timer, always call its Stop method first and—if it had
93 // expired—drain the value from its channel. For example:
98 // This should not be done concurrent to other receives from the Timer's
101 // Note that it is not possible to use Reset's return value correctly, as there
102 // is a race condition between draining the channel and the new timer expiring.
103 // Reset should always be used in concert with Stop, as described above.
104 // The return value exists to preserve compatibility with existing programs.
105 func (t
*Timer
) Reset(d Duration
) bool {
107 panic("time: Reset called on uninitialized Timer")
110 active
:= stopTimer(&t
.r
)
116 func sendTime(c
interface{}, seq
uintptr) {
117 // Non-blocking send of time on c.
118 // Used in NewTimer, it cannot block anyway (buffer).
119 // Used in NewTicker, dropping sends on the floor is
120 // the desired behavior when the reader gets behind,
121 // because the sends are periodic.
123 case c
.(chan Time
) <- Now():
128 // After waits for the duration to elapse and then sends the current time
129 // on the returned channel.
130 // It is equivalent to NewTimer(d).C.
131 // The underlying Timer is not recovered by the garbage collector
132 // until the timer fires. If efficiency is a concern, use NewTimer
133 // instead and call Timer.Stop if the timer is no longer needed.
134 func After(d Duration
) <-chan Time
{
138 // AfterFunc waits for the duration to elapse and then calls f
139 // in its own goroutine. It returns a Timer that can
140 // be used to cancel the call using its Stop method.
141 func AfterFunc(d Duration
, f
func()) *Timer
{
153 func goFunc(arg
interface{}, seq
uintptr) {