Open
Description
Line 120 in 6894780
func (m *trylocker) RTryLock(ctx context.Context) bool {
for {
n := atomic.LoadInt32(m.state)
if n >= 0 {
if atomic.CompareAndSwapInt32(m.state, n, n+1) {
// acquire OK
return true
}
}
if ctx == nil {
return false
}
// get broadcast channel
ch := m.channel()
select {
case <-ch:
// wake up to try again
case <-ctx.Done():
// timeout
return false
}
}
}
say there are two goroutines, W
using Lock and R
using RLock
t0: R: failed to CompareAndSwapInt32 (going to get ch and wait signal)
t1: W: swap chan
t2: R: get a new chan (need try to CompareAndSwapInt32 again in fact)
t3: W: close old chan (no signal sent to the new chan)
now: R blocking
Metadata
Assignees
Labels
No labels
Activity