@@ -35,6 +35,22 @@ extern "C"{
35
35
* TCP/IP Event Task
36
36
* */
37
37
38
+ // https://github.com/espressif/arduino-esp32/issues/10526
39
+ #ifdef CONFIG_LWIP_TCPIP_CORE_LOCKING
40
+ #define TCP_MUTEX_LOCK () \
41
+ if (!sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER)) { \
42
+ LOCK_TCPIP_CORE (); \
43
+ }
44
+
45
+ #define TCP_MUTEX_UNLOCK () \
46
+ if (sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER)) { \
47
+ UNLOCK_TCPIP_CORE (); \
48
+ }
49
+ #else // CONFIG_LWIP_TCPIP_CORE_LOCKING
50
+ #define TCP_MUTEX_LOCK ()
51
+ #define TCP_MUTEX_UNLOCK ()
52
+ #endif // CONFIG_LWIP_TCPIP_CORE_LOCKING
53
+
38
54
typedef enum {
39
55
LWIP_TCP_SENT, LWIP_TCP_RECV, LWIP_TCP_FIN, LWIP_TCP_ERROR, LWIP_TCP_POLL, LWIP_TCP_CLEAR, LWIP_TCP_ACCEPT, LWIP_TCP_CONNECTED, LWIP_TCP_DNS
40
56
} lwip_event_t ;
@@ -688,8 +704,10 @@ bool AsyncClient::connect(IPAddress ip, uint16_t port){
688
704
addr.type = IPADDR_TYPE_V4;
689
705
addr.u_addr .ip4 .addr = ip;
690
706
707
+ TCP_MUTEX_LOCK ();
691
708
tcp_pcb* pcb = tcp_new_ip_type (IPADDR_TYPE_V4);
692
709
if (!pcb){
710
+ TCP_MUTEX_UNLOCK ();
693
711
log_e (" pcb == NULL" );
694
712
return false ;
695
713
}
@@ -699,6 +717,7 @@ bool AsyncClient::connect(IPAddress ip, uint16_t port){
699
717
tcp_recv (pcb, &_tcp_recv);
700
718
tcp_sent (pcb, &_tcp_sent);
701
719
tcp_poll (pcb, &_tcp_poll, 1 );
720
+ TCP_MUTEX_UNLOCK ();
702
721
// _tcp_connect(pcb, &addr, port,(tcp_connected_fn)&_s_connected);
703
722
esp_err_t err = _tcp_connect (pcb, _closed_slot, &addr, port,(tcp_connected_fn)&_tcp_connected);
704
723
return err == ESP_OK;
@@ -711,8 +730,9 @@ bool AsyncClient::connect(const char* host, uint16_t port){
711
730
log_e (" failed to start task" );
712
731
return false ;
713
732
}
714
-
733
+ TCP_MUTEX_LOCK ();
715
734
err_t err = dns_gethostbyname (host, &addr, (dns_found_callback)&_tcp_dns_found, this );
735
+ TCP_MUTEX_UNLOCK ();
716
736
if (err == ERR_OK) {
717
737
return connect (IPAddress (addr.u_addr .ip4 .addr ), port);
718
738
} else if (err == ERR_INPROGRESS) {
@@ -800,11 +820,13 @@ int8_t AsyncClient::_close(){
800
820
int8_t err = ERR_OK;
801
821
if (_pcb) {
802
822
// log_i("");
823
+ TCP_MUTEX_LOCK ();
803
824
tcp_arg (_pcb, NULL );
804
825
tcp_sent (_pcb, NULL );
805
826
tcp_recv (_pcb, NULL );
806
827
tcp_err (_pcb, NULL );
807
828
tcp_poll (_pcb, NULL , 0 );
829
+ TCP_MUTEX_UNLOCK ();
808
830
_tcp_clear_events (this );
809
831
err = _tcp_close (_pcb, _closed_slot);
810
832
if (err != ERR_OK) {
@@ -1271,7 +1293,9 @@ void AsyncServer::begin(){
1271
1293
return ;
1272
1294
}
1273
1295
int8_t err;
1296
+ TCP_MUTEX_LOCK ();
1274
1297
_pcb = tcp_new_ip_type (IPADDR_TYPE_V4);
1298
+ TCP_MUTEX_UNLOCK ();
1275
1299
if (!_pcb){
1276
1300
log_e (" _pcb == NULL" );
1277
1301
return ;
@@ -1294,14 +1318,18 @@ void AsyncServer::begin(){
1294
1318
log_e (" listen_pcb == NULL" );
1295
1319
return ;
1296
1320
}
1321
+ TCP_MUTEX_LOCK ();
1297
1322
tcp_arg (_pcb, (void *) this );
1298
1323
tcp_accept (_pcb, &_s_accept);
1324
+ TCP_MUTEX_UNLOCK ();
1299
1325
}
1300
1326
1301
1327
void AsyncServer::end (){
1302
1328
if (_pcb){
1329
+ TCP_MUTEX_LOCK ();
1303
1330
tcp_arg (_pcb, NULL );
1304
1331
tcp_accept (_pcb, NULL );
1332
+ TCP_MUTEX_UNLOCK ();
1305
1333
if (tcp_close (_pcb) != ERR_OK){
1306
1334
_tcp_abort (_pcb, -1 );
1307
1335
}
0 commit comments