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

Commit ca0ead0

Browse files
authored
Add mutex locking required by ESP32 v3.1.0 (#189)
1 parent 17039c3 commit ca0ead0

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/AsyncTCP.cpp

+29-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ extern "C"{
3535
* TCP/IP Event Task
3636
* */
3737

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+
3854
typedef enum {
3955
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
4056
} lwip_event_t;
@@ -688,8 +704,10 @@ bool AsyncClient::connect(IPAddress ip, uint16_t port){
688704
addr.type = IPADDR_TYPE_V4;
689705
addr.u_addr.ip4.addr = ip;
690706

707+
TCP_MUTEX_LOCK();
691708
tcp_pcb* pcb = tcp_new_ip_type(IPADDR_TYPE_V4);
692709
if (!pcb){
710+
TCP_MUTEX_UNLOCK();
693711
log_e("pcb == NULL");
694712
return false;
695713
}
@@ -699,6 +717,7 @@ bool AsyncClient::connect(IPAddress ip, uint16_t port){
699717
tcp_recv(pcb, &_tcp_recv);
700718
tcp_sent(pcb, &_tcp_sent);
701719
tcp_poll(pcb, &_tcp_poll, 1);
720+
TCP_MUTEX_UNLOCK();
702721
//_tcp_connect(pcb, &addr, port,(tcp_connected_fn)&_s_connected);
703722
esp_err_t err = _tcp_connect(pcb, _closed_slot, &addr, port,(tcp_connected_fn)&_tcp_connected);
704723
return err == ESP_OK;
@@ -711,8 +730,9 @@ bool AsyncClient::connect(const char* host, uint16_t port){
711730
log_e("failed to start task");
712731
return false;
713732
}
714-
733+
TCP_MUTEX_LOCK();
715734
err_t err = dns_gethostbyname(host, &addr, (dns_found_callback)&_tcp_dns_found, this);
735+
TCP_MUTEX_UNLOCK();
716736
if(err == ERR_OK) {
717737
return connect(IPAddress(addr.u_addr.ip4.addr), port);
718738
} else if(err == ERR_INPROGRESS) {
@@ -800,11 +820,13 @@ int8_t AsyncClient::_close(){
800820
int8_t err = ERR_OK;
801821
if(_pcb) {
802822
//log_i("");
823+
TCP_MUTEX_LOCK();
803824
tcp_arg(_pcb, NULL);
804825
tcp_sent(_pcb, NULL);
805826
tcp_recv(_pcb, NULL);
806827
tcp_err(_pcb, NULL);
807828
tcp_poll(_pcb, NULL, 0);
829+
TCP_MUTEX_UNLOCK();
808830
_tcp_clear_events(this);
809831
err = _tcp_close(_pcb, _closed_slot);
810832
if(err != ERR_OK) {
@@ -1271,7 +1293,9 @@ void AsyncServer::begin(){
12711293
return;
12721294
}
12731295
int8_t err;
1296+
TCP_MUTEX_LOCK();
12741297
_pcb = tcp_new_ip_type(IPADDR_TYPE_V4);
1298+
TCP_MUTEX_UNLOCK();
12751299
if (!_pcb){
12761300
log_e("_pcb == NULL");
12771301
return;
@@ -1294,14 +1318,18 @@ void AsyncServer::begin(){
12941318
log_e("listen_pcb == NULL");
12951319
return;
12961320
}
1321+
TCP_MUTEX_LOCK();
12971322
tcp_arg(_pcb, (void*) this);
12981323
tcp_accept(_pcb, &_s_accept);
1324+
TCP_MUTEX_UNLOCK();
12991325
}
13001326

13011327
void AsyncServer::end(){
13021328
if(_pcb){
1329+
TCP_MUTEX_LOCK();
13031330
tcp_arg(_pcb, NULL);
13041331
tcp_accept(_pcb, NULL);
1332+
TCP_MUTEX_UNLOCK();
13051333
if(tcp_close(_pcb) != ERR_OK){
13061334
_tcp_abort(_pcb, -1);
13071335
}

0 commit comments

Comments
 (0)