@@ -39,6 +39,24 @@ extern "C"{
39
39
#include < NetworkInterface.h>
40
40
#endif
41
41
42
+ #define TAG " AsyncTCP"
43
+
44
+ // https://github.com/espressif/arduino-esp32/issues/10526
45
+ #ifdef CONFIG_LWIP_TCPIP_CORE_LOCKING
46
+ #define TCP_MUTEX_LOCK () \
47
+ if (!sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER)) { \
48
+ LOCK_TCPIP_CORE (); \
49
+ }
50
+
51
+ #define TCP_MUTEX_UNLOCK () \
52
+ if (sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER)) { \
53
+ UNLOCK_TCPIP_CORE (); \
54
+ }
55
+ #else // CONFIG_LWIP_TCPIP_CORE_LOCKING
56
+ #define TCP_MUTEX_LOCK ()
57
+ #define TCP_MUTEX_UNLOCK ()
58
+ #endif // CONFIG_LWIP_TCPIP_CORE_LOCKING
59
+
42
60
#define INVALID_CLOSED_SLOT -1
43
61
44
62
/*
@@ -722,17 +740,20 @@ bool AsyncClient::_connect(ip_addr_t addr, uint16_t port){
722
740
return false ;
723
741
}
724
742
743
+ TCP_MUTEX_LOCK ();
725
744
tcp_pcb* pcb = tcp_new_ip_type (addr.type );
726
745
if (!pcb){
746
+ TCP_MUTEX_UNLOCK ();
727
747
log_e (" pcb == NULL" );
728
748
return false ;
729
749
}
730
-
731
750
tcp_arg (pcb, this );
732
751
tcp_err (pcb, &_tcp_error);
733
752
tcp_recv (pcb, &_tcp_recv);
734
753
tcp_sent (pcb, &_tcp_sent);
735
754
tcp_poll (pcb, &_tcp_poll, 1 );
755
+ TCP_MUTEX_UNLOCK ();
756
+
736
757
esp_err_t err =_tcp_connect (pcb, _closed_slot, &addr, port,(tcp_connected_fn)&_tcp_connected);
737
758
return err == ESP_OK;
738
759
}
@@ -863,11 +884,13 @@ int8_t AsyncClient::_close(){
863
884
// ets_printf("X: 0x%08x\n", (uint32_t)this);
864
885
int8_t err = ERR_OK;
865
886
if (_pcb) {
887
+ TCP_MUTEX_LOCK ();
866
888
tcp_arg (_pcb, NULL );
867
889
tcp_sent (_pcb, NULL );
868
890
tcp_recv (_pcb, NULL );
869
891
tcp_err (_pcb, NULL );
870
892
tcp_poll (_pcb, NULL , 0 );
893
+ TCP_MUTEX_UNLOCK ();
871
894
_tcp_clear_events (this );
872
895
err = _tcp_close (_pcb, _closed_slot);
873
896
if (err != ERR_OK) {
@@ -1461,7 +1484,9 @@ void AsyncServer::begin(){
1461
1484
return ;
1462
1485
}
1463
1486
int8_t err;
1487
+ TCP_MUTEX_LOCK ();
1464
1488
_pcb = tcp_new_ip_type (_bind4 && _bind6 ? IPADDR_TYPE_ANY : (_bind6 ? IPADDR_TYPE_V6 : IPADDR_TYPE_V4));
1489
+ TCP_MUTEX_UNLOCK ();
1465
1490
if (!_pcb){
1466
1491
log_e (" _pcb == NULL" );
1467
1492
return ;
@@ -1493,14 +1518,18 @@ void AsyncServer::begin(){
1493
1518
log_e (" listen_pcb == NULL" );
1494
1519
return ;
1495
1520
}
1521
+ TCP_MUTEX_LOCK ();
1496
1522
tcp_arg (_pcb, (void *) this );
1497
1523
tcp_accept (_pcb, &_s_accept);
1524
+ TCP_MUTEX_UNLOCK ();
1498
1525
}
1499
1526
1500
1527
void AsyncServer::end (){
1501
1528
if (_pcb){
1529
+ TCP_MUTEX_LOCK ();
1502
1530
tcp_arg (_pcb, NULL );
1503
1531
tcp_accept (_pcb, NULL );
1532
+ TCP_MUTEX_UNLOCK ();
1504
1533
if (tcp_close (_pcb) != ERR_OK){
1505
1534
_tcp_abort (_pcb, -1 );
1506
1535
}
0 commit comments