Skip to content

seems like there's possible rlock blocking issue #6

Open
@rocaltair

Description

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions