Files
tinygo/src/sync/map.go
T
Jake Bailey e79edb58b2 all: clean up code with Go 1.24+ in mind (#5489)
* all: clean up code with Go 1.24+ in mind

* all: more old go cleanup

* all: even remove rand_fastrand64

* runtime: revert rand changes

* runtime: re-drop rand_fastrand64
2026-07-03 19:10:55 +02:00

122 lines
2.3 KiB
Go

package sync
import "internal/task"
// This file implements just enough of sync.Map to get packages to compile. It
// is no more efficient than a map with a lock.
type Map struct {
lock task.PMutex
m map[interface{}]interface{}
}
func (m *Map) Delete(key interface{}) {
m.lock.Lock()
defer m.lock.Unlock()
delete(m.m, key)
}
func (m *Map) Load(key interface{}) (value interface{}, ok bool) {
m.lock.Lock()
defer m.lock.Unlock()
value, ok = m.m[key]
return
}
func (m *Map) LoadOrStore(key, value interface{}) (actual interface{}, loaded bool) {
m.lock.Lock()
defer m.lock.Unlock()
if m.m == nil {
m.m = make(map[interface{}]interface{})
}
if existing, ok := m.m[key]; ok {
return existing, true
}
m.m[key] = value
return value, false
}
func (m *Map) LoadAndDelete(key interface{}) (value interface{}, loaded bool) {
m.lock.Lock()
defer m.lock.Unlock()
value, ok := m.m[key]
if !ok {
return nil, false
}
delete(m.m, key)
return value, true
}
func (m *Map) Store(key, value interface{}) {
m.lock.Lock()
defer m.lock.Unlock()
if m.m == nil {
m.m = make(map[interface{}]interface{})
}
m.m[key] = value
}
func (m *Map) Range(f func(key, value interface{}) bool) {
m.lock.Lock()
defer m.lock.Unlock()
if m.m == nil {
return
}
for k, v := range m.m {
if !f(k, v) {
break
}
}
}
func (m *Map) Clear() {
m.lock.Lock()
defer m.lock.Unlock()
clear(m.m)
}
// Swap replaces the value for the given key, and returns the old value if any.
func (m *Map) Swap(key, value any) (previous any, loaded bool) {
m.lock.Lock()
defer m.lock.Unlock()
if m.m == nil {
m.m = make(map[interface{}]interface{})
}
previous, loaded = m.m[key]
m.m[key] = value
return
}
// CompareAndSwap swaps the old and new values for an existing key if the value
// stored in the map is equal to old.
func (m *Map) CompareAndSwap(key, old, new any) (swapped bool) {
m.lock.Lock()
defer m.lock.Unlock()
if m.m == nil {
return false
}
value, ok := m.m[key]
if !ok || value != old {
return false
}
m.m[key] = new
return true
}
// CompareAndDelete deletes the entry for key if its value is equal to old.
func (m *Map) CompareAndDelete(key, old any) (deleted bool) {
m.lock.Lock()
defer m.lock.Unlock()
if m.m == nil {
return false
}
value, ok := m.m[key]
if !ok || value != old {
return false
}
delete(m.m, key)
return true
}