Tristan Hsieh has uploaded this change for review. ( https://review.coreboot.org/29315
Change subject: include/timer.h: Add wait_cond_timeout_usecs macro ......................................................................
include/timer.h: Add wait_cond_timeout_usecs macro
Add wait_cond_timeout_usecs and wait_cond_timeout_msecs to wait until a condition gets true or a timeout elapses.
BUG=b:80501386 BRANCH=none TEST=Built and booted on Kukui
Change-Id: I6de38ee00673c46332ae92b8a11099485de5327a Signed-off-by: Tristan Shieh tristan.shieh@mediatek.com --- M src/include/timer.h 1 file changed, 43 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/15/29315/1
diff --git a/src/include/timer.h b/src/include/timer.h index 49eae5e..a5ee72b 100644 --- a/src/include/timer.h +++ b/src/include/timer.h @@ -195,4 +195,47 @@ return stopwatch_duration_usecs(sw) / USECS_PER_MSEC; }
+/* + * Return remaining time. + */ +static inline long stopwatch_remain_usecs(struct stopwatch *sw) +{ + return mono_time_diff_microseconds(&sw->current, &sw->expires); +} + +static inline long stopwatch_remain_msecs(struct stopwatch *sw) +{ + return stopwatch_remain_usecs(sw) / USECS_PER_MSEC; +} + +/* + * Wait until a condition gets true or a timeout elapses. + * + * condition: a C expression to wait for + * timeout: timeout, in microseconds + * + * Returns: + * 0 if the condition evaluated to false after the timeout elapsed, + * 1 if the condition evaluated to true after the timeout elapsed, + * or the remaining microseconds (at least 1) if the condition evaluated + * to true before the timeout elapsed. + */ +#define wait_cond_usecs(condition, timeout) \ +({ \ + long __ret = 0; \ + struct stopwatch __sw; \ + stopwatch_init_usecs_expire(&__sw, timeout); \ + do { \ + if (condition) { \ + __ret = stopwatch_expired(&__sw) ? 1 : \ + stopwatch_remain_usecs(&__sw); \ + break; \ + } \ + } while (!stopwatch_expired(&__sw)); \ + __ret; \ +}) + +#define wait_cond_msecs(condition, timeout) \ + wait_cond_usecs(condition, (timeout) * USECS_PER_MSEC) + #endif /* TIMER_H */