github.com/hoffie/larasync@v0.0.0-20151025221940-0384d2bddcef/helpers/lock/processManager.go (about) 1 package lock 2 3 import ( 4 "sync" 5 ) 6 7 // ProcessManager implements the Manager interface and 8 // can be used to request locks on a process level. 9 type ProcessManager struct { 10 locks map[string]map[string]sync.Locker 11 } 12 13 // newProcessManager is a helper function which initializes 14 // a process manager and returns it. 15 func newProcessManager() *ProcessManager { 16 pm := &ProcessManager{} 17 pm.reset() 18 return pm 19 } 20 21 // Get returns a unique Lock for the given path and role. 22 // Calling this function again with the same input parameters 23 // will return the same lock. 24 func (pm *ProcessManager) Get(path string, role string) sync.Locker { 25 roleMap, ok := pm.locks[path] 26 if !ok { 27 roleMap = map[string]sync.Locker{} 28 pm.locks[path] = roleMap 29 } 30 31 locker, ok := roleMap[role] 32 if !ok { 33 locker = &sync.Mutex{} 34 roleMap[role] = locker 35 } 36 return locker 37 } 38 39 // reset is an internal helper function which is used for testing purposes 40 // and on object initialisation. It purges the lock cache. 41 func (pm *ProcessManager) reset() { 42 pm.locks = map[string]map[string]sync.Locker{} 43 }