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

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

commit 510f53d1ac6a7c6dd07f52fab6033ec259cc1a3e
Author: wangchen <[email protected]>
AuthorDate: Tue Jan 6 16:07:34 2026 +0800

    utils: Add conn_init/conn_uninit and replace direct s_lock ops
    
    Several transport modules initialised socket_conn_s::s_lock by calling
    nxrmutex_init()/nxrmutex_destroy() directly, duplicating the same code in
    every protocol.  Add inline conn_init()/conn_uninit() helpers in
    net/utils/utils.h that wrap the s_lock mutex init and destroy, and use them
    in can, icmp, icmpv6, local, pkt, tcp, udp and usrsock.  The lock lifecycle
    is now defined in one place, so later changes to socket_conn_s only need to
    touch a single helper.
    
    Signed-off-by: wangchen <[email protected]>
---
 net/can/can.c              | 10 ++++++++--
 net/icmp/icmp_conn.c       |  9 ++++++++-
 net/icmp/icmp_sockif.c     |  2 --
 net/icmpv6/icmpv6_conn.c   |  9 ++++++++-
 net/icmpv6/icmpv6_sockif.c |  2 --
 net/local/local_conn.c     | 12 +++++++++---
 net/pkt/pkt_conn.c         |  9 ++++++++-
 net/pkt/pkt_sockif.c       |  2 --
 net/tcp/tcp_conn.c         |  9 +++++++--
 net/udp/udp_conn.c         |  9 +++++++--
 net/usrsock/usrsock_conn.c | 10 ++++++++--
 net/utils/utils.h          | 43 +++++++++++++++++++++++++++++++++++++++++++
 12 files changed, 106 insertions(+), 20 deletions(-)

diff --git a/net/can/can.c b/net/can/can.c
index 603832b36b1..f9814844c8e 100644
--- a/net/can/can.c
+++ b/net/can/can.c
@@ -785,7 +785,10 @@ void can_conn_init(FAR struct can_conn_s *conn)
   conn->sndbufs = CONFIG_NET_SEND_BUFSIZE;
   nxsem_init(&conn->sndsem, 0, 0);
 #endif
-  nxrmutex_init(&conn->sconn.s_lock);
+
+  /* Use conn_init to initialize the connection structure */
+
+  conn_init(&conn->sconn);
 
   /* Enqueue the connection into the active list */
 
@@ -816,7 +819,6 @@ void can_conn_uninit(FAR struct can_conn_s *conn)
   /* Remove the connection from the active list */
 
   dq_rem(&conn->sconn.s_node, &g_active_can_connections);
-  nxrmutex_destroy(&conn->sconn.s_lock);
 
 #ifdef CONFIG_NET_CAN_WRITE_BUFFERS
   /* Free the write queue */
@@ -835,6 +837,10 @@ void can_conn_uninit(FAR struct can_conn_s *conn)
   nxsem_destroy(&conn->sndsem);
 #endif
 
+  /* Use conn_uninit to release all connection resources */
+
+  conn_uninit(&conn->sconn);
+
   /* Free the readahead queue */
 
   iob_free_queue(&conn->readahead);
diff --git a/net/icmp/icmp_conn.c b/net/icmp/icmp_conn.c
index 5c5dd3e8cc3..16cbc6cf7e1 100644
--- a/net/icmp/icmp_conn.c
+++ b/net/icmp/icmp_conn.c
@@ -92,6 +92,10 @@ FAR struct icmp_conn_s *icmp_alloc(void)
   conn = NET_BUFPOOL_TRYALLOC(g_icmp_connections);
   if (conn != NULL)
     {
+      /* Use conn_init to initialize the connection structure */
+
+      conn_init(&conn->sconn);
+
       /* Enqueue the connection into the active list */
 
       dq_addlast(&conn->sconn.s_node, &g_active_icmp_connections);
@@ -128,7 +132,10 @@ void icmp_free(FAR struct icmp_conn_s *conn)
   /* Remove the connection from the active list */
 
   dq_rem(&conn->sconn.s_node, &g_active_icmp_connections);
-  nxrmutex_destroy(&conn->sconn.s_lock);
+
+  /* Use conn_uninit to release all connection resources */
+
+  conn_uninit(&conn->sconn);
 
   /* Free the connection. */
 
diff --git a/net/icmp/icmp_sockif.c b/net/icmp/icmp_sockif.c
index ea45bad5334..cc9ee12ce17 100644
--- a/net/icmp/icmp_sockif.c
+++ b/net/icmp/icmp_sockif.c
@@ -147,8 +147,6 @@ static int icmp_setup(FAR struct socket *psock)
           conn->filter = UINT32_MAX;
         }
 
