Remove socket_wake/socket_delay: callers use wake_loop_any_context/wakeable_delay directly

This commit is contained in:
J. Nick Koston
2026-04-04 12:56:13 -10:00
parent e8a8add6c3
commit 619370170c
4 changed files with 7 additions and 39 deletions
@@ -262,7 +262,7 @@ void ESPHomeOTAComponent::handle_data_() {
/// BSD sockets (ESP32): setblocking(true) makes read/write block
/// lwip sockets (LT): setblocking(true) makes read/write block
/// Raw TCP (8266, RP2040): setblocking is no-op; SO_RCVTIMEO uses
/// socket_delay()/socket_wake() in read();
/// wakeable_delay() in read();
/// write() always returns immediately
ota::OTAResponseTypes error_code = ota::OTA_RESPONSE_ERROR_UNKNOWN;
bool update_started = false;
@@ -20,20 +20,6 @@
namespace esphome::socket {
#if defined(USE_ESP8266) || defined(USE_RP2040)
// socket_delay() and socket_wake() delegate to the core wake mechanism.
// socket_wake() calls wake_loop_any_context() which sets g_main_loop_woke,
// and socket_delay() calls wakeable_delay() which checks that flag.
void socket_delay(uint32_t ms) { esphome::internal::wakeable_delay(ms); }
#ifdef USE_ESP8266
void IRAM_ATTR socket_wake() { esphome::wake_loop_impl(); }
#else
void socket_wake() { esphome::wake_loop_any_context(); }
#endif
#endif
// ---- LWIP thread safety ----
//
// On RP2040 (Pico W), arduino-pico sets PICO_CYW43_ARCH_THREADSAFE_BACKGROUND=1.
@@ -462,10 +448,8 @@ err_t LWIPRawImpl::recv_fn(struct pbuf *pb, err_t err) {
} else {
pbuf_cat(this->rx_buf_, pb);
}
#if (defined(USE_ESP8266) || defined(USE_RP2040))
// Wake the main loop immediately so it can process the received data.
socket_wake();
#endif
esphome::wake_loop_any_context();
return ERR_OK;
}
@@ -474,15 +458,15 @@ void LWIPRawImpl::wait_for_data_() {
// (needs async_context lock).
//
// Loop until data arrives, connection closes, or the full timeout elapses.
// socket_delay() may return early due to other sockets waking the global
// socket_wake() flag, so we re-enter for the remaining time.
// wakeable_delay() may return early due to any wake source,
// so we re-enter for the remaining time.
uint32_t timeout_ms = this->recv_timeout_cs_ * 10;
uint32_t start = millis();
while (this->waiting_for_data_()) {
uint32_t elapsed = millis() - start;
if (elapsed >= timeout_ms)
break;
socket_delay(timeout_ms - elapsed);
esphome::internal::wakeable_delay(timeout_ms - elapsed);
}
}
@@ -870,10 +854,8 @@ err_t LWIPRawListenImpl::accept_fn_(struct tcp_pcb *newpcb, err_t err) {
tcp_err(newpcb, LWIPRawListenImpl::s_queued_err_fn);
tcp_recv(newpcb, LWIPRawListenImpl::s_queued_recv_fn);
LWIP_LOG("Accepted connection, queue size: %d", this->accepted_socket_count_);
#if (defined(USE_ESP8266) || defined(USE_RP2040))
// Wake the main loop immediately so it can accept the new connection.
socket_wake();
#endif
esphome::wake_loop_any_context();
return ERR_OK;
}
@@ -109,7 +109,7 @@ class LWIPRawImpl : public LWIPRawCommon {
return -1;
}
// Raw TCP doesn't use a blocking flag directly. Blocking behavior
// is provided by SO_RCVTIMEO which makes read() wait via socket_delay().
// is provided by SO_RCVTIMEO which makes read() wait via wakeable_delay().
return 0;
}
int loop() { return 0; }
-14
View File
@@ -120,19 +120,5 @@ socklen_t set_sockaddr_any(struct sockaddr *addr, socklen_t addrlen, uint16_t po
/// Format sockaddr into caller-provided buffer, returns length written (excluding null)
size_t format_sockaddr_to(const struct sockaddr *addr_ptr, socklen_t len, std::span<char, SOCKADDR_STR_LEN> buf);
#if (defined(USE_ESP8266) || defined(USE_RP2040)) && defined(USE_SOCKET_IMPL_LWIP_TCP)
/// Delay that can be woken early by socket activity.
/// On ESP8266, uses esp_delay() with a callback that checks socket activity.
/// On RP2040, uses __wfe() (Wait For Event) to truly sleep until an interrupt
/// (for example, CYW43 GPIO or a timer alarm) fires and wakes the CPU.
void socket_delay(uint32_t ms); // NOLINT(readability-redundant-declaration)
/// Signal socket/IO activity and wake the main loop early.
/// On ESP8266: sets flag + esp_schedule().
/// On RP2040: sets flag + __sev() (Send Event) to wake from __wfe().
/// ISR-safe on both platforms.
void socket_wake(); // NOLINT(readability-redundant-declaration)
#endif
} // namespace esphome::socket
#endif