Skip to content
This repository was archived by the owner on Jan 21, 2025. It is now read-only.

Commit bad4972

Browse files
committed
Fix #27: Required to lock TCPIP core functionality
Ref: - #27 - espressif/arduino-esp32#10526
1 parent f005448 commit bad4972

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/AsyncTCP.cpp

+30-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,24 @@ extern "C"{
3939
#include <NetworkInterface.h>
4040
#endif
4141

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+
4260
#define INVALID_CLOSED_SLOT -1
4361

4462
/*
@@ -722,17 +740,20 @@ bool AsyncClient::_connect(ip_addr_t addr, uint16_t port){
722740
return false;
723741
}
724742

743+
TCP_MUTEX_LOCK();
725744
tcp_pcb* pcb = tcp_new_ip_type(addr.type);
726745
if (!pcb){
746+
TCP_MUTEX_UNLOCK();
727747
log_e("pcb == NULL");
728748
return false;
729749
}
730-
731750
tcp_arg(pcb, this);
732751
tcp_err(pcb, &_tcp_error);
733752
tcp_recv(pcb, &_tcp_recv);
734753
tcp_sent(pcb, &_tcp_sent);
735754
tcp_poll(pcb, &_tcp_poll, 1);
755+
TCP_MUTEX_UNLOCK();
756+
736757
esp_err_t err =_tcp_connect(pcb, _closed_slot, &addr, port,(tcp_connected_fn)&_tcp_connected);
737758
return err == ESP_OK;
738759
}
@@ -863,11 +884,13 @@ int8_t AsyncClient::_close(){
863884
//ets_printf("X: 0x%08x\n", (uint32_t)this);
864885
int8_t err = ERR_OK;
865886
if(_pcb) {
887+
TCP_MUTEX_LOCK();
866888
tcp_arg(_pcb, NULL);
867889
tcp_sent(_pcb, NULL);
868890
tcp_recv(_pcb, NULL);
869891
tcp_err(_pcb, NULL);
870892
tcp_poll(_pcb, NULL, 0);
893+
TCP_MUTEX_UNLOCK();
871894
_tcp_clear_events(this);
872895
err = _tcp_close(_pcb, _closed_slot);
873896
if(err != ERR_OK) {
@@ -1461,7 +1484,9 @@ void AsyncServer::begin(){
14611484
return;
14621485
}
14631486
int8_t err;
1487+
TCP_MUTEX_LOCK();
14641488
_pcb = tcp_new_ip_type(_bind4 && _bind6 ? IPADDR_TYPE_ANY : (_bind6 ? IPADDR_TYPE_V6 : IPADDR_TYPE_V4));
1489+
TCP_MUTEX_UNLOCK();
14651490
if (!_pcb){
14661491
log_e("_pcb == NULL");
14671492
return;
@@ -1493,14 +1518,18 @@ void AsyncServer::begin(){
14931518
log_e("listen_pcb == NULL");
14941519
return;
14951520
}
1521+
TCP_MUTEX_LOCK();
14961522
tcp_arg(_pcb, (void*) this);
14971523
tcp_accept(_pcb, &_s_accept);
1524+
TCP_MUTEX_UNLOCK();
14981525
}
14991526

15001527
void AsyncServer::end(){
15011528
if(_pcb){
1529+
TCP_MUTEX_LOCK();
15021530
tcp_arg(_pcb, NULL);
15031531
tcp_accept(_pcb, NULL);
1532+
TCP_MUTEX_UNLOCK();
15041533
if(tcp_close(_pcb) != ERR_OK){
15051534
_tcp_abort(_pcb, -1);
15061535
}

0 commit comments

Comments
 (0)