mclock.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Package mclock is a wrapper for a monotonic clock source
  17. package mclock
  18. import (
  19. "time"
  20. _ "unsafe" // for go:linkname
  21. )
  22. //go:noescape
  23. //go:linkname nanotime runtime.nanotime
  24. func nanotime() int64
  25. // AbsTime represents absolute monotonic time.
  26. type AbsTime int64
  27. // Now returns the current absolute monotonic time.
  28. func Now() AbsTime {
  29. return AbsTime(nanotime())
  30. }
  31. // Add returns t + d as absolute time.
  32. func (t AbsTime) Add(d time.Duration) AbsTime {
  33. return t + AbsTime(d)
  34. }
  35. // Sub returns t - t2 as a duration.
  36. func (t AbsTime) Sub(t2 AbsTime) time.Duration {
  37. return time.Duration(t - t2)
  38. }
  39. // The Clock interface makes it possible to replace the monotonic system clock with
  40. // a simulated clock.
  41. type Clock interface {
  42. Now() AbsTime
  43. Sleep(time.Duration)
  44. NewTimer(time.Duration) ChanTimer
  45. After(time.Duration) <-chan AbsTime
  46. AfterFunc(d time.Duration, f func()) Timer
  47. }
  48. // Timer is a cancellable event created by AfterFunc.
  49. type Timer interface {
  50. // Stop cancels the timer. It returns false if the timer has already
  51. // expired or been stopped.
  52. Stop() bool
  53. }
  54. // ChanTimer is a cancellable event created by NewTimer.
  55. type ChanTimer interface {
  56. Timer
  57. // The channel returned by C receives a value when the timer expires.
  58. C() <-chan AbsTime
  59. // Reset reschedules the timer with a new timeout.
  60. // It should be invoked only on stopped or expired timers with drained channels.
  61. Reset(time.Duration)
  62. }
  63. // System implements Clock using the system clock.
  64. type System struct{}
  65. // Now returns the current monotonic time.
  66. func (c System) Now() AbsTime {
  67. return Now()
  68. }
  69. // Sleep blocks for the given duration.
  70. func (c System) Sleep(d time.Duration) {
  71. time.Sleep(d)
  72. }
  73. // NewTimer creates a timer which can be rescheduled.
  74. func (c System) NewTimer(d time.Duration) ChanTimer {
  75. ch := make(chan AbsTime, 1)
  76. t := time.AfterFunc(d, func() {
  77. // This send is non-blocking because that's how time.Timer
  78. // behaves. It doesn't matter in the happy case, but does
  79. // when Reset is misused.
  80. select {
  81. case ch <- c.Now():
  82. default:
  83. }
  84. })
  85. return &systemTimer{t, ch}
  86. }
  87. // After returns a channel which receives the current time after d has elapsed.
  88. func (c System) After(d time.Duration) <-chan AbsTime {
  89. ch := make(chan AbsTime, 1)
  90. time.AfterFunc(d, func() { ch <- c.Now() })
  91. return ch
  92. }
  93. // AfterFunc runs f on a new goroutine after the duration has elapsed.
  94. func (c System) AfterFunc(d time.Duration, f func()) Timer {
  95. return time.AfterFunc(d, f)
  96. }
  97. type systemTimer struct {
  98. *time.Timer
  99. ch <-chan AbsTime
  100. }
  101. func (st *systemTimer) Reset(d time.Duration) {
  102. st.Timer.Reset(d)
  103. }
  104. func (st *systemTimer) C() <-chan AbsTime {
  105. return st.ch
  106. }