-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform_linux_nocgo.go
More file actions
58 lines (48 loc) · 920 Bytes
/
platform_linux_nocgo.go
File metadata and controls
58 lines (48 loc) · 920 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//go:build linux && !cgo
package hpt
import (
"runtime"
"time"
"golang.org/x/sys/unix"
)
func monotonicNow() int64 {
var ts unix.Timespec
_ = unix.ClockGettime(unix.CLOCK_MONOTONIC, &ts)
return ts.Sec*1e9 + ts.Nsec
}
func sleepUntil(deadline int64) {
ts := unix.Timespec{
Sec: deadline / 1e9,
Nsec: deadline % 1e9,
}
for {
err := unix.ClockNanosleep(unix.CLOCK_MONOTONIC, unix.TIMER_ABSTIME, &ts, nil)
if err != unix.EINTR {
return
}
}
}
func startTickerLoop(period time.Duration, c chan time.Time) (stop func()) {
done := make(chan struct{})
go func() {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
start := monotonicNow()
d := period.Nanoseconds()
var tick int64
for {
tick++
sleepUntil(start + tick*d)
select {
case <-done:
return
default:
}
select {
case c <- time.Now():
default:
}
}
}()
return func() { close(done) }
}