github.com/go-ng/xatomic@v0.0.0-20230519181013-85c0ec87e55f/map.go (about) 1 package xatomic 2 3 import ( 4 "sync/atomic" 5 "unsafe" 6 7 "golang.org/x/exp/constraints" 8 ) 9 10 // StoreMap atomically stores a map, given a pointer to it. 11 // It is a high-efficient method, because `map` is a actually 12 // just a pointer to a structure, so this is effectively just 13 // an atomic.StorePointer call. 14 func StoreMap[K constraints.Ordered, V any](dst *map[K]V, src map[K]V) { 15 // This function works under assumption that type `map` is a pointer. 16 // 17 // This assumption is validated in map_test.go. 18 atomic.StorePointer( 19 (*unsafe.Pointer)((unsafe.Pointer)(dst)), 20 (unsafe.Pointer)(unref(unsafe.Pointer(&src))), 21 ) 22 } 23 24 // LoadMap atomically loads a map, given a pointer to it. 25 // It is a high-efficient method, because `map` is a actually 26 // just a pointer to a structure, so this is effectively just 27 // an atomic.LoadPointer call. 28 func LoadMap[K constraints.Ordered, V any](src *map[K]V) map[K]V { 29 // This function works under assumption that type `map` is a pointer. 30 // 31 // This assumption is validated in map_test.go. 32 return *(*map[K]V)(ref(atomic.LoadPointer( 33 (*unsafe.Pointer)((unsafe.Pointer)(src))), 34 )) 35 }