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

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

commit e4d4f7e2c349f79da61a8a8ccae60344c466c10c
Author: fangpeina <[email protected]>
AuthorDate: Tue Jun 17 12:09:32 2025 +0800

    drivers/math: use small lock to replace enter_critical_section
    
    replace critical_section with spinlock
    
    Signed-off-by: fangpeina <[email protected]>
---
 drivers/math/math.c       | 10 +++++++---
 include/nuttx/math/math.h |  2 ++
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/drivers/math/math.c b/drivers/math/math.c
index 6371f568e91..ed535b49895 100644
--- a/drivers/math/math.c
+++ b/drivers/math/math.c
@@ -117,13 +117,12 @@ static int math_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
   FAR struct inode              *inode =  filep->f_inode;
   FAR math_upperhalf_s          *upper =  inode->i_private;
   int                            ret   = -ENOTTY;
-  irqstate_t                     flags;
 
   DEBUGASSERT(upper != NULL);
 
   _info("cmd: %d arg: %lu\n", cmd, arg);
 
-  flags = enter_critical_section();
+  nxmutex_lock(&upper->lock);
 
   switch (cmd)
     {
@@ -175,7 +174,7 @@ static int math_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
 #endif
     }
 
-  leave_critical_section(flags);
+  nxmutex_unlock(&upper->lock);
 
   return ret;
 }
@@ -215,12 +214,17 @@ int math_register(FAR const char *path,
 
   memcpy(upper, config, sizeof(math_upperhalf_s));
 
+  /* Initialize the mutex lock */
+
+  nxmutex_init(&upper->lock);
+
   /* Register the math timer device */
 
   ret = register_driver(path, &g_math_ops, 0666, upper);
   if (ret < 0)
     {
       _err("register math driver failed: %d\n", ret);
+      nxmutex_destroy(&upper->lock);
       kmm_free(upper);
     }
 
diff --git a/include/nuttx/math/math.h b/include/nuttx/math/math.h
index bfe0ccd7a7e..3d3beffd7fb 100644
--- a/include/nuttx/math/math.h
+++ b/include/nuttx/math/math.h
@@ -27,6 +27,7 @@
  * Included Files
  ****************************************************************************/
 
+#include <nuttx/mutex.h>
 #ifdef CONFIG_MATH_CORDIC
 #include <nuttx/math/cordic.h>
 #endif
@@ -57,6 +58,7 @@ struct math_config_s
 #ifdef CONFIG_MATH_MPI
   FAR struct mpi_lowerhalf_s    *mpi;
 #endif
+  mutex_t lock;
 };
 
 /****************************************************************************

Reply via email to