10000 Replaced usage of esp_yield() with a yield() loop in a number of places · junbowu/esp8266-arduino-threads@f037f92 · GitHub
[go: up one dir, main page]

Skip to content

Commit f037f92

Browse files
author
Björn Mellström
committed
Replaced usage of esp_yield() with a yield() loop in a number of places
1 parent 9623240 commit f037f92

File tree

4 files changed

+35
-48
lines changed

4 files changed

+35
-48
lines changed

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ extern "C" {
4545
#include "WiFiUdp.h"
4646
#include "debug.h"
4747

48-
extern "C" void esp_schedule();
49-
extern "C" void esp_yield();
50-
5148

5249
// -----------------------------------------------------------------------------------------------------------------------
5350
// ------------------------------------------------- Generic WiFi function -----------------------------------------------
@@ -426,7 +423,10 @@ void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback
426423
void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *callback_arg);
427424
#endif
428425

429-
static bool _dns_lookup_pending = false;
426+
427+
static uint32_t dnsLastKey;
428+
static volatile uint32_t dnsResult;
429+
static volatile uint32_t dnsKeyFinished;
430430

431431
/**
432432
* Resolve the given hostname to an IP address.
@@ -453,15 +453,18 @@ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResul
453453
}
454454

455455
DEBUG_WIFI_GENERIC("[hostByName] request IP for: %s\n", aHostname);
456-
err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult);
456+
uint32_t key = ++dnsLastKey; // Safe since we don't have thread preemption
457+
err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, (void*)key);
457458
if(err == ERR_OK) {
458459
aResult = addr.addr;
459460
} else if(err == ERR_INPROGRESS) {
460-
_dns_lookup_pending = true;
461-
delay(timeout_ms);
462-
_dns_lookup_pending = false;
463-
// will return here when dns_found_callback fires
464-
if(aResult != 0) {
461+
u32 start = millis();
462+
while ((dnsKeyFinished != key) && ((millis() - start) < timeout_ms)) {
463+
yield();
464+
}
465+
uint32_t result = dnsResult;
466+
if ((dnsKeyFinished == key) && (result != 0)) {
467+
aResult = result;
465468
err = ERR_OK;
466469
}
467470
}
@@ -488,13 +491,6 @@ void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *ca
488491
#endif
489492
{
490493
(void) name;
491-
if (!_dns_lookup_pending) {
492-
return;
493-
}
494-
if(ipaddr) {
495-
(*reinterpret_cast<IPAddress*>(callback_arg)) = ipaddr->addr;
496-
}
497-
esp_schedule(); // resume the hostByName function
494+
dnsResult = ipaddr->addr;
495+
dnsKeyFinished = (u32)callback_arg;
498496
}
499-
500-

libraries/ESP8266WiFi/src/ESP8266WiFiScan.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ extern "C" {
3737

3838
#include "debug.h"
3939

40-
extern "C" void esp_schedule();
41-
extern "C" void esp_yield();
42-
4340
// -----------------------------------------------------------------------------------------------------------------------
4441
// ---------------------------------------------------- Private functions ------------------------------------------------
4542
// -----------------------------------------------------------------------------------------------------------------------
@@ -96,7 +93,9 @@ int8_t ESP8266WiFiScanClass::scanNetworks(bool async, bool show_hidden) {
9693
return WIFI_SCAN_RUNNING;
9794
}
9895

99-
esp_yield();
96+
while(!ESP8266WiFiScanClass::_scanComplete) {
97+
yield();
98+
}
10099
return ESP8266WiFiScanClass::_scanCount;
101100
} else {
102101
return WIFI_SCAN_FAILED;
@@ -314,9 +313,7 @@ void ESP8266WiFiScanClass::_scanDone(void* result, int status) {
314313
ESP8266WiFiScanClass::_scanStarted = false;
315314
ESP8266WiFiScanClass::_scanComplete = true;
316315

317-
if(!ESP8266WiFiScanClass::_scanAsync) {
318-
esp_schedule();
319-
} else if (ESP8266WiFiScanClass::_onComplete) {
316+
if(ESP8266WiFiScanClass::_scanAsync && ESP8266WiFiScanClass::_onComplete) {
320317
ESP8266WiFiScanClass::_onComplete(ESP8266WiFiScanClass::_scanCount);
321318
ESP8266WiFiScanClass::_onComplete = nullptr;
322319
}

libraries/ESP8266WiFi/src/include/ClientContext.h

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ class WiFiClient;
2626

2727
typedef void (*discard_cb_t)(void*, ClientContext*);
2828

29-
extern "C" void esp_yield();
30-
extern "C" void esp_schedule();
31-
3229
#include "DataSource.h"
3330

3431
class ClientContext
@@ -126,8 +123,9 @@ class ClientContext
126123
}
127124
_connect_pending = 1;
128125
_op_start_time = millis();
129-
// This delay will be interrupted by esp_schedule in the connect callback
130-
delay(_timeout_ms);
126+
while (_connect_pending && !_is_timeout()) {
127+
yield();
128+
}
131129
_connect_pending = 0;
132130
if (state() != ESTABLISHED) {
133131
abort();
@@ -161,7 +159,7 @@ class ClientContext
161159
return tcp_nagle_disabled(_pcb);
162160
}
163161

164-
void setTimeout(int timeout_ms)
162+
void setTimeout(int timeout_ms)
165163
{
166164
_timeout_ms = timeout_ms;
167165
}
@@ -334,15 +332,14 @@ class ClientContext
334332

335333
void _notify_error()
336334
{
337-
if (_connect_pending || _send_waiting) {
338-
esp_schedule();
339-
}
335+
_connect_pending = 0;
336+
_send_pending = 0;
340337
}
341338

342339
size_t _write_from_source(DataSource* ds)
343340
{
344341
assert(_datasource == nullptr);
345-
assert(_send_waiting == 0);
342+
assert(_send_pending == 0);
346343
_datasource = ds;
347344
_written = 0;
348345
_op_start_time = millis();
@@ -360,10 +357,12 @@ class ClientContext
360357
break;
361358
}
362359

363-
++_send_waiting;
364-
esp_yield();
360+
_send_pending = 1;
361+
while (_send_pending && !_is_timeout()) {
362+
yield();
363+
}
365364
} while(true);
366-
_send_waiting = 0;
365+
_send_pending = 0;
367366
return _written;
368367
}
369368

@@ -409,10 +408,7 @@ class ClientContext
409408

410409
void _write_some_from_cb()
411410
{
412-
if (_send_waiting == 1) {
413-
_send_waiting--;
414-
esp_schedule();
415-
}
411+
_send_pending = 0;
416412
}
417413

418414
err_t _sent(tcp_pcb* pcb, uint16_t len)
@@ -489,7 +485,7 @@ class ClientContext
489485
(void) err;
490486
assert(pcb == _pcb);
491487
assert(_connect_pending);
492-
esp_schedule();
488+
_connect_pending = 0;
493489
return ERR_OK;
494490
}
495491

@@ -538,8 +534,8 @@ class ClientContext
538534
size_t _write_chunk_size = 256;
539535
uint32_t _timeout_ms = 5000;
540536
uint32_t _op_start_time = 0;
541-
uint8_t _send_waiting = 0;
542-
uint8_t _connect_pending = 0;
537+
volatile uint8_t _send_pending = 0;
538+
volatile uint8_t _connect_pending = 0;
543539

544540
int8_t _refcnt;
545541
ClientContext* _next;

libraries/ESP8266WiFi/src/include/UdpContext.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
class UdpContext;
2525

2626
extern "C" {
27-
void esp_yield();
28-
void esp_schedule();
2927
#include "lwip/init.h" // LWIP_VERSION_
3028
}
3129

0 commit comments

Comments
 (0)
0