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