github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/sync/map.go (about) 1 package sync 2 3 // This file implements just enough of sync.Map to get packages to compile. It 4 // is no more efficient than a map with a lock. 5 6 type Map struct { 7 lock Mutex 8 m map[interface{}]interface{} 9 } 10 11 func (m *Map) Delete(key interface{}) { 12 m.lock.Lock() 13 defer m.lock.Unlock() 14 delete(m.m, key) 15 } 16 17 func (m *Map) Load(key interface{}) (value interface{}, ok bool) { 18 m.lock.Lock() 19 defer m.lock.Unlock() 20 value, ok = m.m[key] 21 return 22 } 23 24 func (m *Map) LoadOrStore(key, value interface{}) (actual interface{}, loaded bool) { 25 m.lock.Lock() 26 defer m.lock.Unlock() 27 if m.m == nil { 28 m.m = make(map[interface{}]interface{}) 29 } 30 if existing, ok := m.m[key]; ok { 31 return existing, true 32 } 33 m.m[key] = value 34 return value, false 35 } 36 37 func (m *Map) LoadAndDelete(key interface{}) (value interface{}, loaded bool) { 38 m.lock.Lock() 39 defer m.lock.Unlock() 40 value, ok := m.m[key] 41 if !ok { 42 return nil, false 43 } 44 delete(m.m, key) 45 return value, true 46 } 47 48 func (m *Map) Store(key, value interface{}) { 49 m.lock.Lock() 50 defer m.lock.Unlock() 51 if m.m == nil { 52 m.m = make(map[interface{}]interface{}) 53 } 54 m.m[key] = value 55 } 56 57 func (m *Map) Range(f func(key, value interface{}) bool) { 58 m.lock.Lock() 59 defer m.lock.Unlock() 60 61 if m.m == nil { 62 return 63 } 64 65 for k, v := range m.m { 66 if !f(k, v) { 67 break 68 } 69 } 70 }