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
17// Package mclock is a wrapper for a monotonic clock source
18package mclock
19
20import (
21	"time"
22
23	_ "unsafe" // for go:linkname
24)
25
26//go:noescape
27//go:linkname nanotime runtime.nanotime
28func nanotime() int64
29
30// AbsTime represents absolute monotonic time.
31type AbsTime int64
32
33// Now returns the current absolute monotonic time.
34func Now() AbsTime {
35	return AbsTime(nanotime())
36}
37
38// Add returns t + d as absolute time.
39func (t AbsTime) Add(d time.Duration) AbsTime {
40	return t + AbsTime(d)
41}
42
43// Sub returns t - t2 as a duration.
44func (t AbsTime) Sub(t2 AbsTime) time.Duration {
45	return time.Duration(t - t2)
46}
47
48// The Clock interface makes it possible to replace the monotonic system clock with
49// a simulated clock.
50type Clock interface {
51	Now() AbsTime
52	Sleep(time.Duration)
53	NewTimer(time.Duration) ChanTimer
54	After(time.Duration) <-chan AbsTime
55	AfterFunc(d time.Duration, f func()) Timer
56}
57
58// Timer is a cancellable event created by AfterFunc.
59type Timer interface {
60	// Stop cancels the timer. It returns false if the timer has already
61	// expired or been stopped.
62	Stop() bool
63}
64
65// ChanTimer is a cancellable event created by NewTimer.
66type ChanTimer interface {
67	Timer
68
69	// The channel returned by C receives a value when the timer expires.
70	C() <-chan AbsTime
71	// Reset reschedules the timer with a new timeout.
72	// It should be invoked only on stopped or expired timers with drained channels.
73	Reset(time.Duration)
74}
75
76// System implements Clock using the system clock.
77type System struct{}
78
79// Now returns the current monotonic time.
80func (c System) Now() AbsTime {
81	return Now()
82}
83
84// Sleep blocks for the given duration.
85func (c System) Sleep(d time.Duration) {
86	time.Sleep(d)
87}
88
89// NewTimer creates a timer which can be rescheduled.
90func (c System) NewTimer(d time.Duration) ChanTimer {
91	ch := make(chan AbsTime, 1)
92	t := time.AfterFunc(d, func() {
93		// This send is non-blocking because that's how time.Timer
94		// behaves. It doesn't matter in the happy case, but does
95		// when Reset is misused.
96		select {
97		case ch <- c.Now():
98		default:
99		}
100	})
101	return &systemTimer{t, ch}
102}
103
104// After returns a channel which receives the current time after d has elapsed.
105func (c System) After(d time.Duration) <-chan AbsTime {
106	ch := make(chan AbsTime, 1)
107	time.AfterFunc(d, func() { ch <- c.Now() })
108	return ch
109}
110
111// AfterFunc runs f on a new goroutine after the duration has elapsed.
112func (c System) AfterFunc(d time.Duration, f func()) Timer {
113	return time.AfterFunc(d, f)
114}
115
116type systemTimer struct {
117	*time.Timer
118	ch <-chan AbsTime
119}
120
121func (st *systemTimer) Reset(d time.Duration) {
122	st.Timer.Reset(d)
123}
124
125func (st *systemTimer) C() <-chan AbsTime {
126	return st.ch
127}
128