timeouts: sys_check_timeouts: return wait time for next loop

see patch #10133

So that can do:

void my_loop() {
  long timeout;
  while (1) {
    timeout = sys_check_timeouts();
    if (timeout == SYS_TIMEOUTS_SLEEPTIME_INFINITE)
      timeout = 100;
    WaitForEvent(timeout);
  }
}

Signed-off-by: Simon Goldschmidt <goldsimon@gmx.de>
This commit is contained in:
Gang Zhuo
2026-05-12 20:47:41 +02:00
committed by Simon Goldschmidt
parent 5d32779c1b
commit 0c3a93efe3
2 changed files with 9 additions and 4 deletions

View File

@@ -347,8 +347,11 @@ sys_untimeout(sys_timeout_handler handler, void *arg)
* handler functions when timeouts expire. * handler functions when timeouts expire.
* *
* Must be called periodically from your main loop. * Must be called periodically from your main loop.
*
* @return the time left before the next timeout is due. If no timeouts are
* enqueued, returns 0xffffffff
*/ */
void u32_t
sys_check_timeouts(void) sys_check_timeouts(void)
{ {
u32_t now; u32_t now;
@@ -367,11 +370,13 @@ sys_check_timeouts(void)
tmptimeout = next_timeout; tmptimeout = next_timeout;
if (tmptimeout == NULL) { if (tmptimeout == NULL) {
return; return SYS_TIMEOUTS_SLEEPTIME_INFINITE;
} }
if (TIME_LESS_THAN(now, tmptimeout->time)) { if (TIME_LESS_THAN(now, tmptimeout->time)) {
return; u32_t ret = (u32_t)(tmptimeout->time - now);
LWIP_ASSERT("invalid sleeptime", ret <= LWIP_MAX_TIMEOUT);
return ret;
} }
/* Timeout has expired */ /* Timeout has expired */

View File

@@ -111,7 +111,7 @@ void sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg);
void sys_untimeout(sys_timeout_handler handler, void *arg); void sys_untimeout(sys_timeout_handler handler, void *arg);
void sys_restart_timeouts(void); void sys_restart_timeouts(void);
void sys_check_timeouts(void); u32_t sys_check_timeouts(void);
u32_t sys_timeouts_sleeptime(void); u32_t sys_timeouts_sleeptime(void);
#if LWIP_TESTMODE #if LWIP_TESTMODE