This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 1d6b9d3e98 iob: add elapse calc for iob_allocwait
1d6b9d3e98 is described below

commit 1d6b9d3e985fa74a8b0016d10a0e4378efac643c
Author: zhanghongyu <[email protected]>
AuthorDate: Thu May 18 11:27:47 2023 +0800

    iob: add elapse calc for iob_allocwait
    
    Correct the calculation of timeout time
    
    Signed-off-by: zhanghongyu <[email protected]>
---
 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)

Reply via email to