time: yield accurate_sleep instead of relaxing the cpu

This commit is contained in:
hikari
2022-04-16 14:08:37 +03:00
parent b9dc81d808
commit 0a0440a6e8
3 changed files with 18 additions and 2 deletions
+8 -2
View File
@@ -213,6 +213,10 @@ time_add :: proc(t: Time, d: Duration) -> Time {
return Time{t._nsec + i64(d)}
}
yield :: proc "contextless" () {
_yield()
}
// Accurate sleep borrowed from: https://blat-blatnik.github.io/computerBear/making-accurate-sleep-function/
accurate_sleep :: proc(d: Duration) {
to_sleep, estimate, mean, m2, count: Duration
@@ -240,8 +244,10 @@ accurate_sleep :: proc(d: Duration) {
start := tick_now()
for to_sleep > tick_since(start) {
intrinsics.cpu_relax() // prevent the spinlock from taking the thread hostage, still accurate enough
// NOTE: it is possible that sometimes cpu can relax a bit too much, in that case it should spinlock freely for a while (TODO)
// prevent the spinlock from taking the thread hostage, still accurate enough
yield()
// NOTE: it might be possible that it yields for too long, in that case it should spinlock freely for a while
// TODO: needs actual testing done to check if that's the case
}
}