Skip to content

Commit 67c2421

Browse files
aykevldeadprogram
authored andcommitted
sync: implement sync.Map
This is a very simple implementation, just enough to get packages to compile.
1 parent f8876ea commit 67c2421

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/sync/map.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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) Store(key, value interface{}) {
38+
m.lock.Lock()
39+
defer m.lock.Unlock()
40+
if m.m == nil {
41+
m.m = make(map[interface{}]interface{})
42+
}
43+
m.m[key] = value
44+
}

0 commit comments

Comments
 (0)