-      nxrmutex_init(&conn->sconn.s_lock);
-
       /* Save the pre-allocated connection in the socket structure */
 
       psock->s_conn = conn;
diff --git a/net/icmpv6/icmpv6_conn.c b/net/icmpv6/icmpv6_conn.c
index 63114f1f112..edff7012e99 100644
--- a/net/icmpv6/icmpv6_conn.c
+++ b/net/icmpv6/icmpv6_conn.c
@@ -93,6 +93,10 @@ FAR struct icmpv6_conn_s *icmpv6_alloc(void)
   conn = NET_BUFPOOL_TRYALLOC(g_icmpv6_connections);
   if (conn != NULL)
     {
+      /* Use conn_init to initialize the connection structure */
+
+      conn_init(&conn->sconn);
+
       /* Enqueue the connection into the active list */
 
       dq_addlast(&conn->sconn.s_node, &g_active_icmpv6_connections);
@@ -125,7 +129,10 @@ void icmpv6_free(FAR struct icmpv6_conn_s *conn)
   /* Remove the connection from the active list */
 
   dq_rem(&conn->sconn.s_node, &g_active_icmpv6_connections);
-  nxrmutex_destroy(&conn->sconn.s_lock);
+
+  /* Use conn_uninit to release all connection resources */
+
+  conn_uninit(&conn->sconn);
 
   /* Free the connection. */
 
diff --git a/net/icmpv6/icmpv6_sockif.c b/net/icmpv6/icmpv6_sockif.c
index 467ad229305..ba2c04a6784 100644
--- a/net/icmpv6/icmpv6_sockif.c
+++ b/net/icmpv6/icmpv6_sockif.c
@@ -146,8 +146,6 @@ static int icmpv6_setup(FAR struct socket *psock)
           memset(&conn->filter, 0xff, sizeof(conn->filter));
         }
 
-      nxrmutex_init(&conn->sconn.s_lock);
-
       /* Save the pre-allocated connection in the socket structure */
 
       psock->s_conn = conn;
diff --git a/net/local/local_conn.c b/net/local/local_conn.c
index 16126a58906..3baa869f07c 100644
--- a/net/local/local_conn.c
+++ b/net/local/local_conn.c
@@ -38,6 +38,7 @@
 #include <nuttx/queue.h>
 
 #include "local/local.h"
+#include "utils/utils.h"
 
 /****************************************************************************
  * Public Data
@@ -176,7 +177,7 @@ FAR struct local_conn_s *local_alloc(void)
 
   if (conn != NULL)
     {
-      /* Initialize non-zero elements the new connection structure.  Since
+      /* Initialize non-zero elements the new connection structure.
        * Since the memory was allocated with kmm_zalloc(), it is not
        * necessary to zerio-ize any structure elements.
        */
@@ -195,8 +196,10 @@ FAR struct local_conn_s *local_alloc(void)
       nxmutex_init(&conn->lc_sendlock);
       nxmutex_init(&conn->lc_recvlock);
       nxmutex_init(&conn->lc_polllock);
-      nxrmutex_init(&conn->lc_conn.s_lock);
 
+      /* Use conn_init to initialize the connection structure */
+
+      conn_init(&conn->lc_conn);
 #ifdef CONFIG_NET_LOCAL_SCM
       conn->lc_cred.pid = nxsched_getpid();
       conn->lc_cred.uid = getuid();
