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

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

commit e7943bf39e2f392ceffd2d6c4dedbf70cf98130d
Author: Bowen Wang <[email protected]>
AuthorDate: Tue Jul 29 22:46:18 2025 +0800

    !mm/mempool: mempool_allocate support timed alloc buffer
    
    timeout = 0:        do not wait the buffer to become available
    timeout = UINT_MAX: wait forever until the buffer become available
    others:             wait timeout ms for the buffer
    
    BREAKING CHANGE: mempool_allocate() takes a second argument, the wait
    timeout in milliseconds.  Quick fix: pass UINT_MAX to keep the old
    behavior (wait when the pool can wait), or 0 to never wait.
    
    Signed-off-by: Bowen Wang <[email protected]>
---
 include/nuttx/mm/mempool.h    |  6 ++++--
 mm/mempool/mempool.c          | 27 ++++++++++++++++++++++-----
 mm/mempool/mempool_multiple.c |  4 ++--
 3 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/include/nuttx/mm/mempool.h b/include/nuttx/mm/mempool.h
index e0635079afe..7d20e650fc0 100644
--- a/include/nuttx/mm/mempool.h
+++ b/include/nuttx/mm/mempool.h
@@ -180,14 +180,16 @@ int mempool_init(FAR struct mempool_s *pool, FAR const 
char *name);
  *   pool if expandsize isn't zero.
  *
  * Input Parameters:
- *   pool - Address of the memory pool to be used.
+ *   pool    - Address of the memory pool to be used.
+ *   timeout - The maximum time (ms) to wait for a buffer to become
+ *             available.
  *
  * Returned Value:
  *   The pointer to the allocated block on success; NULL on any failure.
  *
  ****************************************************************************/
 
-FAR void *mempool_allocate(FAR struct mempool_s *pool);
+FAR void *mempool_allocate(FAR struct mempool_s *pool, unsigned int timeout);
 
 /****************************************************************************
  * Name: mempool_release
diff --git a/mm/mempool/mempool.c b/mm/mempool/mempool.c
index a2c36a3adc0..27197f76946 100644
--- a/mm/mempool/mempool.c
+++ b/mm/mempool/mempool.c
@@ -343,14 +343,16 @@ int mempool_init(FAR struct mempool_s *pool, FAR const 
char *name)
  *   pool if expandsize isn't zero.
  *
  * Input Parameters:
- *   pool - Address of the memory pool to be used.
+ *   pool    - Address of the memory pool to be used.
+ *   timeout - The maximum time (ms) to wait for a buffer to become
+ *             available.
  *
  * Returned Value:
  *   The pointer to the allocated block on success; NULL on any failure.
  *
  ****************************************************************************/
 
-FAR void *mempool_allocate(FAR struct mempool_s *pool)
+FAR void *mempool_allocate(FAR struct mempool_s *pool, unsigned int timeout)
 {
   FAR sq_entry_t *blk;
   irqstate_t flags;
@@ -394,14 +396,29 @@ retry:
                          &pool->equeue);
               blk = mempool_remove_queue(pool, &pool->queue);
             }
-          else if (!pool->wait ||
-                   pool->expandsize > 0 ||
-                   nxsem_wait_uninterruptible(&pool->waitsem) < 0)
+          else if (!pool->wait || pool->expandsize > 0 || timeout == 0)
             {
               return NULL;
             }
           else
             {
+              int ret;
+
+              if (timeout == UINT_MAX)
+                {
+                  ret = nxsem_wait_uninterruptible(&pool->waitsem);
+                }
+              else
+                {
+                  ret = nxsem_tickwait_uninterruptible(&pool->waitsem,
+                                                       MSEC2TICK(timeout));
+                }
+
+              if (ret < 0)
+                {
+                  return NULL;
+                }
+
               goto retry;
             }
         }
diff --git a/mm/mempool/mempool_multiple.c b/mm/mempool/mempool_multiple.c
index 75a0aa6a587..ead52892331 100644
--- a/mm/mempool/mempool_multiple.c
+++ b/mm/mempool/mempool_multiple.c
@@ -551,7 +551,7 @@ FAR void *mempool_multiple_alloc(FAR struct 
mempool_multiple_s *mpool,
   end = mpool->pools + mpool->npools;
   do
     {
-      FAR void *blk = mempool_allocate(pool);
+      FAR void *blk = mempool_allocate(pool, UINT_MAX);
 
       if (blk)
         {
@@ -722,7 +722,7 @@ FAR void *mempool_multiple_memalign(FAR struct 
mempool_multiple_s *mpool,
   end = mpool->pools + mpool->npools;
   do
     {
-      FAR char *blk = mempool_allocate(pool);
+      FAR char *blk = mempool_allocate(pool, UINT_MAX);
 
       if (blk != NULL)
         {

Reply via email to