github.com/gotranspile/cxgo@v0.3.7/runtime/libc/sync.go (about) 1 package libc 2 3 import ( 4 "sync" 5 "sync/atomic" 6 ) 7 8 type syncMap[K comparable, V any] struct { 9 m sync.Map 10 } 11 12 func (m *syncMap[K, V]) Store(k K, v V) { 13 m.m.Store(k, v) 14 } 15 16 func (m *syncMap[K, V]) Load(k K) (V, bool) { 17 vi, ok := m.m.Load(k) 18 if !ok { 19 var zero V 20 return zero, false 21 } 22 return vi.(V), true 23 } 24 25 func (m *syncMap[K, V]) Delete(k K) { 26 m.m.Delete(k) 27 } 28 29 // old = *p; *p (op)= val; return old; 30 31 func LoadAddInt32(p *int32, v int32) int32 { 32 nv := atomic.AddInt32(p, v) 33 return nv - v 34 } 35 36 func LoadSubInt32(p *int32, v int32) int32 { 37 nv := atomic.AddInt32(p, -v) 38 return nv + v 39 } 40 41 func LoadOrInt32(p *int32, v int32) int32 { 42 for { 43 old := atomic.LoadInt32(p) 44 nv := old | v 45 if atomic.CompareAndSwapInt32(p, old, nv) { 46 return old 47 } 48 } 49 } 50 51 func LoadAndInt32(p *int32, v int32) int32 { 52 for { 53 old := atomic.LoadInt32(p) 54 nv := old & v 55 if atomic.CompareAndSwapInt32(p, old, nv) { 56 return old 57 } 58 } 59 } 60 61 func LoadXorInt32(p *int32, v int32) int32 { 62 for { 63 old := atomic.LoadInt32(p) 64 nv := old ^ v 65 if atomic.CompareAndSwapInt32(p, old, nv) { 66 return old 67 } 68 } 69 } 70 71 func LoadNandInt32(p *int32, v int32) int32 { 72 for { 73 old := atomic.LoadInt32(p) 74 nv := ^old & v 75 if atomic.CompareAndSwapInt32(p, old, nv) { 76 return old 77 } 78 } 79 }