Sync Mutex, locking logic

func (m *Mutex) lockSlow() {

for {

if atomic.CompareAndSwapInt32(&m.state, old, new) {

if old&mutexStarving != 0 {
delta := int32(mutexLocked - 1<<mutexWaiterShift)
if !starving || old>>mutexWaiterShift == 1 {
delta -= mutexStarving
}
atomic.AddInt32(&m.state, delta)
break
}
awoke = true
iter = 0
}
}
}

I don’t fully understand the locking logic of sync.Mutex in Go’s source code.

If a Goroutine enters starvation mode and mutexWaiterShift == 1 at that moment, then starvation mode is removed. If a new Goroutine tries to acquire the lock at this point, will it be able to succeed?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.