New goroutine to immediately acquire the lock when Lock in starving mode?

147 views
Skip to first unread message

Wen Wei

unread,
Mar 23, 2024, 2:58:43 PM3/23/24
to golang-nuts
Lock always calls runtime_SemacquireMutex(&m.sema, queueLifo, 1) -> semacquire1 when in starving mode.

```go
// src/runtime/sema.go
func semacquire1(addr *uint32, lifo bool, profile semaProfileFlags, skipframes int, reason waitReason) {
gp := getg()
if gp != gp.m.curg {
throw("semacquire not on the G stack")
}

// Easy case.
if cansemacquire(addr) {
return
}

// ...
}

func semrelease1(addr *uint32, handoff bool, skipframes int) {
root := semtable.rootFor(addr)
atomic.Xadd(addr, 1) // The semaphore released here may be immediately contended for by goroutines of other threads calling semacquire1.
}
```

Does a new goroutine immediately acquire the lock instead of queuing fairly when in starving mode?

Ian Lance Taylor

unread,
Mar 25, 2024, 8:06:23 PM3/25/24
to Wen Wei, golang-nuts
It does seem possible.

Ian
Reply all
Reply to author
Forward
0 new messages