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/nuttx.git

commit 92b3357182ecc00d30be456f1046beb9139750e8
Author: zhanghongyu <[email protected]>
AuthorDate: Mon Nov 17 21:02:49 2025 +0800

    net/bluetooth: replace net_lock with conn_lock and conn_lock_dev
    
    decouple lock dependencies from other modules.
    
    Signed-off-by: zhanghongyu <[email protected]>
---
 net/bluetooth/bluetooth.h           | 35 +++++++++++++++++++++++++++++++++++
 net/bluetooth/bluetooth_callback.c  |  3 +++
 net/bluetooth/bluetooth_conn.c      | 16 ++++++++++++----
 net/bluetooth/bluetooth_container.c | 12 ++++++------
 net/bluetooth/bluetooth_recvmsg.c   | 11 +++++++----
 net/bluetooth/bluetooth_sendmsg.c   |  9 +++++----
 net/devif/devif_poll.c              |  3 +++
 7 files changed, 71 insertions(+), 18 deletions(-)

diff --git a/net/bluetooth/bluetooth.h b/net/bluetooth/bluetooth.h
index 11c0606e3fa..c45c4333238 100644
--- a/net/bluetooth/bluetooth.h
+++ b/net/bluetooth/bluetooth.h
@@ -32,6 +32,7 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 
+#include <nuttx/mutex.h>
 #include <nuttx/net/net.h>
 
 #include <nuttx/wireless/bluetooth/bt_hci.h>
@@ -119,6 +120,40 @@ extern "C"
 
 EXTERN const struct sock_intf_s g_bluetooth_sockif;
 
+/* The Bluetooth connections rmutex */
+
+extern rmutex_t g_bluetooth_connections_lock;
+
+/****************************************************************************
+ * Inline Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: bluetooth_conn_list_lock
+ *
+ * Description:
+ *   Lock the Bluetooth connection list.
+ *
+ ****************************************************************************/
+
+static inline_function void bluetooth_conn_list_lock(void)
+{
+  nxrmutex_lock(&g_bluetooth_connections_lock);
+}
+
+/****************************************************************************
+ * Name: bluetooth_conn_list_unlock
+ *
+ * Description:
+ *   Unlock the Bluetooth connection list.
+ *
+ ****************************************************************************/
+
+static inline_function void bluetooth_conn_list_unlock(void)
+{
+  nxrmutex_unlock(&g_bluetooth_connections_lock);
+}
+
 /****************************************************************************
  * Public Function Prototypes
  ****************************************************************************/
diff --git a/net/bluetooth/bluetooth_callback.c 
b/net/bluetooth/bluetooth_callback.c
index 50487c769cb..c3f675238b7 100644
--- a/net/bluetooth/bluetooth_callback.c
+++ b/net/bluetooth/bluetooth_callback.c
@@ -37,6 +37,7 @@
 #include <nuttx/net/bluetooth.h>
 
 #include "devif/devif.h"
+#include "utils/utils.h"
 #include "bluetooth/bluetooth.h"
 
 #ifdef CONFIG_NET_BLUETOOTH
@@ -71,7 +72,9 @@ uint32_t bluetooth_callback(FAR struct radio_driver_s *radio,
     {
       /* Perform the callback */
 
+      conn_lock(&conn->bc_conn);
       flags = devif_conn_event(&radio->r_dev, flags, conn->bc_conn.list);
+      conn_unlock(&conn->bc_conn);
     }
 
   return flags;
diff --git a/net/bluetooth/bluetooth_conn.c b/net/bluetooth/bluetooth_conn.c
index 9deb36247ce..abd7b6cc761 100644
--- a/net/bluetooth/bluetooth_conn.c
+++ b/net/bluetooth/bluetooth_conn.c
@@ -55,6 +55,14 @@
 #  define CONFIG_NET_BLUETOOTH_MAX_CONNS 0
 #endif
 
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/* The Bluetooth connections rmutex */
+
+rmutex_t g_bluetooth_connections_lock = NXRMUTEX_INITIALIZER;
+
 /****************************************************************************
  * Private Data
  ****************************************************************************/
@@ -96,7 +104,7 @@ FAR struct bluetooth_conn_s *bluetooth_conn_alloc(void)
 
   /* The free list is protected by the network lock */
 
-  net_lock();
+  bluetooth_conn_list_lock();
 
   conn = NET_BUFPOOL_TRYALLOC(g_bluetooth_connections);
   if (conn)
@@ -110,7 +118,7 @@ FAR struct bluetooth_conn_s *bluetooth_conn_alloc(void)
       dq_addlast(&conn->bc_conn.node, &g_active_bluetooth_connections);
     }
 
