add comments to NRWMutex

This commit is contained in:
Florian Schade
2021-01-14 22:33:52 +01:00
parent a02fb890f7
commit b81156fc57

View File

@@ -4,6 +4,8 @@ import (
"sync"
)
// NRWMutex works the same as RWMutex, the only difference is that it stores mutexes in a map and reuses them.
// It's handy if you want to write-lock, write-unlock, read-lock and read-unlock for specific names only.
type NRWMutex struct {
m sync.Mutex
mm map[string]*nrw
@@ -14,10 +16,12 @@ type nrw struct {
c int
}
// NewNRWMutex returns a new instance of NRWMutex.
func NewNRWMutex() NRWMutex {
return NRWMutex{mm: make(map[string]*nrw)}
}
// Lock locks rw for writing.
func (c *NRWMutex) Lock(k string) {
c.m.Lock()
m := c.get(k)
@@ -26,6 +30,7 @@ func (c *NRWMutex) Lock(k string) {
m.m.Lock()
}
// Unlock unlocks rw for writing.
func (c *NRWMutex) Unlock(k string) {
c.m.Lock()
defer c.m.Unlock()
@@ -37,6 +42,7 @@ func (c *NRWMutex) Unlock(k string) {
}
}
// RLock locks rw for reading.
func (c *NRWMutex) RLock(k string) {
c.m.Lock()
m := c.get(k)
@@ -45,6 +51,7 @@ func (c *NRWMutex) RLock(k string) {
m.m.RLock()
}
// RUnlock undoes a single RLock call.
func (c *NRWMutex) RUnlock(k string) {
c.m.Lock()
defer c.m.Unlock()