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/time.go:/^type timer
16 type runtimeTimer
struct {
22 f
func(interface{}, uintptr) // NOTE: must not be closure
27 // when is a helper function for setting the 'when' field of a runtimeTimer.
28 // It returns what the time will be, in nanoseconds, Duration d in the future.
29 // If d is negative, it is ignored. If the returned value would be less than
30 // zero because of an overflow, MaxInt64 is returned.
31 func when(d Duration
) int64 {
35 t
:= runtimeNano() + int64(d
)
37 t
= 1<<63 - 1 // math.MaxInt64
42 func startTimer(*runtimeTimer
)
43 func stopTimer(*runtimeTimer
) bool
45 // The Timer type represents a single event.
46 // When the Timer expires, the current time will be sent on C,
47 // unless the Timer was created by AfterFunc.
48 // A Timer must be created with NewTimer or AfterFunc.
54 // Stop prevents the Timer from firing.
55 // It returns true if the call stops the timer, false if the timer has already
56 // expired or been stopped.
57 // Stop does not close the channel, to prevent a read from the channel succeeding
60 // To prevent a timer created with NewTimer from firing after a call to Stop,
61 // check the return value and drain the channel.
62 // For example, assuming the program has not received from t.C already:
68 // This cannot be done concurrent to other receives from the Timer's
71 // For a timer created with AfterFunc(d, f), if t.Stop returns false, then the timer
72 // has already expired and the function f has been started in its own goroutine;
73 // Stop does not wait for f to complete before returning.
74 // If the caller needs to know whether f is completed, it must coordinate
76 func (t
*Timer
) Stop() bool {
78 panic("time: Stop called on uninitialized Timer")
80 return stopTimer(&t
.r
)
83 // NewTimer creates a new Timer that will send
84 // the current time on its channel after at least duration d.
85 func NewTimer(d Duration
) *Timer
{
86 c
:= make(chan Time
, 1)
99 // Reset changes the timer to expire after duration d.
100 // It returns true if the timer had been active, false if the timer had
101 // expired or been stopped.
103 // Resetting a timer must take care not to race with the send into t.C
104 // that happens when the current timer expires.
105 // If a program has already received a value from t.C, the timer is known
106 // to have expired, and t.Reset can be used directly.
107 // If a program has not yet received a value from t.C, however,
108 // the timer must be stopped and—if Stop reports that the timer expired
109 // before being stopped—the channel explicitly drained:
116 // This should not be done concurrent to other receives from the Timer's
119 // Note that it is not possible to use Reset's return value correctly, as there
120 // is a race condition between draining the channel and the new timer expiring.
121 // Reset should always be invoked on stopped or expired channels, as described above.
122 // The return value exists to preserve compatibility with existing programs.
123 func (t
*Timer
) Reset(d Duration
) bool {
125 panic("time: Reset called on uninitialized Timer")
128 active
:= stopTimer(&t
.r
)
134 func sendTime(c
interface{}, seq
uintptr) {
135 // Non-blocking send of time on c.
136 // Used in NewTimer, it cannot block anyway (buffer).
137 // Used in NewTicker, dropping sends on the floor is
138 // the desired behavior when the reader gets behind,
139 // because the sends are periodic.
141 case c
.(chan Time
) <- Now():
146 // After waits for the duration to elapse and then sends the current time
147 // on the returned channel.
148 // It is equivalent to NewTimer(d).C.
149 // The underlying Timer is not recovered by the garbage collector
150 // until the timer fires. If efficiency is a concern, use NewTimer
151 // instead and call Timer.Stop if the timer is no longer needed.
152 func After(d Duration
) <-chan Time
{
156 // AfterFunc waits for the duration to elapse and then calls f
157 // in its own goroutine. It returns a Timer that can
158 // be used to cancel the call using its Stop method.
159 func AfterFunc(d Duration
, f
func()) *Timer
{
171 func goFunc(arg
interface{}, seq
uintptr) {