Move RP2040 wakeable_delay to wake.cpp (file-scope state for alarm callback)

This commit is contained in:
J. Nick Koston
2026-04-04 11:49:22 -10:00
parent 113edd2076
commit ac95bde627
2 changed files with 41 additions and 28 deletions
+39
View File
@@ -30,6 +30,45 @@ volatile bool g_main_loop_woke = false;
void IRAM_ATTR wake_loop_any_context() { wake_loop_impl_(); }
#endif
// === RP2040 — wakeable_delay (needs file-scope state for alarm callback) ===
#ifdef USE_RP2040
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static volatile bool s_delay_expired = false;
static int64_t alarm_callback_(alarm_id_t id, void *user_data) {
(void) id;
(void) user_data;
s_delay_expired = true;
__sev();
return 0;
}
namespace internal {
void wakeable_delay(uint32_t ms) {
if (ms == 0) {
yield();
return;
}
if (g_main_loop_woke) {
g_main_loop_woke = false;
return;
}
s_delay_expired = false;
alarm_id_t alarm = add_alarm_in_ms(ms, alarm_callback_, nullptr, true);
if (alarm <= 0) {
delay(ms);
return;
}
while (!g_main_loop_woke && !s_delay_expired) {
__wfe();
}
if (!s_delay_expired)
cancel_alarm(alarm);
g_main_loop_woke = false;
}
} // namespace internal
#endif // USE_RP2040
// === Host (UDP loopback socket) ===
#ifdef USE_HOST
void wake_loop_threadsafe() {
+2 -28
View File
@@ -89,35 +89,9 @@ inline void wake_loop_threadsafe() {
__sev();
}
/// RP2040 wakeable delay uses file-scope state (alarm callback + flag) — defined in wake.cpp.
namespace internal {
inline void wakeable_delay(uint32_t ms) {
static volatile bool s_delay_expired = false;
if (ms == 0) {
yield();
return;
}
if (g_main_loop_woke) {
g_main_loop_woke = false;
return;
}
s_delay_expired = false;
auto alarm_cb = [](alarm_id_t, void *) -> int64_t {
s_delay_expired = true;
__sev();
return 0;
};
alarm_id_t alarm = add_alarm_in_ms(ms, alarm_cb, nullptr, true);
if (alarm <= 0) {
delay(ms);
return;
}
while (!g_main_loop_woke && !s_delay_expired) {
__wfe();
}
if (!s_delay_expired)
cancel_alarm(alarm);
g_main_loop_woke = false;
}
void wakeable_delay(uint32_t ms);
} // namespace internal
// === Host (UDP loopback socket) ===