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

commit b9b62a9204d7b695ef2788ca04f5830a43bf71b6
Author: yangsong8 <[email protected]>
AuthorDate: Tue Jun 17 20:45:43 2025 +0800

    drivers/usbhost: Use small lock to protect usbhost common function
    
    replace critical_section with spinlock
    
    Signed-off-by: yangsong8 <[email protected]>
---
 drivers/usbhost/usbhost_findclass.c     | 6 +++---
 drivers/usbhost/usbhost_registerclass.c | 4 ++--
 drivers/usbhost/usbhost_registry.c      | 1 +
 drivers/usbhost/usbhost_registry.h      | 2 ++
 drivers/usbhost/usbhost_trace.c         | 6 ++++--
 5 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/drivers/usbhost/usbhost_findclass.c 
b/drivers/usbhost/usbhost_findclass.c
index d6ee6bcbffc..642aacf7899 100644
--- a/drivers/usbhost/usbhost_findclass.c
+++ b/drivers/usbhost/usbhost_findclass.c
@@ -139,7 +139,7 @@ const struct usbhost_registry_s *usbhost_findclass(
    * protected by disabling interrupts.
    */
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave_nopreempt(&g_classregistry_lock);
 
   /* Examine each register class in the linked list */
 
@@ -158,7 +158,7 @@ const struct usbhost_registry_s *usbhost_findclass(
             {
               /* Yes.. restore interrupts and return the class info */
 
-              leave_critical_section(flags);
+              spin_unlock_irqrestore_nopreempt(&g_classregistry_lock, flags);
               return usbclass;
             }
         }
@@ -166,6 +166,6 @@ const struct usbhost_registry_s *usbhost_findclass(
 
   /* Not found... restore interrupts and return NULL */
 
-  leave_critical_section(flags);
+  spin_unlock_irqrestore_nopreempt(&g_classregistry_lock, flags);
   return NULL;
 }
diff --git a/drivers/usbhost/usbhost_registerclass.c 
b/drivers/usbhost/usbhost_registerclass.c
index bdd141ae572..24b6f878520 100644
--- a/drivers/usbhost/usbhost_registerclass.c
+++ b/drivers/usbhost/usbhost_registerclass.c
@@ -91,13 +91,13 @@ int usbhost_registerclass(struct usbhost_registry_s 
*usbclass)
    * protected by disabling interrupts.
    */
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&g_classregistry_lock);
 
   /* Add the new class ID info to the head of the list */
 
   usbclass->flink = g_classregistry;
   g_classregistry = usbclass;
 
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&g_classregistry_lock, flags);
   return OK;
 }
diff --git a/drivers/usbhost/usbhost_registry.c 
b/drivers/usbhost/usbhost_registry.c
index 0aaf8995c69..f05cf3d8f45 100644
--- a/drivers/usbhost/usbhost_registry.c
+++ b/drivers/usbhost/usbhost_registry.c
@@ -57,6 +57,7 @@
  */
 
 struct usbhost_registry_s *g_classregistry;
+spinlock_t g_classregistry_lock = SP_UNLOCKED;
 
 /****************************************************************************
  * Private Functions
diff --git a/drivers/usbhost/usbhost_registry.h 
b/drivers/usbhost/usbhost_registry.h
index e1f28b3830d..18569304542 100644
--- a/drivers/usbhost/usbhost_registry.h
+++ b/drivers/usbhost/usbhost_registry.h
@@ -28,6 +28,7 @@
  ****************************************************************************/
 
 #include <nuttx/config.h>
+#include <nuttx/spinlock.h>
 
 #include <nuttx/usb/usbhost.h>
 
@@ -61,6 +62,7 @@ extern "C"
  */
 
 EXTERN struct usbhost_registry_s *g_classregistry;
+EXTERN spinlock_t g_classregistry_lock;
 
 /****************************************************************************
  * Public Function Prototypes
diff --git a/drivers/usbhost/usbhost_trace.c b/drivers/usbhost/usbhost_trace.c
index 378fc13be1b..36754c0ff09 100644
--- a/drivers/usbhost/usbhost_trace.c
+++ b/drivers/usbhost/usbhost_trace.c
@@ -25,6 +25,7 @@
  ****************************************************************************/
 
 #include <nuttx/config.h>
+#include <nuttx/spinlock.h>
 
 #include <sys/types.h>
 #include <stdint.h>
@@ -60,6 +61,7 @@
 
 #ifdef CONFIG_USBHOST_TRACE
 static uint32_t g_trace[CONFIG_USBHOST_TRACE_NRECORDS];
+static spinlock_t g_usbhost_trace_lock = SP_UNLOCKED;
 static volatile uint16_t g_head = 0;
 static volatile uint16_t g_tail = 0;
 static volatile bool g_disabled = false;
@@ -136,7 +138,7 @@ void usbhost_trace_common(uint32_t event)
 
   /* Check if tracing is enabled for this ID */
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&g_usbhost_trace_lock);
   if (!g_disabled)
     {
       /* Yes... save the new trace data at the head */
@@ -159,7 +161,7 @@ void usbhost_trace_common(uint32_t event)
         }
     }
 
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&g_usbhost_trace_lock, flags);
 }
 #endif /* CONFIG_USBHOST_TRACE */
 

Reply via email to