net/local: replace net_lock with local_lock

remove the use of net_lock in the local module and decouple it from
other network modules.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
This commit is contained in:
zhanghongyu
2025-11-10 21:47:16 +08:00
committed by Donny(董九柱)
parent c2774ba1ec
commit 5e1410ecab
10 changed files with 82 additions and 34 deletions
+34
View File
@@ -196,6 +196,40 @@ extern "C"
EXTERN const struct sock_intf_s g_local_sockif;
/* Global protection lock for local socket */
extern mutex_t g_local_lock;
/****************************************************************************
* Inline Functions
****************************************************************************/
/****************************************************************************
* Name: local_lock
*
* Description:
* Take the global local socket lock
*
****************************************************************************/
static inline_function void local_lock(void)
{
nxmutex_lock(&g_local_lock);
}
/****************************************************************************
* Name: local_unlock
*
* Description:
* Release the global local socket lock
*
****************************************************************************/
static inline_function void local_unlock(void)
{
nxmutex_unlock(&g_local_lock);
}
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
+7 -1
View File
@@ -56,7 +56,9 @@ static int local_waitlisten(FAR struct local_conn_s *server)
{
/* No.. wait for a connection or a signal */
ret = net_sem_wait(&server->lc_waitsem);
local_unlock();
ret = nxsem_wait(&server->lc_waitsem);
local_lock();
if (ret < 0)
{
return ret;
@@ -125,6 +127,7 @@ int local_accept(FAR struct socket *psock, FAR struct sockaddr *addr,
/* Loop as necessary if we have to wait for a connection */
local_lock();
for (; ; )
{
/* Are there pending connections. Remove the accept from the
@@ -161,6 +164,7 @@ int local_accept(FAR struct socket *psock, FAR struct sockaddr *addr,
ret = local_set_nonblocking(conn);
}
local_unlock();
return ret;
}
@@ -174,6 +178,7 @@ int local_accept(FAR struct socket *psock, FAR struct sockaddr *addr,
{
/* Yes.. return EAGAIN */
local_unlock();
return -EAGAIN;
}
@@ -182,6 +187,7 @@ int local_accept(FAR struct socket *psock, FAR struct sockaddr *addr,
ret = local_waitlisten(server);
if (ret < 0)
{
local_unlock();
return ret;
}
}
+3 -3
View File
@@ -66,14 +66,14 @@ int psock_local_bind(FAR struct socket *psock,
/* Check if local address is already in use */
net_lock();
local_lock();
if (local_findconn(conn, unaddr) != NULL)
{
net_unlock();
local_unlock();
return -EADDRINUSE;
}
net_unlock();
local_unlock();
/* Save the address family */
+10
View File
@@ -38,6 +38,14 @@
#include "local/local.h"
/****************************************************************************
* Public Data
****************************************************************************/
/* Global protection lock for local socket */
mutex_t g_local_lock = NXMUTEX_INITIALIZER;
/****************************************************************************
* Private Data
****************************************************************************/
@@ -168,6 +176,7 @@ FAR struct local_conn_s *local_alloc(void)
nxmutex_init(&conn->lc_sendlock);
nxmutex_init(&conn->lc_polllock);
nxrmutex_init(&conn->lc_conn.s_lock);
#ifdef CONFIG_NET_LOCAL_SCM
conn->lc_cred.pid = nxsched_getpid();
@@ -347,6 +356,7 @@ void local_free(FAR struct local_conn_s *conn)
nxmutex_destroy(&conn->lc_sendlock);
nxmutex_destroy(&conn->lc_polllock);
nxrmutex_destroy(&conn->lc_conn.s_lock);
/* And free the connection structure */
+7 -9
View File
@@ -88,9 +88,7 @@ static int inline local_stream_connect(FAR struct local_conn_s *client,
return -ECONNREFUSED;
}
net_lock();
ret = local_alloc_accept(server, client, &conn);
net_unlock();
if (ret < 0)
{
nerr("ERROR: Failed to alloc accept conn %s: %d\n",
@@ -151,9 +149,9 @@ errout_with_outfd:
errout_with_conn:
local_release_fifos(conn);
client->lc_state = LOCAL_STATE_BOUND;
net_lock();
local_lock();
local_free(conn);
net_unlock();
local_unlock();
return ret;
}
@@ -175,7 +173,7 @@ int32_t local_generate_instance_id(void)
static int32_t g_next_instance_id = 0;
int32_t id;
/* Called from local_connect with net_lock held. */
/* Called from local_connect with local_lock held. */
id = g_next_instance_id++;
if (g_next_instance_id < 0)
@@ -232,7 +230,7 @@ int psock_local_connect(FAR struct socket *psock,
/* Find the matching server connection */
net_lock();
local_lock();
while ((conn = local_nextconn(conn)) != NULL)
{
/* Self found, continue */
@@ -275,7 +273,7 @@ int psock_local_connect(FAR struct socket *psock,
ret = local_stream_connect(client, conn,
_SS_ISNONBLOCK(client->lc_conn.s_flags));
net_unlock();
local_unlock();
return ret;
}
@@ -283,12 +281,12 @@ int psock_local_connect(FAR struct socket *psock,
default: /* Bad, memory must be corrupted */
DEBUGPANIC(); /* PANIC if debug on */
net_unlock();
local_unlock();
return -EINVAL;
}
}
net_unlock();
local_unlock();
ret = nx_stat(unpath, &buf, 1);
return ret < 0 ? ret : -ECONNREFUSED;
}
+3 -3
View File
@@ -80,14 +80,14 @@ int local_listen(FAR struct socket *psock, int backlog)
return -EOPNOTSUPP;
}
net_lock();
local_lock();
/* Some sanity checks */
if (server->lc_proto != SOCK_STREAM ||
server->lc_state == LOCAL_STATE_UNBOUND)
{
net_unlock();
local_unlock();
return -EOPNOTSUPP;
}
@@ -114,7 +114,7 @@ int local_listen(FAR struct socket *psock, int backlog)
server->lc_state = LOCAL_STATE_LISTENING;
}
net_unlock();
local_unlock();
return OK;
}
+2 -2
View File
@@ -145,7 +145,7 @@ static void local_recvctl(FAR struct local_conn_s *conn,
int *fds;
int i;
net_lock();
local_lock();
if (conn->lc_peer == NULL)
{
@@ -197,7 +197,7 @@ static void local_recvctl(FAR struct local_conn_s *conn,
}
out:
net_unlock();
local_unlock();
}
#endif /* CONFIG_NET_LOCAL_SCM */
+2 -2
View File
@@ -58,7 +58,7 @@ int local_release(FAR struct local_conn_s *conn)
/* There should be no references on this structure */
DEBUGASSERT(conn->lc_crefs == 0);
net_lock();
local_lock();
#ifdef CONFIG_NET_LOCAL_STREAM
/* We should not bet here with state LOCAL_STATE_ACCEPT. That is an
@@ -97,6 +97,6 @@ int local_release(FAR struct local_conn_s *conn)
/* Free the connection structure */
local_free(conn);
net_unlock();
local_unlock();
return OK;
}
+8 -8
View File
@@ -81,7 +81,7 @@ static int local_sendctl(FAR struct local_conn_s *conn,
int ret;
int i = 0;
net_lock();
local_lock();
peer = conn->lc_peer;
if (peer == NULL)
{
@@ -119,12 +119,12 @@ static int local_sendctl(FAR struct local_conn_s *conn,
}
}
net_unlock();
local_unlock();
return count;
fail:
local_freectl(conn, i);
net_unlock();
local_unlock();
return ret;
}
#endif /* CONFIG_NET_LOCAL_SCM */
@@ -293,17 +293,17 @@ static ssize_t local_sendto(FAR struct socket *psock,
return -EISCONN;
}
net_lock();
local_lock();
server = local_findconn(conn, unaddr);
if (server == NULL)
{
net_unlock();
local_unlock();
nerr("ERROR: No such file or directory\n");
return -ENOENT;
}
net_unlock();
local_unlock();
/* Make sure that dgram is sent safely */
@@ -437,9 +437,9 @@ ssize_t local_sendmsg(FAR struct socket *psock, FAR struct msghdr *msg,
if (len < 0 && count > 0)
{
net_lock();
local_lock();
local_freectl(conn, count);
net_unlock();
local_unlock();
}
#else
len = to ? local_sendto(psock, buf, len, flags, to, tolen) :
+6 -6
View File
@@ -132,9 +132,9 @@ static int local_sockif_alloc(FAR struct socket *psock)
/* Allocate the local connection structure */
FAR struct local_conn_s *conn;
net_lock();
local_lock();
conn = local_alloc();
net_unlock();
local_unlock();
if (conn == NULL)
{
/* Failed to reserve a connection structure */
@@ -655,7 +655,7 @@ static int local_setsockopt(FAR struct socket *psock, int level, int option,
return -EINVAL;
}
net_lock();
local_lock();
/* Only SOCK_STREAM sockets need set the send buffer size */
@@ -681,7 +681,7 @@ static int local_setsockopt(FAR struct socket *psock, int level, int option,
}
#endif
net_unlock();
local_unlock();
return ret;
}
@@ -696,7 +696,7 @@ static int local_setsockopt(FAR struct socket *psock, int level, int option,
return -EINVAL;
}
net_lock();
local_lock();
rcvsize = *(FAR const int *)value;
#ifdef CONFIG_NET_LOCAL_DGRAM
@@ -740,7 +740,7 @@ static int local_setsockopt(FAR struct socket *psock, int level, int option,
conn->lc_rcvsize = rcvsize;
}
net_unlock();
local_unlock();
return ret;
}