diff options
Diffstat (limited to 'vendor/codeberg.org/gruf/go-atomics')
-rw-r--r-- | vendor/codeberg.org/gruf/go-atomics/LICENSE | 9 | ||||
-rw-r--r-- | vendor/codeberg.org/gruf/go-atomics/README.md | 3 | ||||
-rw-r--r-- | vendor/codeberg.org/gruf/go-atomics/atomic.tpl | 57 | ||||
-rw-r--r-- | vendor/codeberg.org/gruf/go-atomics/atomic_test.tpl | 60 | ||||
-rw-r--r-- | vendor/codeberg.org/gruf/go-atomics/bool.go | 47 | ||||
-rw-r--r-- | vendor/codeberg.org/gruf/go-atomics/bytes.go | 57 | ||||
-rw-r--r-- | vendor/codeberg.org/gruf/go-atomics/error.go | 57 | ||||
-rw-r--r-- | vendor/codeberg.org/gruf/go-atomics/flags.go | 97 | ||||
-rw-r--r-- | vendor/codeberg.org/gruf/go-atomics/int.go | 69 | ||||
-rw-r--r-- | vendor/codeberg.org/gruf/go-atomics/interface.go | 57 | ||||
-rw-r--r-- | vendor/codeberg.org/gruf/go-atomics/state.go | 58 | ||||
-rw-r--r-- | vendor/codeberg.org/gruf/go-atomics/string.go | 57 | ||||
-rw-r--r-- | vendor/codeberg.org/gruf/go-atomics/time.go | 58 | ||||
-rw-r--r-- | vendor/codeberg.org/gruf/go-atomics/uint.go | 69 |
14 files changed, 755 insertions, 0 deletions
diff --git a/vendor/codeberg.org/gruf/go-atomics/LICENSE b/vendor/codeberg.org/gruf/go-atomics/LICENSE new file mode 100644 index 000000000..e4163ae35 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-atomics/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2022 gruf + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/vendor/codeberg.org/gruf/go-atomics/README.md b/vendor/codeberg.org/gruf/go-atomics/README.md new file mode 100644 index 000000000..38ba53276 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-atomics/README.md @@ -0,0 +1,3 @@ +# go-atomics + +This library provides a variety of types for atomic operations on common Go types.
\ No newline at end of file diff --git a/vendor/codeberg.org/gruf/go-atomics/atomic.tpl b/vendor/codeberg.org/gruf/go-atomics/atomic.tpl new file mode 100644 index 000000000..00134c1e8 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-atomics/atomic.tpl @@ -0,0 +1,57 @@ +package atomics + +import ( + "sync/atomic" + "unsafe" +) + +// {{ .Name }} provides user-friendly means of performing atomic operations on {{ .Type }} types. +type {{ .Name }} struct{ ptr unsafe.Pointer } + +// New{{ .Name }} will return a new {{ .Name }} instance initialized with zero value. +func New{{ .Name }}() *{{ .Name }} { + var v {{ .Type }} + return &{{ .Name }}{ + ptr: unsafe.Pointer(&v), + } +} + +// Store will atomically store {{ .Type }} value in address contained within v. +func (v *{{ .Name }}) Store(val {{ .Type }}) { + atomic.StorePointer(&v.ptr, unsafe.Pointer(&val)) +} + +// Load will atomically load {{ .Type }} value at address contained within v. +func (v *{{ .Name }}) Load() {{ .Type }} { + return *(*{{ .Type }})(atomic.LoadPointer(&v.ptr)) +} + +// CAS performs a compare-and-swap for a(n) {{ .Type }} value at address contained within v. +func (v *{{ .Name }}) CAS(cmp, swp {{ .Type }}) bool { + for { + // Load current value at address + ptr := atomic.LoadPointer(&v.ptr) + cur := *(*{{ .Type }})(ptr) + + // Perform comparison against current + if !({{ call .Compare "cur" "cmp" }}) { + return false + } + + // Attempt to replace pointer + if atomic.CompareAndSwapPointer( + &v.ptr, + ptr, + unsafe.Pointer(&swp), + ) { + return true + } + } +} + +// Swap atomically stores new {{ .Type }} value into address contained within v, and returns previous value. +func (v *{{ .Name }}) Swap(swp {{ .Type }}) {{ .Type }} { + ptr := unsafe.Pointer(&swp) + ptr = atomic.SwapPointer(&v.ptr, ptr) + return *(*{{ .Type }})(ptr) +} diff --git a/vendor/codeberg.org/gruf/go-atomics/atomic_test.tpl b/vendor/codeberg.org/gruf/go-atomics/atomic_test.tpl new file mode 100644 index 000000000..4e659d81f --- /dev/null +++ b/vendor/codeberg.org/gruf/go-atomics/atomic_test.tpl @@ -0,0 +1,60 @@ +package atomics_test + +import ( + "atomic" + "unsafe" + "testing" + + "codeberg.org/gruf/go-atomics" +) + +func Test{{ .Name }}StoreLoad(t *testing.T) { + for _, test := range {{ .Name }}Tests { + val := atomics.New{{ .Name }}() + + val.Store(test.V1) + + if !({{ call .Compare "val.Load()" "test.V1" }}) { + t.Fatalf("failed testing .Store and .Load: expect=%v actual=%v", val.Load(), test.V1) + } + + val.Store(test.V2) + + if !({{ call .Compare "val.Load()" "test.V2" }}) { + t.Fatalf("failed testing .Store and .Load: expect=%v actual=%v", val.Load(), test.V2) + } + } +} + +func Test{{ .Name }}CAS(t *testing.T) { + for _, test := range {{ .Name }}Tests { + val := atomics.New{{ .Name }}() + + val.Store(test.V1) + + if val.CAS(test.V2, test.V1) { + t.Fatalf("failed testing negative .CAS: test=%+v state=%v", test, val.Load()) + } + + if !val.CAS(test.V1, test.V2) { + t.Fatalf("failed testing positive .CAS: test=%+v state=%v", test, val.Load()) + } + } +} + +func Test{{ .Name }}Swap(t *testing.T) { + for _, test := range {{ .Name }}Tests { + val := atomics.New{{ .Name }}() + + val.Store(test.V1) + + if !({{ call .Compare "val.Swap(test.V2)" "test.V1" }}) { + t.Fatal("failed testing .Swap") + } + + if !({{ call .Compare "val.Swap(test.V1)" "test.V2" }}) { + t.Fatal("failed testing .Swap") + } + } +} + diff --git a/vendor/codeberg.org/gruf/go-atomics/bool.go b/vendor/codeberg.org/gruf/go-atomics/bool.go new file mode 100644 index 000000000..52660d356 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-atomics/bool.go @@ -0,0 +1,47 @@ +package atomics + +import "sync/atomic" + +// Bool provides user-friendly means of performing atomic operations on bool types. +type Bool uint32 + +// NewBool will return a new Bool instance initialized with zero value. +func NewBool() *Bool { + return new(Bool) +} + +// Store will atomically store bool value in address contained within i. +func (b *Bool) Store(val bool) { + atomic.StoreUint32((*uint32)(b), fromBool(val)) +} + +// Load will atomically load bool value at address contained within i. +func (b *Bool) Load() bool { + return toBool(atomic.LoadUint32((*uint32)(b))) +} + +// CAS performs a compare-and-swap for a(n) bool value at address contained within i. +func (b *Bool) CAS(cmp, swp bool) bool { + return atomic.CompareAndSwapUint32((*uint32)(b), fromBool(cmp), fromBool(swp)) +} + +// Swap atomically stores new bool value into address contained within i, and returns previous value. +func (b *Bool) Swap(swp bool) bool { + return toBool(atomic.SwapUint32((*uint32)(b), fromBool(swp))) +} + +// toBool converts uint32 value to bool. +func toBool(u uint32) bool { + if u == 0 { + return false + } + return true +} + +// fromBool converts from bool to uint32 value. +func fromBool(b bool) uint32 { + if b { + return 1 + } + return 0 +} diff --git a/vendor/codeberg.org/gruf/go-atomics/bytes.go b/vendor/codeberg.org/gruf/go-atomics/bytes.go new file mode 100644 index 000000000..3e40d186c --- /dev/null +++ b/vendor/codeberg.org/gruf/go-atomics/bytes.go @@ -0,0 +1,57 @@ +package atomics + +import ( + "sync/atomic" + "unsafe" +) + +// Bytes provides user-friendly means of performing atomic operations on []byte types. +type Bytes struct{ ptr unsafe.Pointer } + +// NewBytes will return a new Bytes instance initialized with zero value. +func NewBytes() *Bytes { + var v []byte + return &Bytes{ + ptr: unsafe.Pointer(&v), + } +} + +// Store will atomically store []byte value in address contained within v. +func (v *Bytes) Store(val []byte) { + atomic.StorePointer(&v.ptr, unsafe.Pointer(&val)) +} + +// Load will atomically load []byte value at address contained within v. +func (v *Bytes) Load() []byte { + return *(*[]byte)(atomic.LoadPointer(&v.ptr)) +} + +// CAS performs a compare-and-swap for a(n) []byte value at address contained within v. +func (v *Bytes) CAS(cmp, swp []byte) bool { + for { + // Load current value at address + ptr := atomic.LoadPointer(&v.ptr) + cur := *(*[]byte)(ptr) + + // Perform comparison against current + if !(string(cur) == string(cmp)) { + return false + } + + // Attempt to replace pointer + if atomic.CompareAndSwapPointer( + &v.ptr, + ptr, + unsafe.Pointer(&swp), + ) { + return true + } + } +} + +// Swap atomically stores new []byte value into address contained within v, and returns previous value. +func (v *Bytes) Swap(swp []byte) []byte { + ptr := unsafe.Pointer(&swp) + ptr = atomic.SwapPointer(&v.ptr, ptr) + return *(*[]byte)(ptr) +} diff --git a/vendor/codeberg.org/gruf/go-atomics/error.go b/vendor/codeberg.org/gruf/go-atomics/error.go new file mode 100644 index 000000000..0ecc4e9ad --- /dev/null +++ b/vendor/codeberg.org/gruf/go-atomics/error.go @@ -0,0 +1,57 @@ +package atomics + +import ( + "sync/atomic" + "unsafe" +) + +// Error provides user-friendly means of performing atomic operations on error types. +type Error struct{ ptr unsafe.Pointer } + +// NewError will return a new Error instance initialized with zero value. +func NewError() *Error { + var v error + return &Error{ + ptr: unsafe.Pointer(&v), + } +} + +// Store will atomically store error value in address contained within v. +func (v *Error) Store(val error) { + atomic.StorePointer(&v.ptr, unsafe.Pointer(&val)) +} + +// Load will atomically load error value at address contained within v. +func (v *Error) Load() error { + return *(*error)(atomic.LoadPointer(&v.ptr)) +} + +// CAS performs a compare-and-swap for a(n) error value at address contained within v. +func (v *Error) CAS(cmp, swp error) bool { + for { + // Load current value at address + ptr := atomic.LoadPointer(&v.ptr) + cur := *(*error)(ptr) + + // Perform comparison against current + if !(cur == cmp) { + return false + } + + // Attempt to replace pointer + if atomic.CompareAndSwapPointer( + &v.ptr, + ptr, + unsafe.Pointer(&swp), + ) { + return true + } + } +} + +// Swap atomically stores new error value into address contained within v, and returns previous value. +func (v *Error) Swap(swp error) error { + ptr := unsafe.Pointer(&swp) + ptr = atomic.SwapPointer(&v.ptr, ptr) + return *(*error)(ptr) +} diff --git a/vendor/codeberg.org/gruf/go-atomics/flags.go b/vendor/codeberg.org/gruf/go-atomics/flags.go new file mode 100644 index 000000000..42176bece --- /dev/null +++ b/vendor/codeberg.org/gruf/go-atomics/flags.go @@ -0,0 +1,97 @@ +package atomics + +import ( + "sync/atomic" + + "codeberg.org/gruf/go-bitutil" +) + +// Flags32 provides user-friendly means of performing atomic operations on bitutil.Flags32 types. +type Flags32 bitutil.Flags32 + +// NewFlags32 will return a new Flags32 instance initialized with zero value. +func NewFlags32() *Flags32 { + return new(Flags32) +} + +// Get will atomically load a(n) bitutil.Flags32 value contained within f, and check if bit value is set. +func (f *Flags32) Get(bit uint8) bool { + return f.Load().Get(bit) +} + +// Set performs a compare-and-swap for a(n) bitutil.Flags32 with bit value set, at address contained within f. +func (f *Flags32) Set(bit uint8) bool { + cur := f.Load() + return f.CAS(cur, cur.Set(bit)) +} + +// Unset performs a compare-and-swap for a(n) bitutil.Flags32 with bit value unset, at address contained within f. +func (f *Flags32) Unset(bit uint8) bool { + cur := f.Load() + return f.CAS(cur, cur.Unset(bit)) +} + +// Store will atomically store bitutil.Flags32 value in address contained within f. +func (f *Flags32) Store(val bitutil.Flags32) { + atomic.StoreUint32((*uint32)(f), uint32(val)) +} + +// Load will atomically load bitutil.Flags32 value at address contained within f. +func (f *Flags32) Load() bitutil.Flags32 { + return bitutil.Flags32(atomic.LoadUint32((*uint32)(f))) +} + +// CAS performs a compare-and-swap for a(n) bitutil.Flags32 value at address contained within f. +func (f *Flags32) CAS(cmp, swp bitutil.Flags32) bool { + return atomic.CompareAndSwapUint32((*uint32)(f), uint32(cmp), uint32(swp)) +} + +// Swap atomically stores new bitutil.Flags32 value into address contained within f, and returns previous value. +func (f *Flags32) Swap(swp bitutil.Flags32) bitutil.Flags32 { + return bitutil.Flags32(atomic.SwapUint32((*uint32)(f), uint32(swp))) +} + +// Flags64 provides user-friendly means of performing atomic operations on bitutil.Flags64 types. +type Flags64 bitutil.Flags64 + +// NewFlags64 will return a new Flags64 instance initialized with zero value. +func NewFlags64() *Flags64 { + return new(Flags64) +} + +// Get will atomically load a(n) bitutil.Flags64 value contained within f, and check if bit value is set. +func (f *Flags64) Get(bit uint8) bool { + return f.Load().Get(bit) +} + +// Set performs a compare-and-swap for a(n) bitutil.Flags64 with bit value set, at address contained within f. +func (f *Flags64) Set(bit uint8) bool { + cur := f.Load() + return f.CAS(cur, cur.Set(bit)) +} + +// Unset performs a compare-and-swap for a(n) bitutil.Flags64 with bit value unset, at address contained within f. +func (f *Flags64) Unset(bit uint8) bool { + cur := f.Load() + return f.CAS(cur, cur.Unset(bit)) +} + +// Store will atomically store bitutil.Flags64 value in address contained within f. +func (f *Flags64) Store(val bitutil.Flags64) { + atomic.StoreUint64((*uint64)(f), uint64(val)) +} + +// Load will atomically load bitutil.Flags64 value at address contained within f. +func (f *Flags64) Load() bitutil.Flags64 { + return bitutil.Flags64(atomic.LoadUint64((*uint64)(f))) +} + +// CAS performs a compare-and-swap for a(n) bitutil.Flags64 value at address contained within f. +func (f *Flags64) CAS(cmp, swp bitutil.Flags64) bool { + return atomic.CompareAndSwapUint64((*uint64)(f), uint64(cmp), uint64(swp)) +} + +// Swap atomically stores new bitutil.Flags64 value into address contained within f, and returns previous value. +func (f *Flags64) Swap(swp bitutil.Flags64) bitutil.Flags64 { + return bitutil.Flags64(atomic.SwapUint64((*uint64)(f), uint64(swp))) +} diff --git a/vendor/codeberg.org/gruf/go-atomics/int.go b/vendor/codeberg.org/gruf/go-atomics/int.go new file mode 100644 index 000000000..019ca1034 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-atomics/int.go @@ -0,0 +1,69 @@ +package atomics + +import "sync/atomic" + +// Int32 provides user-friendly means of performing atomic operations on int32 types. +type Int32 int32 + +// NewInt32 will return a new Int32 instance initialized with zero value. +func NewInt32() *Int32 { + return new(Int32) +} + +// Add will atomically add int32 delta to value in address contained within i, returning new value. +func (i *Int32) Add(delta int32) int32 { + return atomic.AddInt32((*int32)(i), delta) +} + +// Store will atomically store int32 value in address contained within i. +func (i *Int32) Store(val int32) { + atomic.StoreInt32((*int32)(i), val) +} + +// Load will atomically load int32 value at address contained within i. +func (i *Int32) Load() int32 { + return atomic.LoadInt32((*int32)(i)) +} + +// CAS performs a compare-and-swap for a(n) int32 value at address contained within i. +func (i *Int32) CAS(cmp, swp int32) bool { + return atomic.CompareAndSwapInt32((*int32)(i), cmp, swp) +} + +// Swap atomically stores new int32 value into address contained within i, and returns previous value. +func (i *Int32) Swap(swp int32) int32 { + return atomic.SwapInt32((*int32)(i), swp) +} + +// Int64 provides user-friendly means of performing atomic operations on int64 types. +type Int64 int64 + +// NewInt64 will return a new Int64 instance initialized with zero value. +func NewInt64() *Int64 { + return new(Int64) +} + +// Add will atomically add int64 delta to value in address contained within i, returning new value. +func (i *Int64) Add(delta int64) int64 { + return atomic.AddInt64((*int64)(i), delta) +} + +// Store will atomically store int64 value in address contained within i. +func (i *Int64) Store(val int64) { + atomic.StoreInt64((*int64)(i), val) +} + +// Load will atomically load int64 value at address contained within i. +func (i *Int64) Load() int64 { + return atomic.LoadInt64((*int64)(i)) +} + +// CAS performs a compare-and-swap for a(n) int64 value at address contained within i. +func (i *Int64) CAS(cmp, swp int64) bool { + return atomic.CompareAndSwapInt64((*int64)(i), cmp, swp) +} + +// Swap atomically stores new int64 value into address contained within i, and returns previous value. +func (i *Int64) Swap(swp int64) int64 { + return atomic.SwapInt64((*int64)(i), swp) +} diff --git a/vendor/codeberg.org/gruf/go-atomics/interface.go b/vendor/codeberg.org/gruf/go-atomics/interface.go new file mode 100644 index 000000000..f0d1c4355 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-atomics/interface.go @@ -0,0 +1,57 @@ +package atomics + +import ( + "sync/atomic" + "unsafe" +) + +// Interface provides user-friendly means of performing atomic operations on interface{} types. +type Interface struct{ ptr unsafe.Pointer } + +// NewInterface will return a new Interface instance initialized with zero value. +func NewInterface() *Interface { + var v interface{} + return &Interface{ + ptr: unsafe.Pointer(&v), + } +} + +// Store will atomically store interface{} value in address contained within v. +func (v *Interface) Store(val interface{}) { + atomic.StorePointer(&v.ptr, unsafe.Pointer(&val)) +} + +// Load will atomically load interface{} value at address contained within v. +func (v *Interface) Load() interface{} { + return *(*interface{})(atomic.LoadPointer(&v.ptr)) +} + +// CAS performs a compare-and-swap for a(n) interface{} value at address contained within v. +func (v *Interface) CAS(cmp, swp interface{}) bool { + for { + // Load current value at address + ptr := atomic.LoadPointer(&v.ptr) + cur := *(*interface{})(ptr) + + // Perform comparison against current + if !(cur == cmp) { + return false + } + + // Attempt to replace pointer + if atomic.CompareAndSwapPointer( + &v.ptr, + ptr, + unsafe.Pointer(&swp), + ) { + return true + } + } +} + +// Swap atomically stores new interface{} value into address contained within v, and returns previous value. +func (v *Interface) Swap(swp interface{}) interface{} { + ptr := unsafe.Pointer(&swp) + ptr = atomic.SwapPointer(&v.ptr, ptr) + return *(*interface{})(ptr) +} diff --git a/vendor/codeberg.org/gruf/go-atomics/state.go b/vendor/codeberg.org/gruf/go-atomics/state.go new file mode 100644 index 000000000..21892f378 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-atomics/state.go @@ -0,0 +1,58 @@ +package atomics + +import "sync" + +// State provides user-friendly means of performing atomic-like +// operations on a uint32 state, and allowing callbacks on successful +// state change. This is a bit of a misnomer being where it is, as it +// actually uses a mutex under-the-hood. +type State struct { + mutex sync.Mutex + state uint32 +} + +// Store will update State value safely within mutex lock. +func (st *State) Store(val uint32) { + st.mutex.Lock() + st.state = val + st.mutex.Unlock() +} + +// Load will get value of State safely within mutex lock. +func (st *State) Load() uint32 { + st.mutex.Lock() + state := st.state + st.mutex.Unlock() + return state +} + +// WithLock performs fn within State mutex lock, useful if you want +// to just use State's mutex for locking instead of creating another. +func (st *State) WithLock(fn func()) { + st.mutex.Lock() + defer st.mutex.Unlock() + fn() +} + +// Update performs fn within State mutex lock, with the current state +// value provided as an argument, and return value used to update state. +func (st *State) Update(fn func(state uint32) uint32) { + st.mutex.Lock() + defer st.mutex.Unlock() + st.state = fn(st.state) +} + +// CAS performs a compare-and-swap on State, calling fn on success. Success value is also returned. +func (st *State) CAS(cmp, swp uint32, fn func()) (ok bool) { + // Acquire lock + st.mutex.Lock() + defer st.mutex.Unlock() + + // Perform CAS operation, fn() on success + if ok = (st.state == cmp); ok { + st.state = swp + fn() + } + + return +} diff --git a/vendor/codeberg.org/gruf/go-atomics/string.go b/vendor/codeberg.org/gruf/go-atomics/string.go new file mode 100644 index 000000000..a0a6e115f --- /dev/null +++ b/vendor/codeberg.org/gruf/go-atomics/string.go @@ -0,0 +1,57 @@ +package atomics + +import ( + "sync/atomic" + "unsafe" +) + +// String provides user-friendly means of performing atomic operations on string types. +type String struct{ ptr unsafe.Pointer } + +// NewString will return a new String instance initialized with zero value. +func NewString() *String { + var v string + return &String{ + ptr: unsafe.Pointer(&v), + } +} + +// Store will atomically store string value in address contained within v. +func (v *String) Store(val string) { + atomic.StorePointer(&v.ptr, unsafe.Pointer(&val)) +} + +// Load will atomically load string value at address contained within v. +func (v *String) Load() string { + return *(*string)(atomic.LoadPointer(&v.ptr)) +} + +// CAS performs a compare-and-swap for a(n) string value at address contained within v. +func (v *String) CAS(cmp, swp string) bool { + for { + // Load current value at address + ptr := atomic.LoadPointer(&v.ptr) + cur := *(*string)(ptr) + + // Perform comparison against current + if !(cur == cmp) { + return false + } + + // Attempt to replace pointer + if atomic.CompareAndSwapPointer( + &v.ptr, + ptr, + unsafe.Pointer(&swp), + ) { + return true + } + } +} + +// Swap atomically stores new string value into address contained within v, and returns previous value. +func (v *String) Swap(swp string) string { + ptr := unsafe.Pointer(&swp) + ptr = atomic.SwapPointer(&v.ptr, ptr) + return *(*string)(ptr) +} diff --git a/vendor/codeberg.org/gruf/go-atomics/time.go b/vendor/codeberg.org/gruf/go-atomics/time.go new file mode 100644 index 000000000..4bf20cc74 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-atomics/time.go @@ -0,0 +1,58 @@ +package atomics + +import ( + "sync/atomic" + "time" + "unsafe" +) + +// Time provides user-friendly means of performing atomic operations on time.Time types. +type Time struct{ ptr unsafe.Pointer } + +// NewTime will return a new Time instance initialized with zero value. +func NewTime() *Time { + var v time.Time + return &Time{ + ptr: unsafe.Pointer(&v), + } +} + +// Store will atomically store time.Time value in address contained within v. +func (v *Time) Store(val time.Time) { + atomic.StorePointer(&v.ptr, unsafe.Pointer(&val)) +} + +// Load will atomically load time.Time value at address contained within v. +func (v *Time) Load() time.Time { + return *(*time.Time)(atomic.LoadPointer(&v.ptr)) +} + +// CAS performs a compare-and-swap for a(n) time.Time value at address contained within v. +func (v *Time) CAS(cmp, swp time.Time) bool { + for { + // Load current value at address + ptr := atomic.LoadPointer(&v.ptr) + cur := *(*time.Time)(ptr) + + // Perform comparison against current + if !(cur.Equal(cmp)) { + return false + } + + // Attempt to replace pointer + if atomic.CompareAndSwapPointer( + &v.ptr, + ptr, + unsafe.Pointer(&swp), + ) { + return true + } + } +} + +// Swap atomically stores new time.Time value into address contained within v, and returns previous value. +func (v *Time) Swap(swp time.Time) time.Time { + ptr := unsafe.Pointer(&swp) + ptr = atomic.SwapPointer(&v.ptr, ptr) + return *(*time.Time)(ptr) +} diff --git a/vendor/codeberg.org/gruf/go-atomics/uint.go b/vendor/codeberg.org/gruf/go-atomics/uint.go new file mode 100644 index 000000000..087e231d1 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-atomics/uint.go @@ -0,0 +1,69 @@ +package atomics + +import "sync/atomic" + +// Uint32 provides user-friendly means of performing atomic operations on uint32 types. +type Uint32 uint32 + +// NewUint32 will return a new Uint32 instance initialized with zero value. +func NewUint32() *Uint32 { + return new(Uint32) +} + +// Add will atomically add uint32 delta to value in address contained within i, returning new value. +func (u *Uint32) Add(delta uint32) uint32 { + return atomic.AddUint32((*uint32)(u), delta) +} + +// Store will atomically store uint32 value in address contained within i. +func (u *Uint32) Store(val uint32) { + atomic.StoreUint32((*uint32)(u), val) +} + +// Load will atomically load uint32 value at address contained within i. +func (u *Uint32) Load() uint32 { + return atomic.LoadUint32((*uint32)(u)) +} + +// CAS performs a compare-and-swap for a(n) uint32 value at address contained within i. +func (u *Uint32) CAS(cmp, swp uint32) bool { + return atomic.CompareAndSwapUint32((*uint32)(u), cmp, swp) +} + +// Swap atomically stores new uint32 value into address contained within i, and returns previous value. +func (u *Uint32) Swap(swp uint32) uint32 { + return atomic.SwapUint32((*uint32)(u), swp) +} + +// Uint64 provides user-friendly means of performing atomic operations on uint64 types. +type Uint64 uint64 + +// NewUint64 will return a new Uint64 instance initialized with zero value. +func NewUint64() *Uint64 { + return new(Uint64) +} + +// Add will atomically add uint64 delta to value in address contained within i, returning new value. +func (u *Uint64) Add(delta uint64) uint64 { + return atomic.AddUint64((*uint64)(u), delta) +} + +// Store will atomically store uint64 value in address contained within i. +func (u *Uint64) Store(val uint64) { + atomic.StoreUint64((*uint64)(u), val) +} + +// Load will atomically load uint64 value at address contained within i. +func (u *Uint64) Load() uint64 { + return atomic.LoadUint64((*uint64)(u)) +} + +// CAS performs a compare-and-swap for a(n) uint64 value at address contained within i. +func (u *Uint64) CAS(cmp, swp uint64) bool { + return atomic.CompareAndSwapUint64((*uint64)(u), cmp, swp) +} + +// Swap atomically stores new uint64 value into address contained within i, and returns previous value. +func (u *Uint64) Swap(swp uint64) uint64 { + return atomic.SwapUint64((*uint64)(u), swp) +} |