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 5e1410ecabf41961c3bfa7eeddad3c9996e52f30 Author: zhanghongyu <[email protected]> AuthorDate: Mon Nov 10 21:47:16 2025 +0800 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 <[email protected]> --- net/local/local.h | 34 ++++++++++++++++++++++++++++++++++ net/local/local_accept.c | 8 +++++++- net/local/local_bind.c | 6 +++--- net/local/local_conn.c | 10 ++++++++++ net/local/local_connect.c | 16 +++++++--------- net/local/local_listen.c | 6 +++--- net/local/local_recvmsg.c | 4 ++-- net/local/local_release.c | 4 ++-- net/local/local_sendmsg.c | 16 ++++++++-------- net/local/local_sockif.c | 12 ++++++------ 10 files changed, 82 insertions(+), 34 deletions(-) diff --git a/net/local/local.h b/net/local/local.h index a15e59eeef5..bdeb14bd4c3 100644 --- a/net/local/local.h +++ b/net/local/local.h @@ -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 ****************************************************************************/ diff --git a/net/local/local_accept.c b/net/local/local_accept.c index a950fb7c37e..10e675c3f91 100644 --- a/net/local/local_accept.c +++ b/net/local/local_accept.c @@ -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; } } diff --git a/net/local/local_bind.c b/net/local/local_bind.c index 6bcb8c221f5..593534faddf 100644 --- a/net/local/local_bind.c +++ b/net/local/local_bind.c @@ -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 */ diff --git a/net/local/local_conn.c b/net/local/local_conn.c index 13f071fad04..2ec417e4269 100644 --- a/net/local/local_conn.c +++ b/net/local/local_conn.c @@ -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 */ diff --git a/net/local/local_connect.c b/net/local/local_connect.c index 1778a5ce0dc..3661caba8ec 100644 --- a/net/local/local_connect.c +++ b/net/local/local_connect.c @@ -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; } diff --git a/net/local/local_listen.c b/net/local/local_listen.c index 1104760ee9f..1e0fdaa3c18 100644 --- a/net/local/local_listen.c +++ b/net/local/local_listen.c @@ -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; } diff --git a/net/local/local_recvmsg.c b/net/local/local_recvmsg.c index 14fe7c7041a..eff19e77826 100644 --- a/net/local/local_recvmsg.c +++ b/net/local/local_recvmsg.c @@ -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 */ diff --git a/net/local/local_release.c b/net/local/local_release.c index 0f32da000db..86973b4d8fb 100644 --- a/net/local/local_release.c +++ b/net/local/local_release.c @@ -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; } diff --git a/net/local/local_sendmsg.c b/net/local/local_sendmsg.c index 72fe2049c93..c74a2ec5435 100644 --- a/net/local/local_sendmsg.c +++ b/net/local/local_sendmsg.c @@ -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) : diff --git a/net/local/local_sockif.c b/net/local/local_sockif.c index 1cefb25e1d8..3db4187acd9 100644 --- a/net/local/local_sockif.c +++ b/net/local/local_sockif.c @@ -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; }