-  net_unlock();
+  bluetooth_conn_list_unlock();
   return conn;
 }
 
@@ -134,7 +142,7 @@ void bluetooth_conn_free(FAR struct bluetooth_conn_s *conn)
 
   /* Remove the connection from the active list */
 
-  net_lock();
+  bluetooth_conn_list_lock();
   dq_rem(&conn->bc_conn.node, &g_active_bluetooth_connections);
 
   /* Check if there any any frames attached to the container */
@@ -162,7 +170,7 @@ void bluetooth_conn_free(FAR struct bluetooth_conn_s *conn)
 
   NET_BUFPOOL_FREE(g_bluetooth_connections, conn);
 
-  net_unlock();
+  bluetooth_conn_list_unlock();
 }
 
 /****************************************************************************
diff --git a/net/bluetooth/bluetooth_container.c 
b/net/bluetooth/bluetooth_container.c
index 3a337b1454a..6ef1f4ce162 100644
--- a/net/bluetooth/bluetooth_container.c
+++ b/net/bluetooth/bluetooth_container.c
@@ -133,17 +133,17 @@ FAR struct bluetooth_container_s 
*bluetooth_container_allocate(void)
 
   /* Try the free list first */
 
-  net_lock();
+  bluetooth_conn_list_lock();
   if (g_free_container != NULL)
     {
       container        = g_free_container;
       g_free_container = container->bn_flink;
       pool             = BLUETOOTH_POOL_PREALLOCATED;
-      net_unlock();
+      bluetooth_conn_list_unlock();
     }
   else
     {
-      net_unlock();
+      bluetooth_conn_list_unlock();
       container = (FAR struct bluetooth_container_s *)
         kmm_malloc((sizeof(struct bluetooth_container_s)));
       pool = BLUETOOTH_POOL_DYNAMIC;
@@ -188,12 +188,12 @@ void bluetooth_container_free(FAR struct 
bluetooth_container_s *container)
    * in the free list.
    */
 
-  net_lock();
+  bluetooth_conn_list_lock();
   if (container->bn_pool == BLUETOOTH_POOL_PREALLOCATED)
     {
       container->bn_flink = g_free_container;
       g_free_container    = container;
-      net_unlock();
+      bluetooth_conn_list_unlock();
     }
   else
     {
@@ -201,7 +201,7 @@ void bluetooth_container_free(FAR struct 
bluetooth_container_s *container)
 
       /* Otherwise, deallocate it. */
 
-      net_unlock();
+      bluetooth_conn_list_unlock();
       kmm_free(container);
     }
 }
diff --git a/net/bluetooth/bluetooth_recvmsg.c 
b/net/bluetooth/bluetooth_recvmsg.c
index da42b2a28a6..8ba4d22038e 100644
--- a/net/bluetooth/bluetooth_recvmsg.c
+++ b/net/bluetooth/bluetooth_recvmsg.c
@@ -47,6 +47,7 @@
 #include "netdev/netdev.h"
 #include "devif/devif.h"
 #include "socket/socket.h"
+#include "utils/utils.h"
 #include "bluetooth/bluetooth.h"
 
 #ifdef CONFIG_NET_BLUETOOTH
