sleep.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. package time
  5. // Sleep pauses the current goroutine for at least the duration d.
  6. // A negative or zero duration causes Sleep to return immediately.
  7. func Sleep(d Duration)
  8. // runtimeNano returns the current value of the runtime clock in nanoseconds.
  9. func runtimeNano() int64
  10. // Interface to timers implemented in package runtime.
  11. // Must be in sync with ../runtime/runtime.h:/^struct.Timer$
  12. type runtimeTimer struct {
  13. i int
  14. when int64
  15. period int64
  16. f func(interface{}, uintptr) // NOTE: must not be closure
  17. arg interface{}
  18. seq uintptr
  19. }
  20. // when is a helper function for setting the 'when' field of a runtimeTimer.
  21. // It returns what the time will be, in nanoseconds, Duration d in the future.
  22. // If d is negative, it is ignored. If the returned value would be less than
  23. // zero because of an overflow, MaxInt64 is returned.
  24. func when(d Duration) int64 {
  25. if d <= 0 {
  26. return runtimeNano()
  27. }
  28. t := runtimeNano() + int64(d)
  29. if t < 0 {
  30. t = 1<<63 - 1 // math.MaxInt64
  31. }
  32. return t
  33. }
  34. func startTimer(*runtimeTimer)
  35. func stopTimer(*runtimeTimer) bool
  36. // The Timer type represents a single event.
  37. // When the Timer expires, the current time will be sent on C,
  38. // unless the Timer was created by AfterFunc.
  39. // A Timer must be created with NewTimer or AfterFunc.
  40. type Timer struct {
  41. C <-chan Time
  42. r runtimeTimer
  43. }
  44. // Stop prevents the Timer from firing.
  45. // It returns true if the call stops the timer, false if the timer has already
  46. // expired or been stopped.
  47. // Stop does not close the channel, to prevent a read from the channel succeeding
  48. // incorrectly.
  49. func (t *Timer) Stop() bool {
  50. if t.r.f == nil {
  51. panic("time: Stop called on uninitialized Timer")
  52. }
  53. return stopTimer(&t.r)
  54. }
  55. // NewTimer creates a new Timer that will send
  56. // the current time on its channel after at least duration d.
  57. func NewTimer(d Duration) *Timer {
  58. c := make(chan Time, 1)
  59. t := &Timer{
  60. C: c,
  61. r: runtimeTimer{
  62. when: when(d),
  63. f: sendTime,
  64. arg: c,
  65. },
  66. }
  67. startTimer(&t.r)
  68. return t
  69. }
  70. // Reset changes the timer to expire after duration d.
  71. // It returns true if the timer had been active, false if the timer had
  72. // expired or been stopped.
  73. func (t *Timer) Reset(d Duration) bool {
  74. if t.r.f == nil {
  75. panic("time: Reset called on uninitialized Timer")
  76. }
  77. w := when(d)
  78. active := stopTimer(&t.r)
  79. t.r.when = w
  80. startTimer(&t.r)
  81. return active
  82. }
  83. func sendTime(c interface{}, seq uintptr) {
  84. // Non-blocking send of time on c.
  85. // Used in NewTimer, it cannot block anyway (buffer).
  86. // Used in NewTicker, dropping sends on the floor is
  87. // the desired behavior when the reader gets behind,
  88. // because the sends are periodic.
  89. select {
  90. case c.(chan Time) <- Now():
  91. default:
  92. }
  93. }
  94. // After waits for the duration to elapse and then sends the current time
  95. // on the returned channel.
  96. // It is equivalent to NewTimer(d).C.
  97. func After(d Duration) <-chan Time {
  98. return NewTimer(d).C
  99. }
  100. // AfterFunc waits for the duration to elapse and then calls f
  101. // in its own goroutine. It returns a Timer that can
  102. // be used to cancel the call using its Stop method.
  103. func AfterFunc(d Duration, f func()) *Timer {
  104. t := &Timer{
  105. r: runtimeTimer{
  106. when: when(d),
  107. f: goFunc,
  108. arg: f,
  109. },
  110. }
  111. startTimer(&t.r)
  112. return t
  113. }
  114. func goFunc(arg interface{}, seq uintptr) {
  115. go arg.(func())()
  116. }