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

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


The following commit(s) were added to refs/heads/master by this push:
     new 0b18cfb  mm/iob: add support of alloc with timeout iob_timedalloc()
0b18cfb is described below

commit 0b18cfbd9a1f9103c06c7c017d7eaabc9347ff4b
Author: chao.an <anc...@xiaomi.com>
AuthorDate: Thu Feb 17 12:34:36 2022 +0800

    mm/iob: add support of alloc with timeout iob_timedalloc()
    
    Signed-off-by: chao.an <anc...@xiaomi.com>
---
 include/nuttx/mm/iob.h | 17 +++++++++++++++++
 mm/iob/iob_alloc.c     | 52 ++++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 63 insertions(+), 6 deletions(-)

diff --git a/include/nuttx/mm/iob.h b/include/nuttx/mm/iob.h
index afd63c0..e64130d 100644
--- a/include/nuttx/mm/iob.h
+++ b/include/nuttx/mm/iob.h
@@ -234,6 +234,23 @@ struct iob_userstats_s
 void iob_initialize(void);
 
 /****************************************************************************
+ * Name: iob_timedalloc
+ *
+ * Description:
+ *  Allocate an I/O buffer by taking the buffer at the head of the free list.
+ *  This wait will be terminated when the specified timeout expires.
+ *
+ * Input Parameters:
+ *   throttled  - An indication of the IOB allocation is "throttled"
+ *   timeout    - Timeout value in milliseconds.
+ *   consumerid - id representing who is consuming the IOB
+ *
+ ****************************************************************************/
+
+FAR struct iob_s *iob_timedalloc(bool throttled, unsigned int timeout,
+                                 enum iob_user_e consumerid);
+
+/****************************************************************************
  * Name: iob_alloc
  *
  * Description:
diff --git a/mm/iob/iob_alloc.c b/mm/iob/iob_alloc.c
index c26269f..7df00e8 100644
--- a/mm/iob/iob_alloc.c
+++ b/mm/iob/iob_alloc.c
@@ -93,7 +93,7 @@ static FAR struct iob_s *iob_alloc_committed(enum iob_user_e 
consumerid)
  *
  ****************************************************************************/
 
-static FAR struct iob_s *iob_allocwait(bool throttled,
+static FAR struct iob_s *iob_allocwait(bool throttled, unsigned int timeout,
                                        enum iob_user_e consumerid)
 {
   FAR struct iob_s *iob;
@@ -130,7 +130,27 @@ static FAR struct iob_s *iob_allocwait(bool throttled,
        * list.
        */
 
-      ret = nxsem_wait_uninterruptible(sem);
+      if (timeout == UINT_MAX)
+        {
+          ret = nxsem_wait_uninterruptible(sem);
+        }
+      else
+        {
+          struct timespec abstime;
+
+          DEBUGVERIFY(clock_gettime(CLOCK_REALTIME, &abstime));
+
+          abstime.tv_sec  += timeout / MSEC_PER_SEC;
+          abstime.tv_nsec += timeout % MSEC_PER_SEC * NSEC_PER_MSEC;
+          if (abstime.tv_nsec >= NSEC_PER_SEC)
+            {
+              abstime.tv_sec++;
+              abstime.tv_nsec -= NSEC_PER_SEC;
+            }
+
+          ret = nxsem_timedwait_uninterruptible(sem, &abstime);
+        }
+
       if (ret >= 0)
         {
           /* When we wake up from wait successfully, an I/O buffer was
@@ -180,18 +200,25 @@ static FAR struct iob_s *iob_allocwait(bool throttled,
  ****************************************************************************/
 
 /****************************************************************************
- * Name: iob_alloc
+ * Name: iob_timedalloc
  *
  * Description:
  *  Allocate an I/O buffer by taking the buffer at the head of the free list.
+ *  This wait will be terminated when the specified timeout expires.
+ *
+ * Input Parameters:
+ *   throttled  - An indication of the IOB allocation is "throttled"
+ *   timeout    - Timeout value in milliseconds.
+ *   consumerid - id representing who is consuming the IOB
  *
  ****************************************************************************/
 
-FAR struct iob_s *iob_alloc(bool throttled, enum iob_user_e consumerid)
+FAR struct iob_s *iob_timedalloc(bool throttled, unsigned int timeout,
+                                 enum iob_user_e consumerid)
 {
   /* Were we called from the interrupt level? */
 
-  if (up_interrupt_context() || sched_idletask())
+  if (up_interrupt_context() || sched_idletask() || timeout == 0)
     {
       /* Yes, then try to allocate an I/O buffer without waiting */
 
@@ -201,11 +228,24 @@ FAR struct iob_s *iob_alloc(bool throttled, enum 
iob_user_e consumerid)
     {
       /* Then allocate an I/O buffer, waiting as necessary */
 
-      return iob_allocwait(throttled, consumerid);
+      return iob_allocwait(throttled, timeout, consumerid);
     }
 }
 
 /****************************************************************************
+ * Name: iob_alloc
+ *
+ * Description:
+ *  Allocate an I/O buffer by taking the buffer at the head of the free list.
+ *
+ ****************************************************************************/
+
+FAR struct iob_s *iob_alloc(bool throttled, enum iob_user_e consumerid)
+{
+  return iob_timedalloc(throttled, UINT_MAX, consumerid);
+}
+
+/****************************************************************************
  * Name: iob_tryalloc
  *
  * Description:

Reply via email to