mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-05 03:27:48 +00:00
sync: implement sync.Map
This is a very simple implementation, just enough to get packages to compile.
This commit is contained in:
committed by
Ron Evans
parent
f8876ea245
commit
67c242173c
@@ -0,0 +1,44 @@
|
||||
package sync
|
||||
|
||||
// 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 Mutex
|
||||
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) 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
|
||||
}
|
||||
Reference in New Issue
Block a user