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

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

commit 6196550f8fe8cc49d38eb8b6743ec4747f16c0b8
Author: wangchen <[email protected]>
AuthorDate: Tue Sep 3 15:39:51 2024 +0800

    tcp:add net_lock to protect tcp resource
    
    add net_lock in the tcp_callback_cleanup & Set the devif_callback_s of
    tcp_callback_s to a two-dimensional pointer.
    
    Signed-off-by: wangchen <[email protected]>
---
 net/tcp/tcp.h                 | 2 +-
 net/tcp/tcp_callback.c        | 4 +++-
 net/tcp/tcp_connect.c         | 2 +-
 net/tcp/tcp_recvfrom.c        | 2 +-
 net/tcp/tcp_send_buffered.c   | 2 +-
 net/tcp/tcp_send_unbuffered.c | 2 +-
 6 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/net/tcp/tcp.h b/net/tcp/tcp.h
index 73ab8b4e457..e8ba598612a 100644
--- a/net/tcp/tcp.h
+++ b/net/tcp/tcp.h
@@ -442,7 +442,7 @@ struct tcp_backlog_s
 struct tcp_callback_s
 {
   FAR struct tcp_conn_s *tc_conn;
-  FAR struct devif_callback_s *tc_cb;
+  FAR struct devif_callback_s **tc_cb;
   FAR sem_t *tc_sem;
 };
 
diff --git a/net/tcp/tcp_callback.c b/net/tcp/tcp_callback.c
index 4890b3be392..8632d6fcc5b 100644
--- a/net/tcp/tcp_callback.c
+++ b/net/tcp/tcp_callback.c
@@ -437,8 +437,10 @@ void tcp_callback_cleanup(FAR void *arg)
 {
   FAR struct tcp_callback_s *cb = (FAR struct tcp_callback_s *)arg;
 
+  net_lock();
   nerr("ERROR: pthread is being canceled, need to cleanup cb\n");
-  tcp_callback_free(cb->tc_conn, cb->tc_cb);
+  tcp_callback_free(cb->tc_conn, *(cb->tc_cb));
   nxsem_destroy(cb->tc_sem);
+  net_unlock();
 }
 #endif /* NET_TCP_HAVE_STACK */
diff --git a/net/tcp/tcp_connect.c b/net/tcp/tcp_connect.c
index 750d3875b7d..888636b7349 100644
--- a/net/tcp/tcp_connect.c
+++ b/net/tcp/tcp_connect.c
@@ -375,7 +375,7 @@ int psock_tcp_connect(FAR struct socket *psock,
                */
 
               info.tc_conn = conn;
-              info.tc_cb = state.tc_cb;
+              info.tc_cb = &state.tc_cb;
               info.tc_sem = &state.tc_sem;
               tls_cleanup_push(tls_get_info(), tcp_callback_cleanup, &info);
 
diff --git a/net/tcp/tcp_recvfrom.c b/net/tcp/tcp_recvfrom.c
index fa5a2d82ebf..ab2a51fa5e6 100644
--- a/net/tcp/tcp_recvfrom.c
+++ b/net/tcp/tcp_recvfrom.c
@@ -803,7 +803,7 @@ static ssize_t tcp_recvfrom_one(FAR struct tcp_conn_s 
*conn, FAR void *buf,
            */
 
           info.tc_conn = conn;
-          info.tc_cb   = state.ir_cb;
+          info.tc_cb   = &state.ir_cb;
           info.tc_sem  = &state.ir_sem;
           tls_cleanup_push(tls_get_info(), tcp_callback_cleanup, &info);
 
diff --git a/net/tcp/tcp_send_buffered.c b/net/tcp/tcp_send_buffered.c
index bbf1fa6957f..49bc42a186d 100644
--- a/net/tcp/tcp_send_buffered.c
+++ b/net/tcp/tcp_send_buffered.c
@@ -1467,7 +1467,7 @@ ssize_t psock_tcp_send(FAR struct socket *psock, FAR 
const void *buf,
            */
 
           info.tc_conn = conn;
-          info.tc_cb   = conn->sndcb;
+          info.tc_cb   = &conn->sndcb;
           info.tc_sem  = &conn->snd_sem;
           tls_cleanup_push(tls_get_info(), tcp_callback_cleanup, &info);
 
diff --git a/net/tcp/tcp_send_unbuffered.c b/net/tcp/tcp_send_unbuffered.c
index b19d29e85d5..7d3078c9a71 100644
--- a/net/tcp/tcp_send_unbuffered.c
+++ b/net/tcp/tcp_send_unbuffered.c
@@ -611,7 +611,7 @@ ssize_t psock_tcp_send(FAR struct socket *psock,
                */
 
               info.tc_conn = conn;
-              info.tc_cb   = state.snd_cb;
+              info.tc_cb   = &state.snd_cb;
               info.tc_sem  = &state.snd_sem;
               tls_cleanup_push(tls_get_info(), tcp_callback_cleanup, &info);
 

Reply via email to