2016-10-14 05:51:29 +02:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-06-14 02:52:50 -07:00
|
|
|
// Package mclock is a wrapper for a monotonic clock source
|
2016-10-14 05:51:29 +02:00
|
|
|
package mclock
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2021-01-22 20:15:27 +01:00
|
|
|
_ "unsafe" // for go:linkname
|
2016-10-14 05:51:29 +02:00
|
|
|
)
|
|
|
|
|
2021-01-22 20:15:27 +01:00
|
|
|
//go:noescape
|
|
|
|
//go:linkname nanotime runtime.nanotime
|
|
|
|
func nanotime() int64
|
|
|
|
|
2018-06-14 02:52:50 -07:00
|
|
|
// AbsTime represents absolute monotonic time.
|
2021-01-22 20:15:27 +01:00
|
|
|
type AbsTime int64
|
2016-10-14 05:51:29 +02:00
|
|
|
|
2018-06-14 02:52:50 -07:00
|
|
|
// Now returns the current absolute monotonic time.
|
2016-10-14 05:51:29 +02:00
|
|
|
func Now() AbsTime {
|
2021-01-22 20:15:27 +01:00
|
|
|
return AbsTime(nanotime())
|
2016-10-14 05:51:29 +02:00
|
|
|
}
|
2018-08-14 22:44:46 +02:00
|
|
|
|
2020-02-11 16:36:49 +01:00
|
|
|
// Add returns t + d as absolute time.
|
2018-08-14 22:44:46 +02:00
|
|
|
func (t AbsTime) Add(d time.Duration) AbsTime {
|
|
|
|
return t + AbsTime(d)
|
|
|
|
}
|
|
|
|
|
2020-02-11 16:36:49 +01:00
|
|
|
// Sub returns t - t2 as a duration.
|
|
|
|
func (t AbsTime) Sub(t2 AbsTime) time.Duration {
|
|
|
|
return time.Duration(t - t2)
|
|
|
|
}
|
|
|
|
|
2019-09-16 11:16:30 +02:00
|
|
|
// The Clock interface makes it possible to replace the monotonic system clock with
|
2018-08-14 22:44:46 +02:00
|
|
|
// a simulated clock.
|
|
|
|
type Clock interface {
|
|
|
|
Now() AbsTime
|
|
|
|
Sleep(time.Duration)
|
2020-02-11 16:36:49 +01:00
|
|
|
NewTimer(time.Duration) ChanTimer
|
|
|
|
After(time.Duration) <-chan AbsTime
|
2019-09-16 11:16:30 +02:00
|
|
|
AfterFunc(d time.Duration, f func()) Timer
|
2019-08-03 14:36:10 +02:00
|
|
|
}
|
|
|
|
|
2020-02-11 16:36:49 +01:00
|
|
|
// Timer is a cancellable event created by AfterFunc.
|
2019-09-16 11:16:30 +02:00
|
|
|
type Timer interface {
|
2020-02-11 16:36:49 +01:00
|
|
|
// Stop cancels the timer. It returns false if the timer has already
|
|
|
|
// expired or been stopped.
|
2019-09-16 11:16:30 +02:00
|
|
|
Stop() bool
|
2018-08-14 22:44:46 +02:00
|
|
|
}
|
|
|
|
|
2020-02-11 16:36:49 +01:00
|
|
|
// ChanTimer is a cancellable event created by NewTimer.
|
|
|
|
type ChanTimer interface {
|
|
|
|
Timer
|
|
|
|
|
|
|
|
// The channel returned by C receives a value when the timer expires.
|
|
|
|
C() <-chan AbsTime
|
|
|
|
// Reset reschedules the timer with a new timeout.
|
|
|
|
// It should be invoked only on stopped or expired timers with drained channels.
|
|
|
|
Reset(time.Duration)
|
|
|
|
}
|
|
|
|
|
2018-08-14 22:44:46 +02:00
|
|
|
// System implements Clock using the system clock.
|
|
|
|
type System struct{}
|
|
|
|
|
2019-09-16 11:16:30 +02:00
|
|
|
// Now returns the current monotonic time.
|
2020-02-11 16:36:49 +01:00
|
|
|
func (c System) Now() AbsTime {
|
2021-01-22 20:15:27 +01:00
|
|
|
return Now()
|
2018-08-14 22:44:46 +02:00
|
|
|
}
|
|
|
|
|
2019-09-16 11:16:30 +02:00
|
|
|
// Sleep blocks for the given duration.
|
2020-02-11 16:36:49 +01:00
|
|
|
func (c System) Sleep(d time.Duration) {
|
2018-08-14 22:44:46 +02:00
|
|
|
time.Sleep(d)
|
|
|
|
}
|
|
|
|
|
2020-02-11 16:36:49 +01:00
|
|
|
// NewTimer creates a timer which can be rescheduled.
|
|
|
|
func (c System) NewTimer(d time.Duration) ChanTimer {
|
|
|
|
ch := make(chan AbsTime, 1)
|
|
|
|
t := time.AfterFunc(d, func() {
|
|
|
|
// This send is non-blocking because that's how time.Timer
|
|
|
|
// behaves. It doesn't matter in the happy case, but does
|
|
|
|
// when Reset is misused.
|
|
|
|
select {
|
|
|
|
case ch <- c.Now():
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return &systemTimer{t, ch}
|
|
|
|
}
|
|
|
|
|
2019-09-16 11:16:30 +02:00
|
|
|
// After returns a channel which receives the current time after d has elapsed.
|
2020-02-11 16:36:49 +01:00
|
|
|
func (c System) After(d time.Duration) <-chan AbsTime {
|
|
|
|
ch := make(chan AbsTime, 1)
|
|
|
|
time.AfterFunc(d, func() { ch <- c.Now() })
|
|
|
|
return ch
|
2018-08-14 22:44:46 +02:00
|
|
|
}
|
2019-08-03 14:36:10 +02:00
|
|
|
|
2019-09-16 11:16:30 +02:00
|
|
|
// AfterFunc runs f on a new goroutine after the duration has elapsed.
|
2020-02-11 16:36:49 +01:00
|
|
|
func (c System) AfterFunc(d time.Duration, f func()) Timer {
|
2019-09-16 11:16:30 +02:00
|
|
|
return time.AfterFunc(d, f)
|
2019-08-03 14:36:10 +02:00
|
|
|
}
|
2020-02-11 16:36:49 +01:00
|
|
|
|
|
|
|
type systemTimer struct {
|
|
|
|
*time.Timer
|
|
|
|
ch <-chan AbsTime
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *systemTimer) Reset(d time.Duration) {
|
|
|
|
st.Timer.Reset(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *systemTimer) C() <-chan AbsTime {
|
|
|
|
return st.ch
|
|
|
|
}
|