gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

flags.go (3466B)


      1 package atomics
      2 
      3 import (
      4 	"sync/atomic"
      5 
      6 	"codeberg.org/gruf/go-bitutil"
      7 )
      8 
      9 // Flags32 provides user-friendly means of performing atomic operations on bitutil.Flags32 types.
     10 type Flags32 bitutil.Flags32
     11 
     12 // NewFlags32 will return a new Flags32 instance initialized with zero value.
     13 func NewFlags32() *Flags32 {
     14 	return new(Flags32)
     15 }
     16 
     17 // Get will atomically load a(n) bitutil.Flags32 value contained within f, and check if bit value is set.
     18 func (f *Flags32) Get(bit uint8) bool {
     19 	return f.Load().Get(bit)
     20 }
     21 
     22 // Set performs a compare-and-swap for a(n) bitutil.Flags32 with bit value set, at address contained within f.
     23 func (f *Flags32) Set(bit uint8) bool {
     24 	cur := f.Load()
     25 	return f.CAS(cur, cur.Set(bit))
     26 }
     27 
     28 // Unset performs a compare-and-swap for a(n) bitutil.Flags32 with bit value unset, at address contained within f.
     29 func (f *Flags32) Unset(bit uint8) bool {
     30 	cur := f.Load()
     31 	return f.CAS(cur, cur.Unset(bit))
     32 }
     33 
     34 // Store will atomically store bitutil.Flags32 value in address contained within f.
     35 func (f *Flags32) Store(val bitutil.Flags32) {
     36 	atomic.StoreUint32((*uint32)(f), uint32(val))
     37 }
     38 
     39 // Load will atomically load bitutil.Flags32 value at address contained within f.
     40 func (f *Flags32) Load() bitutil.Flags32 {
     41 	return bitutil.Flags32(atomic.LoadUint32((*uint32)(f)))
     42 }
     43 
     44 // CAS performs a compare-and-swap for a(n) bitutil.Flags32 value at address contained within f.
     45 func (f *Flags32) CAS(cmp, swp bitutil.Flags32) bool {
     46 	return atomic.CompareAndSwapUint32((*uint32)(f), uint32(cmp), uint32(swp))
     47 }
     48 
     49 // Swap atomically stores new bitutil.Flags32 value into address contained within f, and returns previous value.
     50 func (f *Flags32) Swap(swp bitutil.Flags32) bitutil.Flags32 {
     51 	return bitutil.Flags32(atomic.SwapUint32((*uint32)(f), uint32(swp)))
     52 }
     53 
     54 // Flags64 provides user-friendly means of performing atomic operations on bitutil.Flags64 types.
     55 type Flags64 bitutil.Flags64
     56 
     57 // NewFlags64 will return a new Flags64 instance initialized with zero value.
     58 func NewFlags64() *Flags64 {
     59 	return new(Flags64)
     60 }
     61 
     62 // Get will atomically load a(n) bitutil.Flags64 value contained within f, and check if bit value is set.
     63 func (f *Flags64) Get(bit uint8) bool {
     64 	return f.Load().Get(bit)
     65 }
     66 
     67 // Set performs a compare-and-swap for a(n) bitutil.Flags64 with bit value set, at address contained within f.
     68 func (f *Flags64) Set(bit uint8) bool {
     69 	cur := f.Load()
     70 	return f.CAS(cur, cur.Set(bit))
     71 }
     72 
     73 // Unset performs a compare-and-swap for a(n) bitutil.Flags64 with bit value unset, at address contained within f.
     74 func (f *Flags64) Unset(bit uint8) bool {
     75 	cur := f.Load()
     76 	return f.CAS(cur, cur.Unset(bit))
     77 }
     78 
     79 // Store will atomically store bitutil.Flags64 value in address contained within f.
     80 func (f *Flags64) Store(val bitutil.Flags64) {
     81 	atomic.StoreUint64((*uint64)(f), uint64(val))
     82 }
     83 
     84 // Load will atomically load bitutil.Flags64 value at address contained within f.
     85 func (f *Flags64) Load() bitutil.Flags64 {
     86 	return bitutil.Flags64(atomic.LoadUint64((*uint64)(f)))
     87 }
     88 
     89 // CAS performs a compare-and-swap for a(n) bitutil.Flags64 value at address contained within f.
     90 func (f *Flags64) CAS(cmp, swp bitutil.Flags64) bool {
     91 	return atomic.CompareAndSwapUint64((*uint64)(f), uint64(cmp), uint64(swp))
     92 }
     93 
     94 // Swap atomically stores new bitutil.Flags64 value into address contained within f, and returns previous value.
     95 func (f *Flags64) Swap(swp bitutil.Flags64) bitutil.Flags64 {
     96 	return bitutil.Flags64(atomic.SwapUint64((*uint64)(f), uint64(swp)))
     97 }