-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamedrwmutexsm.go
53 lines (44 loc) · 1.28 KB
/
namedrwmutexsm.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
45
46
47
48
49
50
51
52
53
package advsync
import (
"github.com/puzpuzpuz/xsync/v3"
"sync"
)
// NamedRWMutexSM is a named read/write mutex via sync.Map
type NamedRWMutexSM[K comparable] struct {
internalMap *xsync.MapOf[K, *sync.RWMutex]
}
func NewNamedRWMutexSM[K comparable]() NamedRWMutexSM[K] {
return NamedRWMutexSM[K]{
xsync.NewMapOf[K, *sync.RWMutex](),
}
}
// Unlock mutex by name
func (nm *NamedRWMutexSM[K]) Unlock(slug K) {
mutex, _ := nm.internalMap.LoadOrStore(slug, &sync.RWMutex{})
mutex.Unlock()
}
// UnlockSafe mutex by name
func (nm *NamedRWMutexSM[K]) UnlockSafe(slug K) bool {
mutex, _ := nm.internalMap.LoadOrStore(slug, &sync.RWMutex{})
return unlockSafeRW(mutex)
}
// Lock mutex by name
func (nm *NamedRWMutexSM[K]) Lock(slug K) {
mutex, _ := nm.internalMap.LoadOrStore(slug, &sync.RWMutex{})
mutex.Lock()
}
// RUnlock mutex by name
func (nm *NamedRWMutexSM[K]) RUnlock(slug K) {
mutex, _ := nm.internalMap.LoadOrStore(slug, &sync.RWMutex{})
mutex.RUnlock()
}
// RUnlockSafe mutex by name
func (nm *NamedRWMutexSM[K]) RUnlockSafe(slug K) bool {
mutex, _ := nm.internalMap.LoadOrStore(slug, &sync.RWMutex{})
return rUnlockSafeRW(mutex)
}
// RLock mutex by name
func (nm *NamedRWMutexSM[K]) RLock(slug K) {
mutex, _ := nm.internalMap.LoadOrStore(slug, &sync.RWMutex{})
mutex.RLock()
}