-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.go
More file actions
54 lines (46 loc) · 1.06 KB
/
map.go
File metadata and controls
54 lines (46 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package bugfruit
import "sync"
// muMap is a map that has a RWMutex on it.
type muMap struct {
data map[string]*datum
mu sync.RWMutex
}
func newMuMap() muMap {
return muMap{
data: make(map[string]*datum),
}
}
// Store sets a key/value pair in the map.
func (m *muMap) Store(key string, value *datum) {
m.mu.Lock()
defer m.mu.Unlock()
m.data[key] = value
}
// Load returns a key/value pair from the map,
// if it can, and whether the key exists in the map.
func (m *muMap) Load(key string) (*datum, bool) {
m.mu.RLock()
defer m.mu.RUnlock()
val, ok := m.data[key]
return val, ok
}
// Load returns a key/value pair from the map,
// if it can, and whether the key exists in the map.
// It deletes the key from the map if it existed.
func (m *muMap) LoadAndDelete(key string) (*datum, bool) {
m.mu.Lock()
defer m.mu.Unlock()
val, ok := m.data[key]
if ok {
delete(m.data, key)
}
return val, ok
}
// RLock locks muMap for reading.
func (m *muMap) RLock() {
m.mu.RLock()
}
// RUnlock unlocks muMap for reading.
func (m *muMap) RUnlock() {
m.mu.RUnlock()
}