diff --git a/core/threaded/scheduler_static.c b/core/threaded/scheduler_static.c index be50ea1b9..e8979cd37 100644 --- a/core/threaded/scheduler_static.c +++ b/core/threaded/scheduler_static.c @@ -275,27 +275,20 @@ void execute_inst_DU(lf_scheduler_t* scheduler, size_t worker_number, operand_t int pc_orig = (int) *pc; tracepoint_static_scheduler_DU_starts(worker_number, pc_orig); #endif - // FIXME: There seems to be an overflow problem. - // When wakeup_time overflows but lf_time_physical() doesn't, - // _lf_interruptable_sleep_until_locked() terminates immediately. reg_t *src = op1.reg; - instant_t current_time = lf_time_physical(); instant_t wakeup_time = *src + op2.imm; LF_PRINT_DEBUG("DU wakeup time: %lld, base: %lld, offset: %lld", wakeup_time, *src, op2.imm); + // Check if we need to sleep. + instant_t current_time; + _lf_clock_gettime(¤t_time); instant_t wait_interval = wakeup_time - current_time; - // LF_PRINT_DEBUG("*** start_time: %lld, wakeup_time: %lld, op1: %lld, op2: %lld, current_physical_time: %lld\n", start_time, wakeup_time, *src, op2.imm, lf_time_physical()); - LF_PRINT_DEBUG("*** [Line %zu] Worker %zu delaying, current_physical_time: %lld, wakeup_time: %lld, wait_interval: %lld", *pc, worker_number, current_time, wakeup_time, wait_interval); + LF_PRINT_DEBUG( + "*** [Line %zu] Worker %zu delaying, current_physical_time: %lld, wakeup_time: %lld, wait_interval: %lld", *pc, + worker_number, current_time, wakeup_time, wait_interval); if (wait_interval > 0) { - // Approach 1: Only spin when the wait interval is less than SPIN_WAIT_THRESHOLD. - if (wait_interval < SPIN_WAIT_THRESHOLD) { - // Spin wait if the wait interval is less than 1 ms. - while (lf_time_physical() < wakeup_time); - } else { - // Otherwise sleep. - _lf_interruptable_sleep_until_locked(scheduler->env, wakeup_time); - } - // Approach 2: Spin wait. - // while (lf_time_physical() < wakeup_time); + // Recalculate the wakeup time for max accuracy. + _lf_clock_gettime(¤t_time); + lf_sleep(wakeup_time - current_time); } LF_PRINT_DEBUG("*** [Line %zu] Worker %zu done delaying", *pc, worker_number); *pc += 1; // Increment pc.