1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tcpip
import (
"fmt"
"time"
)
// stdClock implements Clock with the time package.
//
// +stateify savable
type stdClock struct {
// baseTime holds the time when the clock was constructed.
//
// This value is used to calculate the monotonic time from the time package.
// As per https://golang.org/pkg/time/#hdr-Monotonic_Clocks,
//
// Operating systems provide both a “wall clock,” which is subject to
// changes for clock synchronization, and a “monotonic clock,” which is not.
// The general rule is that the wall clock is for telling time and the
// monotonic clock is for measuring time. Rather than split the API, in this
// package the Time returned by time.Now contains both a wall clock reading
// and a monotonic clock reading; later time-telling operations use the wall
// clock reading, but later time-measuring operations, specifically
// comparisons and subtractions, use the monotonic clock reading.
//
// ...
//
// If Times t and u both contain monotonic clock readings, the operations
// t.After(u), t.Before(u), t.Equal(u), and t.Sub(u) are carried out using
// the monotonic clock readings alone, ignoring the wall clock readings. If
// either t or u contains no monotonic clock reading, these operations fall
// back to using the wall clock readings.
//
// Given the above, we can safely conclude that time.Since(baseTime) will
// return monotonically increasing values if we use time.Now() to set baseTime
// at the time of clock construction.
//
// Note that time.Since(t) is shorthand for time.Now().Sub(t), as per
// https://golang.org/pkg/time/#Since.
baseTime time.Time `state:"nosave"`
// monotonicOffset is the offset applied to the calculated monotonic time.
//
// monotonicOffset is assigned after restore so that the monotonic time
// will continue from where it "left off" before saving as part of S/R.
monotonicOffset MonotonicTime
}
// NewStdClock returns an instance of a clock that uses the time package.
func NewStdClock() Clock {
return &stdClock{
baseTime: time.Now(),
}
}
var _ Clock = (*stdClock)(nil)
// Now implements Clock.Now.
func (*stdClock) Now() time.Time {
return time.Now()
}
// NowMonotonic implements Clock.NowMonotonic.
func (s *stdClock) NowMonotonic() MonotonicTime {
sinceBase := time.Since(s.baseTime)
if sinceBase < 0 {
panic(fmt.Sprintf("got negative duration = %s since base time = %s", sinceBase, s.baseTime))
}
return s.monotonicOffset.Add(sinceBase)
}
// AfterFunc implements Clock.AfterFunc.
func (*stdClock) AfterFunc(d time.Duration, f func()) Timer {
return &stdTimer{
t: time.AfterFunc(d, f),
}
}
// +stateify savable
type stdTimer struct {
t *time.Timer
}
var _ Timer = (*stdTimer)(nil)
// Stop implements Timer.Stop.
func (st *stdTimer) Stop() bool {
return st.t.Stop()
}
// Reset implements Timer.Reset.
func (st *stdTimer) Reset(d time.Duration) {
st.t.Reset(d)
}
// NewStdTimer returns a Timer implemented with the time package.
func NewStdTimer(t *time.Timer) Timer {
return &stdTimer{t: t}
}
|