Skip to content

Commit

Permalink
Dynamically change resolution passed to OS_WAIT
Browse files Browse the repository at this point in the history
res was hard coded to 16 ms, which could waste too many iterations when
CPU is fast enough.

Suppose WAIT starts with timeout = 30, and Awake_System always returns
negatives:
1. first iteration will call OS_WAIT with 30, and actually waits 14 ms
(timeout - res).
2. second iteration will call OS_WAIT with 16, and OS_WAIT will not wait
at all, because (timeout - res == 0).
3. third iteration will call OS_WAIT with probably 16 (or < 16, depending
on how fast the second iteration finished),

and a busy loop will start until 16 ms elapses.
  • Loading branch information
zsx committed Jan 26, 2016
1 parent 47524c2 commit f397fae
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/core/c-port.c
Expand Up @@ -206,6 +206,7 @@
REBINT result;
REBCNT wt = 1;
REBCNT res = (timeout >= 1000) ? 0 : 16; // OS dependent?
REBCNT old_time = -1;

This comment has been minimized.

Copy link
@Oldes

Oldes Oct 17, 2018

Just curious... REBCNT is defined as u32, so the old_time is never negative and so the following old_time >= 0 is always true, right?


while (wt) {
if (GET_SIGNAL(SIG_ESCAPE)) {
Expand All @@ -229,6 +230,13 @@
if (time >= timeout) break; // done (was dt = 0 before)
else if (wt > timeout - time) // use smaller residual time
wt = timeout - time;

if (old_time >= 0
&& time - old_time < res) {
res = time - old_time;
}

old_time = time;
}

//printf("%d %d %d\n", dt, time, timeout);
Expand Down

0 comments on commit f397fae

Please sign in to comment.