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

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

commit 8b632e0f85fae15cd815ce65863140f8ba6f7dc7
Author: zhanghongyu <[email protected]>
AuthorDate: Wed Dec 17 17:01:18 2025 +0800

    net/pkt: fix dead lock issue and optimize Performance
    
    minimize the logic of locking and unlocking, attempt to acquire resources
    first, and when acquisition fails and it is in block mode, unlock and then
    wait to acquire resources.
    
    Signed-off-by: zhanghongyu <[email protected]>
---
 net/pkt/pkt_sendmsg_buffered.c | 29 ++++++++++++-----------------
 1 file changed, 12 insertions(+), 17 deletions(-)

diff --git a/net/pkt/pkt_sendmsg_buffered.c b/net/pkt/pkt_sendmsg_buffered.c
index b9d5ac98ad4..6a03310a9b2 100644
--- a/net/pkt/pkt_sendmsg_buffered.c
+++ b/net/pkt/pkt_sendmsg_buffered.c
@@ -264,13 +264,12 @@ ssize_t pkt_sendmsg(FAR struct socket *psock, FAR const 
struct msghdr *msg,
     }
 #endif
 
-  if (nonblock)
-    {
-      iob = iob_tryalloc(false);
-    }
-  else
+  iob = iob_tryalloc(true);
+  if (iob == NULL && !nonblock)
     {
+      conn_dev_unlock(&conn->sconn, dev);
       iob = net_iobtimedalloc(true, _SO_TIMEOUT(conn->sconn.s_sndtimeo));
+      conn_dev_lock(&conn->sconn, dev);
     }
 
   if (iob == NULL)
@@ -292,7 +291,7 @@ ssize_t pkt_sendmsg(FAR struct socket *psock, FAR const 
struct msghdr *msg,
     }
 
   iob_reserve(iob, CONFIG_NET_LL_GUARDSIZE);
-  iob_update_pktlen(iob, 0, false);
+  iob_update_pktlen(iob, 0, true);
 
 #ifdef CONFIG_NET_TIMESTAMP
   if (_SO_GETOPT(conn->sconn.s_options, SO_TIMESTAMPING))
@@ -310,11 +309,8 @@ ssize_t pkt_sendmsg(FAR struct socket *psock, FAR const 
struct msghdr *msg,
       offset = -NET_LL_HDRLEN(dev);
     }
 
-  if (nonblock)
-    {
-      ret = iob_trycopyin(iob, buf, len, offset, false);
-    }
-  else
+  ret = iob_trycopyin(iob, buf, len, offset, true);
+  if (ret < 0 && !nonblock)
     {
       /* iob_copyin might wait for buffers to be freed, but if
        * network is locked this might never happen, since network
@@ -322,7 +318,7 @@ ssize_t pkt_sendmsg(FAR struct socket *psock, FAR const 
struct msghdr *msg,
        */
 
       conn_dev_unlock(&conn->sconn, dev);
-      ret = iob_copyin(iob, buf, len, offset, false);
+      ret = iob_copyin(iob, buf, len, offset, true);
       conn_dev_lock(&conn->sconn, dev);
     }
 
@@ -362,13 +358,12 @@ ssize_t pkt_sendmsg(FAR struct socket *psock, FAR const 
struct msghdr *msg,
       goto errout_with_iob;
     }
 
-  if (nonblock)
-    {
-      ret = iob_tryadd_queue(iob, &conn->write_q);
-    }
-  else
+  ret = iob_tryadd_queue(iob, &conn->write_q);
+  if (ret < 0 && !nonblock)
     {
+      conn_dev_unlock(&conn->sconn, dev);
       ret = iob_add_queue(iob, &conn->write_q);
+      conn_dev_lock(&conn->sconn, dev);
     }
 
   if (ret < 0)

Reply via email to