diff --git a/ocis-pkg/sync/mutex.go b/ocis-pkg/sync/mutex.go index c10cb7b4e..258d998e2 100644 --- a/ocis-pkg/sync/mutex.go +++ b/ocis-pkg/sync/mutex.go @@ -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()