Skip to content

Commit

Permalink
Merge pull request #495 from lf-lang/static-use-lf-sleep
Browse files Browse the repository at this point in the history
[STATIC SCHEDULER] Use lf_sleep instead of busywait
  • Loading branch information
lsk567 authored Nov 8, 2024
2 parents 1837c6c + e8f0d54 commit 6538c34
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions core/threaded/scheduler_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(&current_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(&current_time);
lf_sleep(wakeup_time - current_time);
}
LF_PRINT_DEBUG("*** [Line %zu] Worker %zu done delaying", *pc, worker_number);
*pc += 1; // Increment pc.
Expand Down

0 comments on commit 6538c34

Please sign in to comment.