Skip to content

Instantly share code, notes, and snippets.

@kawasin73
Last active November 29, 2019 01:23
Show Gist options
  • Save kawasin73/9703ba6ba1ab872ba95b2bd9f5c4f26b to your computer and use it in GitHub Desktop.
Save kawasin73/9703ba6ba1ab872ba95b2bd9f5c4f26b to your computer and use it in GitHub Desktop.
property
w int
r int
mu sync.Mutex
rwmu sync.RWMutex
rlock
mu.lock
r++
if w == 0
rwmu.rlock
mu.unlock
else
mu.unlock
rwmu.rlock
tryrlock
mu.lock
if w > 0
mu.unlock
return false
else
r++
rwmu.rlock
mu.unlock
lock
mu.lock
w++
if r > 0 || w > 1
mu.unlock
rwmu.lock
else
rwmu.lock
mu.unlock
trylock
mu.lock
if r > 0 || w > 0
mu.unlock
return false
else
w++
rwmu.lock
mu.unlock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment