From 1d6b9d3e985fa74a8b0016d10a0e4378efac643c Mon Sep 17 00:00:00 2001 From: zhanghongyu Date: Thu, 18 May 2023 11:27:47 +0800 Subject: [PATCH] iob: add elapse calc for iob_allocwait Correct the calculation of timeout time Signed-off-by: zhanghongyu --- mm/iob/iob_alloc.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/mm/iob/iob_alloc.c b/mm/iob/iob_alloc.c index 5acaebe141..3cb7dff71c 100644 --- a/mm/iob/iob_alloc.c +++ b/mm/iob/iob_alloc.c @@ -38,6 +38,23 @@ * Private Functions ****************************************************************************/ +static clock_t iob_allocwait_gettimeout(clock_t start, unsigned int timeout) +{ + sclock_t tick; + + tick = clock_systime_ticks() - start; + if (tick >= MSEC2TICK(timeout)) + { + tick = 0; + } + else + { + tick = MSEC2TICK(timeout) - tick; + } + + return tick; +} + /**************************************************************************** * Name: iob_alloc_committed * @@ -93,6 +110,7 @@ static FAR struct iob_s *iob_allocwait(bool throttled, unsigned int timeout) FAR struct iob_s *iob; irqstate_t flags; FAR sem_t *sem; + clock_t start; int ret = OK; #if CONFIG_IOB_THROTTLE > 0 @@ -115,7 +133,8 @@ static FAR struct iob_s *iob_allocwait(bool throttled, unsigned int timeout) * decremented atomically. */ - iob = iob_tryalloc(throttled); + start = clock_systime_ticks(); + iob = iob_tryalloc(throttled); while (ret == OK && iob == NULL) { /* If not successful, then the semaphore count was less than or equal @@ -130,7 +149,8 @@ static FAR struct iob_s *iob_allocwait(bool throttled, unsigned int timeout) } else { - ret = nxsem_tickwait_uninterruptible(sem, MSEC2TICK(timeout)); + ret = nxsem_tickwait_uninterruptible(sem, + iob_allocwait_gettimeout(start, timeout)); } if (ret >= 0)