net/bluetooth: replace net_lock with conn_lock and conn_lock_dev

decouple lock dependencies from other modules.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
This commit is contained in:
zhanghongyu
2025-11-17 21:02:49 +08:00
committed by Alan C. Assis
parent 11c4bb7f31
commit 92b3357182
7 changed files with 71 additions and 18 deletions
+35
View File
@@ -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
****************************************************************************/
+3
View File
@@ -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;
+12 -4
View File
@@ -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();
}
/****************************************************************************
+6 -6
View File
@@ -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);
}
}
+7 -4
View File
@@ -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;
}
+5 -4
View File
@@ -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;
}
+3
View File
@@ -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 */