-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpause.go
More file actions
30 lines (25 loc) · 766 Bytes
/
pause.go
File metadata and controls
30 lines (25 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// pause.go - relax the cpu or schedule
package sieve
import (
"runtime"
_ "unsafe" // for go:linkname
)
const (
// Spin tuning — mirrors Go runtime's sync.Mutex active spin constants.
// 4 rounds of 30 PAUSE instructions ≈ 400-500ns on modern x86.
_SpinPAUSE = 4 // procyield iterations before falling back to Gosched
_PauseCycles = 30 // PAUSE instructions per procyield call
)
// procyield emits N PAUSE instructions (x86) or YIELD (arm64).
// Used by Go's own sync.Mutex; unlikely to disappear.
//
//go:linkname procyield runtime.procyield
func procyield(cycles uint32)
// pause - relax the cpu if we haven't paused enough else yield the cpu
func pause(n int) {
if n < _SpinPAUSE {
procyield(_PauseCycles)
} else {
runtime.Gosched()
}
}