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 f9647b4e2f lib_pathbuffer: change nxmutex -> spinlock
f9647b4e2f is described below

commit f9647b4e2f0586d76c8631fe7e5755cbff5a7f97
Author: chenrun1 <[email protected]>
AuthorDate: Wed Nov 13 13:39:13 2024 +0800

    lib_pathbuffer: change nxmutex -> spinlock
    
    Summary:
    _assert
    |
     ->dump_fatal_info
       |
        ->dump_tasks
          |
           ->dump_filelist
             |
              ->files_dumplist
                |
                 ->lib_get_pathbuffer
                   |
                    ->nxmutex_lock      <-- hold mutex on assert will trigger 
an nested exception
    
    Signed-off-by: chenrun1 <[email protected]>
---
 libs/libc/misc/lib_pathbuffer.c | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/libs/libc/misc/lib_pathbuffer.c b/libs/libc/misc/lib_pathbuffer.c
index 6c022c3c73..3928130dc8 100644
--- a/libs/libc/misc/lib_pathbuffer.c
+++ b/libs/libc/misc/lib_pathbuffer.c
@@ -25,7 +25,7 @@
  ****************************************************************************/
 
 #include <nuttx/config.h>
-#include <nuttx/mutex.h>
+#include <nuttx/spinlock.h>
 #include <nuttx/lib/lib.h>
 
 #include <stdlib.h>
@@ -41,7 +41,7 @@
 
 struct pathbuffer_s
 {
-  mutex_t lock;              /* Lock for the buffer */
+  spinlock_t lock;           /* Lock for the buffer */
   unsigned long free_bitmap; /* Bitmap of free buffer */
   char buffer[CONFIG_LIBC_MAX_PATHBUFFER][PATH_MAX];
 };
@@ -52,7 +52,7 @@ struct pathbuffer_s
 
 static struct pathbuffer_s g_pathbuffer =
 {
-  NXMUTEX_INITIALIZER,
+  SP_UNLOCKED,
   (1u << CONFIG_LIBC_MAX_PATHBUFFER) - 1,
 };
 
@@ -82,20 +82,21 @@ static struct pathbuffer_s g_pathbuffer =
 
 FAR char *lib_get_pathbuffer(void)
 {
+  irqstate_t flags;
   int index;
 
   /* Try to find a free buffer */
 
-  nxmutex_lock(&g_pathbuffer.lock);
+  flags = spin_lock_irqsave(&g_pathbuffer.lock);
   index = ffsl(g_pathbuffer.free_bitmap) - 1;
   if (index >= 0 && index < CONFIG_LIBC_MAX_PATHBUFFER)
     {
       g_pathbuffer.free_bitmap &= ~(1u << index);
-      nxmutex_unlock(&g_pathbuffer.lock);
+      spin_unlock_irqrestore(&g_pathbuffer.lock, flags);
       return g_pathbuffer.buffer[index];
     }
 
-  nxmutex_unlock(&g_pathbuffer.lock);
+  spin_unlock_irqrestore(&g_pathbuffer.lock, flags);
 
   /* If no free buffer is found, allocate a new one if
    * CONFIG_LIBC_PATHBUFFER_MALLOC is enabled
@@ -124,22 +125,20 @@ FAR char *lib_get_pathbuffer(void)
 
 void lib_put_pathbuffer(FAR char *buffer)
 {
+  irqstate_t flags;
   int index;
 
-  nxmutex_lock(&g_pathbuffer.lock);
   index = (buffer - &g_pathbuffer.buffer[0][0]) / PATH_MAX;
-
   if (index >= 0 && index < CONFIG_LIBC_MAX_PATHBUFFER)
     {
       /* Mark the corresponding bit as free */
 
+      flags = spin_lock_irqsave(&g_pathbuffer.lock);
       g_pathbuffer.free_bitmap |= 1u << index;
-      nxmutex_unlock(&g_pathbuffer.lock);
+      spin_unlock_irqrestore(&g_pathbuffer.lock, flags);
       return;
     }
 
-  nxmutex_unlock(&g_pathbuffer.lock);
-
   /* Free the buffer if it was dynamically allocated */
 
 #ifdef CONFIG_LIBC_PATHBUFFER_MALLOC

Reply via email to