emu: implement thread sleep in granular steps

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-10-28 21:58:55 -03:00
parent 4eb715a138
commit 10aec67ee0
1 changed files with 11 additions and 9 deletions

View File

@ -132,11 +132,10 @@ fn videoSync(timer: *Timer, wake_time: u64) u64 {
// TODO: Better sleep impl? // TODO: Better sleep impl?
fn sleep(timer: *Timer, wake_time: u64) ?u64 { fn sleep(timer: *Timer, wake_time: u64) ?u64 {
// const step = std.time.ns_per_ms * 10; // 10ms
const timestamp = timer.read(); const timestamp = timer.read();
// ns_late is non zero if we are late. // ns_late is non zero if we are late.
const ns_late = timestamp -| wake_time; var ns_late = timestamp -| wake_time;
// If we're more than a frame late, skip the rest of this loop // If we're more than a frame late, skip the rest of this loop
// Recalculate what our new wake time should be so that we can // Recalculate what our new wake time should be so that we can
@ -144,15 +143,18 @@ fn sleep(timer: *Timer, wake_time: u64) ?u64 {
if (ns_late > frame_period) return timestamp + frame_period; if (ns_late > frame_period) return timestamp + frame_period;
const sleep_for = frame_period - ns_late; const sleep_for = frame_period - ns_late;
// // Employ several sleep calls in periods of 10ms const step = 2 * std.time.ns_per_ms; // Granularity of 2ms
// // By doing this the behaviour should average out to be const times = sleep_for / step;
// // more consistent var i: usize = 0;
// const loop_count = sleep_for / step; // How many groups of 10ms
// var i: usize = 0; while (i < times) : (i += 1) {
// while (i < loop_count) : (i += 1) std.time.sleep(step); std.time.sleep(step);
std.time.sleep(sleep_for); // Upon wakeup, check to see if this particular sleep was longer than expected
// if so we should exit early, but probably not skip a whole frame period
ns_late = timer.read() -| wake_time;
if (ns_late > frame_period) return null;
}
return null; return null;
} }