Possible wrap-around in timer.c/timer_wait() prevents buys-waiting
ul5255 opened this issue · 2 comments
Looking into https://github.com/organix/pijFORTHos/blob/master/timer.c
I can see a problem in function 'timer_wait()':
If the counter is about to wrap around then t1 = t0 + dt
might very well be smaller than t0. In that case
the function will return immediately and not busy-wait.
I think the best way would be to detect the situation where
t1 < t0 and handle this case in two steps:
1st) busy wait until counter wraps and only then
2nd) busy wait for (t0 - t1) >= 0.
As a side note I do not understand why in the
timer struct you operate with u32 types but then
in the functions below switch back to int?
The structures are u32
because that's the machine/device interpretation of that field.
Thanks for your review, but I believe you are mistaken about this issue. The treatment of signed integer wrap-around is the main reason for the implicit cast from u32
to int
. By checking the sign of the difference between two timer values, we can treat half of the range as "before" and half as "after", regardless of the absolute values of the counter.
This issue (and rationale) is discussed in https://github.com/organix/pijFORTHos/blob/master/doc/blinker.md
Thanks, Dale. I stand corrected. I just verified this with a small C program.