github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/controller/internal/enforcer/dnsproxy/mutex_map.go (about) 1 package dnsproxy 2 3 import ( 4 "sync" 5 ) 6 7 // mutexMap 8 type mutexMap struct { 9 // as the mutex map has a map of its own 10 // we also need to lock access to this map 11 l sync.Mutex 12 m map[string]*sync.Mutex 13 } 14 15 // unlocker defines the Unlock mechanism which is returned by the Lock method 16 type unlocker interface { 17 // Unlock releases the lock 18 Unlock() 19 } 20 21 // newMutexMap initializes a new map of strings which provide a mutex 22 func newMutexMap() *mutexMap { 23 return &mutexMap{m: map[string]*sync.Mutex{}} 24 } 25 26 // Remove removes an entry from the mutex map 27 func (m *mutexMap) Remove(entry string) { 28 m.l.Lock() 29 defer m.l.Unlock() 30 delete(m.m, entry) 31 } 32 33 // Lock will gain a lock on `entry`. The caller must call `Unlock` on the returned unlocker when done. 34 func (m *mutexMap) Lock(entry string) unlocker { 35 m.l.Lock() 36 e, ok := m.m[entry] 37 if !ok { 38 m.m[entry] = &sync.Mutex{} 39 e = m.m[entry] 40 } 41 m.l.Unlock() 42 e.Lock() 43 return e 44 }