You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
go-trylock/trylock.go
Line 120 in 6894780
say there are two goroutines,
W
using Lock andR
using RLockt0: 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
The text was updated successfully, but these errors were encountered: