Source File
lock_sema.go
Belonging Package
runtime
package runtime
import (
)
const (
locked uintptr = 1
active_spin = 4
active_spin_cnt = 30
passive_spin = 1
)
func ( *mutex) {
lockWithRank(, getLockRank())
}
func ( *mutex) {
:= getg()
if .m.locks < 0 {
throw("runtime·lock: lock count")
}
.m.locks++
if atomic.Casuintptr(&.key, 0, locked) {
return
}
semacreate(.m)
:= 0
if ncpu > 1 {
= active_spin
}
:
for := 0; ; ++ {
:= atomic.Loaduintptr(&.key)
if atomic.Casuintptr(&.key, , |locked) {
return
}
= 0
}
if < {
procyield(active_spin_cnt)
} else if < +passive_spin {
osyield()
semasleep(-1)
= 0
}
}
}
}
func ( *mutex) {
unlockWithRank()
}
semawakeup()
break
}
}
}
.m.locks--
if .m.locks < 0 {
throw("runtime·unlock: lock count")
}
if .m.locks == 0 && .preempt { // restore the preemption request in case we've cleared it in newstack
.stackguard0 = stackPreempt
}
}
func ( *note) {
atomic.Storeuintptr(&.key, 0)
} else {
.key = 0
}
}
func ( *note) {
var uintptr
for {
= atomic.Loaduintptr(&.key)
if atomic.Casuintptr(&.key, , locked) {
break
}
}
switch {
throw("notewakeup - double wakeup")
const = 10e6
for atomic.Loaduintptr(&.key) == 0 {
semasleep()
asmcgocall(*cgo_yield, nil)
}
}
.m.blocked = false
}
= getg()
= - nanotime()
if <= 0 {
break
}
for {
:= atomic.Loaduintptr(&.key)
switch {
if atomic.Casuintptr(&.key, , 0) {
return false
}
.m.blocked = true
if semasleep(-1) < 0 {
throw("runtime: unable to acquire - semaphore out of sync")
}
.m.blocked = false
return true
default:
throw("runtime: unexpected waitm - semaphore out of sync")
}
}
}
func ( *note, int64) bool {
:= getg()
if != .m.g0 {
throw("notetsleep not on g0")
}
semacreate(.m)
return notetsleep_internal(, , nil, 0)
}
func ( *note, int64) bool {
:= getg()
if == .m.g0 {
throw("notetsleepg on g0")
}
semacreate(.m)
entersyscallblock()
:= notetsleep_internal(, , nil, 0)
exitsyscall()
return
}
func (int64) (*g, bool) {
return nil, false
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |