Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crtime: add AtomicMono #7

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions crsync/typed_atomic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2024 The Cockroach 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 crsync

import "sync/atomic"

// TypedAtomicInt64 is a thin wrapper aorund atomic.Int64 that provides type
// safety.
type TypedAtomicInt64[T ~int64] struct {
v atomic.Int64
}

// Load atomically loads and returns the value stored in x.
func (x *TypedAtomicInt64[T]) Load() T { return T(x.v.Load()) }

// Store atomically stores val into x.
func (x *TypedAtomicInt64[T]) Store(val T) { x.v.Store(int64(val)) }

// Swap atomically stores new into x and returns the previous value.
func (x *TypedAtomicInt64[T]) Swap(new T) (old T) { return T(x.v.Swap(int64(new))) }

// CompareAndSwap executes the compare-and-swap operation for x.
func (x *TypedAtomicInt64[T]) CompareAndSwap(old, new T) (swapped bool) {
return x.v.CompareAndSwap(int64(old), int64(new))
}

// Add atomically adds delta to x and returns the new value.
func (x *TypedAtomicInt64[T]) Add(delta T) (new T) { return T(x.v.Add(int64(delta))) }
9 changes: 8 additions & 1 deletion crtime/monotonic.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

package crtime

import "time"
import (
"time"

"github.com/cockroachdb/crlib/crsync"
)

// Mono represents a moment in time in terms of a monotonic clock. Its value is
// the duration since the start of the process.
Expand Down Expand Up @@ -46,6 +50,9 @@ func MonoFromTime(t time.Time) Mono {
return Mono(t.Sub(startTime))
}

// AtomicMono provides atomic access to a Mono value.
type AtomicMono = crsync.TypedAtomicInt64[Mono]

// We use startTime as a reference point against which we can call
// time.Since(). This solution is suggested by the Go runtime code:
// https://github.com/golang/go/blob/889abb17e125bb0f5d8de61bb80ef15fbe2a130d/src/runtime/time_nofake.go#L19
Expand Down
Loading