@@ -383,7 +386,10 @@ void local_free(FAR struct local_conn_s *conn)
   nxmutex_destroy(&conn->lc_sendlock);
   nxmutex_destroy(&conn->lc_recvlock);
   nxmutex_destroy(&conn->lc_polllock);
-  nxrmutex_destroy(&conn->lc_conn.s_lock);
+
+  /* Use conn_uninit to release all connection resources */
+
+  conn_uninit(&conn->lc_conn);
 
   /* And free the connection structure */
 
diff --git a/net/pkt/pkt_conn.c b/net/pkt/pkt_conn.c
index e91906ea48f..912284387d4 100644
--- a/net/pkt/pkt_conn.c
+++ b/net/pkt/pkt_conn.c
@@ -99,6 +99,10 @@ FAR struct pkt_conn_s *pkt_alloc(void)
   conn = NET_BUFPOOL_TRYALLOC(g_pkt_connections);
   if (conn)
     {
+      /* Use conn_init to initialize the connection structure */
+
+      conn_init(&conn->sconn);
+
       /* Enqueue the connection into the active list */
 
       dq_addlast(&conn->sconn.s_node, &g_active_pkt_connections);
@@ -128,7 +132,10 @@ void pkt_free(FAR struct pkt_conn_s *conn)
   /* Remove the connection from the active list */
 
   dq_rem(&conn->sconn.s_node, &g_active_pkt_connections);
-  nxrmutex_destroy(&conn->sconn.s_lock);
+
+  /* Use conn_uninit to release all connection resources */
+
+  conn_uninit(&conn->sconn);
 
 #ifdef CONFIG_NET_PKT_WRITE_BUFFERS
   /* Free the write queue */
diff --git a/net/pkt/pkt_sockif.c b/net/pkt/pkt_sockif.c
index 824ba194f3a..5643c01ddd5 100644
--- a/net/pkt/pkt_sockif.c
+++ b/net/pkt/pkt_sockif.c
@@ -138,8 +138,6 @@ static int pkt_sockif_alloc(FAR struct socket *psock)
   nxsem_init(&conn->sndsem, 0, 0);
 #endif
 
-  nxrmutex_init(&conn->sconn.s_lock);
-
   /* Save the pre-allocated connection in the socket structure */
 
   psock->s_conn = conn;
diff --git a/net/tcp/tcp_conn.c b/net/tcp/tcp_conn.c
index 43675bc4a66..3c6c7d038d1 100644
--- a/net/tcp/tcp_conn.c
+++ b/net/tcp/tcp_conn.c
@@ -738,7 +738,9 @@ FAR struct tcp_conn_s *tcp_alloc(uint8_t domain)
 
       nxsem_init(&conn->snd_sem, 0, 0);
 #endif
-      nxrmutex_init(&conn->sconn.s_lock);
+      /* Use conn_init to initialize the connection structure */
+
+      conn_init(&conn->sconn);
 
       /* Set the default value of mss to max, this field will changed when
        * receive SYN.
@@ -861,7 +863,6 @@ void tcp_free(FAR struct tcp_conn_s *conn)
 
   tcp_stop_timer(conn);
 
-  nxrmutex_destroy(&conn->sconn.s_lock);
   tcp_free_rx_buffers(conn);
 
 #ifdef CONFIG_NET_TCP_WRITE_BUFFERS
@@ -891,6 +892,10 @@ void tcp_free(FAR struct tcp_conn_s *conn)
   nxsem_destroy(&conn->snd_sem);
 #endif
 
+  /* Use conn_uninit to release all connection resources */
+
+  conn_uninit(&conn->sconn);
+
 #ifdef CONFIG_NET_TCPBACKLOG
   tcp_conn_list_lock();
 
diff --git a/net/udp/udp_conn.c b/net/udp/udp_conn.c
index cd7823b3b94..ff072a139bf 100644
--- a/net/udp/udp_conn.c
+++ b/net/udp/udp_conn.c
@@ -706,8 +706,10 @@ FAR struct udp_conn_s *udp_alloc(uint8_t domain)
 
       nxsem_init(&conn->sndsem, 0, 0);
 #endif
+      /* Use conn_init to initialize the connection structure */
+
+      conn_init(&conn->sconn);
 
-      nxrmutex_init(&conn->sconn.s_lock);
 #ifdef CONFIG_NET_UDP_WRITE_BUFFERS
       /* Initialize the write buffer lists */
 
@@ -747,7 +749,6 @@ void udp_free(FAR struct udp_conn_s *conn)
   /* Remove the connection from the active list */
 
   dq_rem(&conn->sconn.s_node, &g_active_udp_connections);
-  nxrmutex_destroy(&conn->sconn.s_lock);
 
   /* Release any read-ahead buffers attached to the connection, NULL is ok */
 
@@ -774,6 +775,10 @@ void udp_free(FAR struct udp_conn_s *conn)
   nxsem_destroy(&conn->sndsem);
 #endif
 
+  /* Use conn_uninit to release all connection resources */
+
+  conn_uninit(&conn->sconn);
+
   /* Free the connection. */
 
   NET_BUFPOOL_FREE(g_udp_connections, conn);
diff --git a/net/usrsock/usrsock_conn.c b/net/usrsock/usrsock_conn.c
index e00eb290127..0b76e6996b8 100644
--- a/net/usrsock/usrsock_conn.c
+++ b/net/usrsock/usrsock_conn.c
@@ -103,7 +103,10 @@ FAR struct usrsock_conn_s *usrsock_alloc(void)
       /* Make sure that the connection is marked as uninitialized */
 
       nxsem_init(&conn->resp.sem, 0, 1);
-      nxrmutex_init(&conn->sconn.s_lock);
+
+      /* Use conn_init to initialize the connection structure */
+
+      conn_init(&conn->sconn);
       conn->usockid = USRSOCK_USOCKID_INVALID;
       conn->state = USRSOCK_CONN_STATE_UNINITIALIZED;
 
@@ -140,7 +143,10 @@ void usrsock_free(FAR struct usrsock_conn_s *conn)
   /* Reset structure */
 
   nxsem_destroy(&conn->resp.sem);
-  nxrmutex_destroy(&conn->sconn.s_lock);
+
+  /* Use conn_uninit to release all connection resources */
+
+  conn_uninit(&conn->sconn);
 
   /* Free the connection. */
 
diff --git a/net/utils/utils.h b/net/utils/utils.h
index 01bc890a08c..94e8cada27a 100644
--- a/net/utils/utils.h
+++ b/net/utils/utils.h
@@ -154,6 +154,49 @@ extern "C"
  * Inline Functions
  ****************************************************************************/
 
+/****************************************************************************
+ * Name: conn_init
+ *
+ * Description:
+ *   Initialize a connection structure, including s_lock mutex
+ *
+ * Parameters:
+ *   sconn - Pointer to the socket_conn_s structure to initialize
+ *
+ * Return:
+ *   None
+ *
+ ****************************************************************************/
+
+static inline_function void conn_init(FAR struct socket_conn_s *sconn)
+{
+  DEBUGASSERT(sconn != NULL);
+
+  /* Initialize the connection lock */
+
+  nxrmutex_init(&sconn->s_lock);
+}
+
+/****************************************************************************
+ * Name: conn_uninit
+ *
+ * Description:
+ *   Uninitialize a connection structure, releasing all resources
+ *
+ * Parameters:
+ *   sconn - Pointer to the socket_conn_s structure to uninitialize
+ *
+ ****************************************************************************/
+
+static inline_function void conn_uninit(FAR struct socket_conn_s *sconn)
+{
+  DEBUGASSERT(sconn != NULL);
+
+  /* Destroy the connection lock */
+
+  nxrmutex_destroy(&sconn->s_lock);
+}
+
 /****************************************************************************
  * Name: conn_lock, conn_unlock, conn_dev_lock, conn_dev_unlock
  *

Reply via email to