-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Synchronize repo processing in app manager
- Loading branch information
Alexander Matyushentsev
authored and
Alexander Matyushentsev
committed
Feb 23, 2018
1 parent
dd47c74
commit d82cd75
Showing
4 changed files
with
112 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package util | ||
|
||
import "sync" | ||
|
||
// Allows to lock by string key | ||
type KeyLock struct { | ||
giantLock sync.RWMutex | ||
locks map[string]*sync.Mutex | ||
} | ||
|
||
// NewKeyLock creates new instance of KeyLock | ||
func NewKeyLock() *KeyLock { | ||
return &KeyLock{ | ||
giantLock: sync.RWMutex{}, | ||
locks: map[string]*sync.Mutex{}, | ||
} | ||
} | ||
|
||
func (keyLock *KeyLock) getLock(key string) *sync.Mutex { | ||
keyLock.giantLock.RLock() | ||
if lock, ok := keyLock.locks[key]; ok { | ||
keyLock.giantLock.RUnlock() | ||
return lock | ||
} | ||
|
||
keyLock.giantLock.RUnlock() | ||
keyLock.giantLock.Lock() | ||
|
||
if lock, ok := keyLock.locks[key]; ok { | ||
keyLock.giantLock.Unlock() | ||
return lock | ||
} | ||
|
||
lock := &sync.Mutex{} | ||
keyLock.locks[key] = lock | ||
keyLock.giantLock.Unlock() | ||
return lock | ||
} | ||
|
||
// Lock blocks goroutine using key specific mutex | ||
func (keyLock *KeyLock) Lock(key string) { | ||
keyLock.getLock(key).Lock() | ||
} | ||
|
||
// Unlock releases key specific mutex | ||
func (keyLock *KeyLock) Unlock(key string) { | ||
keyLock.getLock(key).Unlock() | ||
} |