mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-02-24 22:49:06 -06:00
add comments to NRWMutex
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user