@@ -341,7 +342,6 @@ ssize_t bluetooth_recvmsg(FAR struct socket *psock, FAR 
struct msghdr *msg,
    * locked because we don't want anything to happen until we are ready.
    */
 
-  net_lock();
   memset(&state, 0, sizeof(struct bluetooth_recvfrom_s));
 
   state.ir_buflen = len;
@@ -358,6 +358,8 @@ ssize_t bluetooth_recvmsg(FAR struct socket *psock, FAR 
struct msghdr *msg,
       goto errout_with_lock;
     }
 
+  conn_dev_lock(&conn->bc_conn, &radio->r_dev);
+
   /* Before we wait for data, let's check if there are already frame(s)
    * waiting in the RX queue.
    */
@@ -367,7 +369,7 @@ ssize_t bluetooth_recvmsg(FAR struct socket *psock, FAR 
struct msghdr *msg,
     {
       /* Good newe!  We have a frame and we are done. */
 
-      net_unlock();
+      conn_dev_unlock(&conn->bc_conn, &radio->r_dev);
       return ret;
     }
 
@@ -388,7 +390,8 @@ ssize_t bluetooth_recvmsg(FAR struct socket *psock, FAR 
struct msghdr *msg,
        * the task sleeps and automatically re-locked when the task restarts.
        */
 
-      net_sem_wait(&state.ir_sem);
+      conn_dev_sem_timedwait(&state.ir_sem, true, UINT_MAX,
+                             &conn->bc_conn, &radio->r_dev);
 
       /* Make sure that no further events are processed */
 
@@ -403,7 +406,7 @@ ssize_t bluetooth_recvmsg(FAR struct socket *psock, FAR 
struct msghdr *msg,
   nxsem_destroy(&state.ir_sem);
 
 errout_with_lock:
-  net_unlock();
+  conn_dev_unlock(&conn->bc_conn, &radio->r_dev);
   return ret;
 }
 
diff --git a/net/bluetooth/bluetooth_sendmsg.c 
b/net/bluetooth/bluetooth_sendmsg.c
index 201393b72c7..2ca5a5694fa 100644
--- a/net/bluetooth/bluetooth_sendmsg.c
+++ b/net/bluetooth/bluetooth_sendmsg.c
@@ -302,7 +302,7 @@ static ssize_t bluetooth_sendto(FAR struct socket *psock,
    * because we don't want anything to happen until we are ready.
    */
 
-  net_lock();
+  conn_dev_lock(&conn->bc_conn, &radio->r_dev);
   memset(&state, 0, sizeof(struct bluetooth_sendto_s));
   nxsem_init(&state.is_sem, 0, 0); /* Doesn't really fail */
 
@@ -350,7 +350,8 @@ static ssize_t bluetooth_sendto(FAR struct socket *psock,
            * net_sem_wait will also terminate if a signal is received.
            */
 
-          ret = net_sem_wait(&state.is_sem);
+          ret = conn_dev_sem_timedwait(&state.is_sem, true, UINT_MAX,
+                                       &conn->bc_conn, &radio->r_dev);
 
           /* Make sure that no further events are processed */
 
@@ -359,7 +360,7 @@ static ssize_t bluetooth_sendto(FAR struct socket *psock,
     }
 
   nxsem_destroy(&state.is_sem);
-  net_unlock();
+  conn_dev_unlock(&conn->bc_conn, &radio->r_dev);
 
   /* Check for a errors, Errors are signaled by negative errno values
    * for the send length
@@ -386,7 +387,7 @@ static ssize_t bluetooth_sendto(FAR struct socket *psock,
 
 err_with_net:
   nxsem_destroy(&state.is_sem);
-  net_unlock();
+  conn_dev_unlock(&conn->bc_conn, &radio->r_dev);
 
   return ret;
 }
diff --git a/net/devif/devif_poll.c b/net/devif/devif_poll.c
index 777c31c7cc7..00445ceb1c6 100644
--- a/net/devif/devif_poll.c
+++ b/net/devif/devif_poll.c
@@ -340,6 +340,7 @@ devif_poll_bluetooth_connections(FAR struct net_driver_s 
*dev,
    * action.
    */
 
+  bluetooth_conn_list_lock();
   while (!bstop && (bluetooth_conn = bluetooth_conn_next(bluetooth_conn)))
     {
       /* Perform the packet TX poll */
@@ -354,6 +355,8 @@ devif_poll_bluetooth_connections(FAR struct net_driver_s 
*dev,
         }
     }
 
+  bluetooth_conn_list_unlock();
+
   return bstop;
 }
 #endif /* CONFIG_NET_BLUETOOTH */

Reply via email to