Skip to content

Commit

Permalink
Initialise message queue but don't activate until init complete
Browse files Browse the repository at this point in the history
Can deadlock if used inside `init()` as it's called in low-priority thread.
  • Loading branch information
mikee47 committed Nov 23, 2024
1 parent 815869b commit ef31d29
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
3 changes: 3 additions & 0 deletions Sming/Arch/Esp32/Components/esp32/src/startup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

extern void init();
extern void esp_network_initialise();
extern void start_sming_task_loop();

extern "C" void __wrap_esp_newlib_init_global_stdio(const char*)
{
Expand All @@ -43,4 +44,6 @@ extern "C" void app_main(void)
// Application gets called outside main thread at startup
// Things like smartconfig won't work if called via task queue
init();

start_sming_task_loop();
}
26 changes: 13 additions & 13 deletions Sming/Arch/Esp32/Components/esp32/src/tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,28 @@ bool system_os_task(os_task_t callback, uint8_t prio, os_event_t* queue, uint8_t
return false;
}

taskCallback = callback;

return true;
}

void start_sming_task_loop()
{
#ifdef DISABLE_NETWORK

#if defined(SOC_ESP32) && !CONFIG_FREERTOS_UNICORE
constexpr unsigned core_id{1};
#else
constexpr unsigned core_id{0};
#endif
auto res = xTaskCreatePinnedToCore(sming_task_loop, "Sming", CONFIG_LWIP_TCPIP_TASK_STACK_SIZE, nullptr,
CONFIG_LWIP_TCPIP_TASK_PRIO, nullptr, core_id);
if(res != pdTRUE) {
return false;
}
xTaskCreatePinnedToCore(sming_task_loop, "Sming", CONFIG_LWIP_TCPIP_TASK_STACK_SIZE, nullptr,
CONFIG_LWIP_TCPIP_TASK_PRIO, nullptr, core_id);

#else

callbackMessage = tcpip_callbackmsg_new(tcpip_callback_fn(tcpip_message_handler), nullptr);

if(callbackMessage == nullptr) {
return false;
}

#endif

taskCallback = callback;

return true;
}

bool IRAM_ATTR system_os_post(uint8_t prio, os_signal_t sig, os_param_t par)
Expand All @@ -102,6 +98,10 @@ bool IRAM_ATTR system_os_post(uint8_t prio, os_signal_t sig, os_param_t par)
}

#ifndef DISABLE_NETWORK
if(!callbackMessage) {
// Message loop not yet active
return true;
}
// If queue isn't empty and we haven't already asked for a tcpip callback, do that now
if(xQueueIsQueueEmptyFromISR(eventQueue) == pdFALSE && !eventQueueFlag) {
eventQueueFlag = true;
Expand Down

0 comments on commit ef31d29

Please sign in to comment.