|
| 1 | +package locker |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + "sync/atomic" |
| 6 | +) |
| 7 | + |
| 8 | +// RWMutexMap is a more convenient map[T]sync.RWMutex. It automatically makes |
| 9 | +// and deletes mutexes as needed. Unlocked mutexes consume no memory. |
| 10 | +// |
| 11 | +// The zero value is a valid MutexMap. |
| 12 | +type RWMutexMap[T comparable] struct { |
| 13 | + mu sync.Mutex |
| 14 | + locks map[T]*rwlockCtr |
| 15 | +} |
| 16 | + |
| 17 | +// rwlockCtr is used by RWLocker to represent a lock with a given key. |
| 18 | +type rwlockCtr struct { |
| 19 | + sync.RWMutex |
| 20 | + waiters atomic.Int32 // Number of callers waiting to acquire the lock |
| 21 | + readers atomic.Int32 // Number of readers currently holding the lock |
| 22 | +} |
| 23 | + |
| 24 | +var rwlockCtrPool = sync.Pool{New: func() any { return new(rwlockCtr) }} |
| 25 | + |
| 26 | +func (l *RWMutexMap[T]) get(key T) *rwlockCtr { |
| 27 | + if l.locks == nil { |
| 28 | + l.locks = make(map[T]*rwlockCtr) |
| 29 | + } |
| 30 | + |
| 31 | + nameLock, exists := l.locks[key] |
| 32 | + if !exists { |
| 33 | + nameLock = rwlockCtrPool.Get().(*rwlockCtr) |
| 34 | + l.locks[key] = nameLock |
| 35 | + } |
| 36 | + return nameLock |
| 37 | +} |
| 38 | + |
| 39 | +// Lock locks the RWMutex identified by key for writing. |
| 40 | +func (l *RWMutexMap[T]) Lock(key T) { |
| 41 | + l.mu.Lock() |
| 42 | + nameLock := l.get(key) |
| 43 | + |
| 44 | + // Increment the nameLock waiters while inside the main mutex. |
| 45 | + // This makes sure that the lock isn't deleted if `Lock` and `Unlock` are called concurrently. |
| 46 | + nameLock.waiters.Add(1) |
| 47 | + l.mu.Unlock() |
| 48 | + |
| 49 | + // Lock the nameLock outside the main mutex so we don't block other operations. |
| 50 | + // Once locked then we can decrement the number of waiters for this lock. |
| 51 | + nameLock.Lock() |
| 52 | + nameLock.waiters.Add(-1) |
| 53 | +} |
| 54 | + |
| 55 | +// RLock locks the RWMutex identified by key for reading. |
| 56 | +func (l *RWMutexMap[T]) RLock(key T) { |
| 57 | + l.mu.Lock() |
| 58 | + nameLock := l.get(key) |
| 59 | + |
| 60 | + nameLock.waiters.Add(1) |
| 61 | + l.mu.Unlock() |
| 62 | + |
| 63 | + nameLock.RLock() |
| 64 | + // Increment the number of readers before decrementing the waiters |
| 65 | + // so concurrent calls to RUnlock will not see a glitch where both |
| 66 | + // waiters and readers are 0. |
| 67 | + nameLock.readers.Add(1) |
| 68 | + nameLock.waiters.Add(-1) |
| 69 | +} |
| 70 | + |
| 71 | +// Unlock unlocks the RWMutex identified by key. |
| 72 | +// |
| 73 | +// It is a run-time error if the lock is not locked for writing on entry to Unlock. |
| 74 | +func (l *RWMutexMap[T]) Unlock(key T) { |
| 75 | + l.mu.Lock() |
| 76 | + defer l.mu.Unlock() |
| 77 | + nameLock := l.get(key) |
| 78 | + // We don't have to do anything special to handle the error case: |
| 79 | + // l.get(key) will return an unlocked mutex. |
| 80 | + |
| 81 | + if nameLock.waiters.Load() <= 0 && nameLock.readers.Load() <= 0 { |
| 82 | + delete(l.locks, key) |
| 83 | + defer rwlockCtrPool.Put(nameLock) |
| 84 | + } |
| 85 | + nameLock.Unlock() |
| 86 | +} |
| 87 | + |
| 88 | +// RUnlock unlocks the RWMutex identified by key for reading. |
| 89 | +// |
| 90 | +// It is a run-time error if the lock is not locked for reading on entry to RUnlock. |
| 91 | +func (l *RWMutexMap[T]) RUnlock(key T) { |
| 92 | + l.mu.Lock() |
| 93 | + defer l.mu.Unlock() |
| 94 | + nameLock := l.get(key) |
| 95 | + nameLock.readers.Add(-1) |
| 96 | + |
| 97 | + if nameLock.waiters.Load() <= 0 && nameLock.readers.Load() <= 0 { |
| 98 | + delete(l.locks, key) |
| 99 | + defer rwlockCtrPool.Put(nameLock) |
| 100 | + } |
| 101 | + nameLock.RUnlock() |
| 102 | +} |
| 103 | + |
| 104 | +// Locker returns a [sync.Locker] interface that implements |
| 105 | +// the [sync.Locker.Lock] and [sync.Locker.Unlock] methods |
| 106 | +// by calling l.Lock(name) and l.Unlock(name). |
| 107 | +func (l *RWMutexMap[T]) Locker(key T) sync.Locker { |
| 108 | + return nameRWLocker[T]{l: l, key: key} |
| 109 | +} |
| 110 | + |
| 111 | +// RLocker returns a [sync.Locker] interface that implements |
| 112 | +// the [sync.Locker.Lock] and [sync.Locker.Unlock] methods |
| 113 | +// by calling l.RLock(name) and l.RUnlock(name). |
| 114 | +func (l *RWMutexMap[T]) RLocker(key T) sync.Locker { |
| 115 | + return nameRLocker[T]{l: l, key: key} |
| 116 | +} |
| 117 | + |
| 118 | +type nameRWLocker[T comparable] struct { |
| 119 | + l *RWMutexMap[T] |
| 120 | + key T |
| 121 | +} |
| 122 | +type nameRLocker[T comparable] nameRWLocker[T] |
| 123 | + |
| 124 | +func (n nameRWLocker[T]) Lock() { |
| 125 | + n.l.Lock(n.key) |
| 126 | +} |
| 127 | +func (n nameRWLocker[T]) Unlock() { |
| 128 | + n.l.Unlock(n.key) |
| 129 | +} |
| 130 | + |
| 131 | +func (n nameRLocker[T]) Lock() { |
| 132 | + n.l.RLock(n.key) |
| 133 | +} |
| 134 | +func (n nameRLocker[T]) Unlock() { |
| 135 | + n.l.RUnlock(n.key) |
| 136 | +} |
0 commit comments