-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsleep.go
More file actions
30 lines (25 loc) · 769 Bytes
/
sleep.go
File metadata and controls
30 lines (25 loc) · 769 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
package hpt
import (
"runtime"
"time"
)
// Sleep pauses the current goroutine for at least the duration d using
// OS-level high-precision sleep primitives. Unlike time.Sleep, this locks
// the current goroutine to an OS thread and bypasses Go's timer coalescing.
//
// A negative or zero duration causes Sleep to return immediately.
//
// The calling goroutine's OS thread is locked for the duration of the sleep.
// Do not use this in high-concurrency scenarios with thousands of sleeping
// goroutines, as each consumes a real OS thread.
func Sleep(d time.Duration) {
if d <= 0 {
return
}
threadStarted()
defer threadStopped()
runtime.LockOSThread()
defer runtime.UnlockOSThread()
deadline := monotonicNow() + d.Nanoseconds()
sleepUntil(deadline)
}