github.com/panjjo/go@v0.0.0-20161104043856-d62b31386338/src/time/sleep.go (about) 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. 4 5 package time 6 7 // Sleep pauses the current goroutine for at least the duration d. 8 // A negative or zero duration causes Sleep to return immediately. 9 func Sleep(d Duration) 10 11 // runtimeNano returns the current value of the runtime clock in nanoseconds. 12 func runtimeNano() int64 13 14 // Interface to timers implemented in package runtime. 15 // Must be in sync with ../runtime/time.go:/^type timer 16 type runtimeTimer struct { 17 i int 18 when int64 19 period int64 20 f func(interface{}, uintptr) // NOTE: must not be closure 21 arg interface{} 22 seq uintptr 23 } 24 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 { 30 if d <= 0 { 31 return runtimeNano() 32 } 33 t := runtimeNano() + int64(d) 34 if t < 0 { 35 t = 1<<63 - 1 // math.MaxInt64 36 } 37 return t 38 } 39 40 func startTimer(*runtimeTimer) 41 func stopTimer(*runtimeTimer) bool 42 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. 47 type Timer struct { 48 C <-chan Time 49 r runtimeTimer 50 } 51 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 56 // incorrectly. 57 // 58 // To prevent the timer firing after a call to Stop, 59 // check the return value and drain the channel. 60 // For example, assuming the program has not received from t.C already: 61 // 62 // if !t.Stop() { 63 // <-t.C 64 // } 65 // 66 // This cannot be done concurrent to other receives from the Timer's 67 // channel. 68 func (t *Timer) Stop() bool { 69 if t.r.f == nil { 70 panic("time: Stop called on uninitialized Timer") 71 } 72 return stopTimer(&t.r) 73 } 74 75 // NewTimer creates a new Timer that will send 76 // the current time on its channel after at least duration d. 77 func NewTimer(d Duration) *Timer { 78 c := make(chan Time, 1) 79 t := &Timer{ 80 C: c, 81 r: runtimeTimer{ 82 when: when(d), 83 f: sendTime, 84 arg: c, 85 }, 86 } 87 startTimer(&t.r) 88 return t 89 } 90 91 // Reset changes the timer to expire after duration d. 92 // It returns true if the timer had been active, false if the timer had 93 // expired or been stopped. 94 // 95 // Resetting a timer must take care not to race with the send into t.C 96 // that happens when the current timer expires. 97 // If a program has already received a value from t.C, the timer is known 98 // to have expired, and t.Reset can be used directly. 99 // If a program has not yet received a value from t.C, however, 100 // the timer must be stopped and—if Stop reports that the timer expired 101 // before being stopped—the channel explicitly drained: 102 // 103 // if !t.Stop() { 104 // <-t.C 105 // } 106 // t.Reset(d) 107 // 108 // This should not be done concurrent to other receives from the Timer's 109 // channel. 110 // 111 // Note that it is not possible to use Reset's return value correctly, as there 112 // is a race condition between draining the channel and the new timer expiring. 113 // Reset should always be invoked on stopped or expired channels, as described above. 114 // The return value exists to preserve compatibility with existing programs. 115 func (t *Timer) Reset(d Duration) bool { 116 if t.r.f == nil { 117 panic("time: Reset called on uninitialized Timer") 118 } 119 w := when(d) 120 active := stopTimer(&t.r) 121 t.r.when = w 122 startTimer(&t.r) 123 return active 124 } 125 126 func sendTime(c interface{}, seq uintptr) { 127 // Non-blocking send of time on c. 128 // Used in NewTimer, it cannot block anyway (buffer). 129 // Used in NewTicker, dropping sends on the floor is 130 // the desired behavior when the reader gets behind, 131 // because the sends are periodic. 132 select { 133 case c.(chan Time) <- Now(): 134 default: 135 } 136 } 137 138 // After waits for the duration to elapse and then sends the current time 139 // on the returned channel. 140 // It is equivalent to NewTimer(d).C. 141 // The underlying Timer is not recovered by the garbage collector 142 // until the timer fires. If efficiency is a concern, use NewTimer 143 // instead and call Timer.Stop if the timer is no longer needed. 144 func After(d Duration) <-chan Time { 145 return NewTimer(d).C 146 } 147 148 // AfterFunc waits for the duration to elapse and then calls f 149 // in its own goroutine. It returns a Timer that can 150 // be used to cancel the call using its Stop method. 151 func AfterFunc(d Duration, f func()) *Timer { 152 t := &Timer{ 153 r: runtimeTimer{ 154 when: when(d), 155 f: goFunc, 156 arg: f, 157 }, 158 } 159 startTimer(&t.r) 160 return t 161 } 162 163 func goFunc(arg interface{}, seq uintptr) { 164 go arg.(func())() 165 }