You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.4 KiB
Go
72 lines
1.4 KiB
Go
7 months ago
|
package store
|
||
|
|
||
|
import (
|
||
|
"sync"
|
||
7 months ago
|
"sync/atomic"
|
||
7 months ago
|
"time"
|
||
|
)
|
||
|
|
||
|
// AtomicTime is a time.Time with atomic operations.
|
||
|
type AtomicTime struct {
|
||
|
t time.Time
|
||
|
mu sync.RWMutex
|
||
|
}
|
||
|
|
||
|
// NewAtomicTime returns a new AtomicTime.
|
||
|
func NewAtomicTime() *AtomicTime {
|
||
|
return &AtomicTime{}
|
||
|
}
|
||
|
|
||
|
// Store stores a new time.
|
||
|
func (t *AtomicTime) Store(newTime time.Time) {
|
||
|
t.mu.Lock()
|
||
|
defer t.mu.Unlock()
|
||
|
t.t = newTime
|
||
|
}
|
||
|
|
||
|
// Load returns the stored time.
|
||
|
func (t *AtomicTime) Load() time.Time {
|
||
|
t.mu.RLock()
|
||
|
defer t.mu.RUnlock()
|
||
|
return t.t
|
||
|
}
|
||
|
|
||
|
// Add adds a duration to the stored time.
|
||
|
func (t *AtomicTime) Add(d time.Duration) {
|
||
|
t.mu.Lock()
|
||
|
defer t.mu.Unlock()
|
||
|
t.t = t.t.Add(d)
|
||
|
}
|
||
|
|
||
|
// Sub return the difference between the stored time and the given time.
|
||
|
func (t *AtomicTime) Sub(tt *AtomicTime) time.Duration {
|
||
|
t.mu.RLock()
|
||
|
defer t.mu.RUnlock()
|
||
|
return t.t.Sub(tt.t)
|
||
|
}
|
||
7 months ago
|
|
||
|
// AtomicBool is a boolean with atomic operations.
|
||
|
type AtomicBool struct {
|
||
|
state int32 // 1 for true, 0 for false
|
||
|
}
|
||
|
|
||
|
// NewAtomicBool returns a new AtomicBool initialized to false.
|
||
|
func NewAtomicBool() *AtomicBool {
|
||
|
return &AtomicBool{state: 0}
|
||
|
}
|
||
|
|
||
|
// Set sets the AtomicBool to true.
|
||
|
func (b *AtomicBool) Set() {
|
||
|
atomic.StoreInt32(&b.state, 1)
|
||
|
}
|
||
|
|
||
|
// Unset sets the AtomicBool to false.
|
||
|
func (b *AtomicBool) Unset() {
|
||
|
atomic.StoreInt32(&b.state, 0)
|
||
|
}
|
||
|
|
||
|
// Is returns true if the AtomicBool is true, false otherwise.
|
||
|
func (b *AtomicBool) Is() bool {
|
||
|
return atomic.LoadInt32(&b.state) == 1
|
||
|
}
|