-
Notifications
You must be signed in to change notification settings - Fork 51
/
mutex_map.go
44 lines (38 loc) · 922 Bytes
/
mutex_map.go
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
package dnsproxy
import (
"sync"
)
// mutexMap
type mutexMap struct {
// as the mutex map has a map of its own
// we also need to lock access to this map
l sync.Mutex
m map[string]*sync.Mutex
}
// unlocker defines the Unlock mechanism which is returned by the Lock method
type unlocker interface {
// Unlock releases the lock
Unlock()
}
// newMutexMap initializes a new map of strings which provide a mutex
func newMutexMap() *mutexMap {
return &mutexMap{m: map[string]*sync.Mutex{}}
}
// Remove removes an entry from the mutex map
func (m *mutexMap) Remove(entry string) {
m.l.Lock()
defer m.l.Unlock()
delete(m.m, entry)
}
// Lock will gain a lock on `entry`. The caller must call `Unlock` on the returned unlocker when done.
func (m *mutexMap) Lock(entry string) unlocker {
m.l.Lock()
e, ok := m.m[entry]
if !ok {
m.m[entry] = &sync.Mutex{}
e = m.m[entry]
}
m.l.Unlock()
e.Lock()
return e
}