Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

seems like there's possible rlock blocking issue #6

Open
rocaltair opened this issue Mar 25, 2022 · 0 comments
Open

seems like there's possible rlock blocking issue #6

rocaltair opened this issue Mar 25, 2022 · 0 comments

Comments

@rocaltair
Copy link

rocaltair commented Mar 25, 2022

ch := m.channel()

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant