Interaction with a thread from ISR using C++ Standard Library on ESP32

55 views Asked by At

How can I create or notify a thread (or similar) from within an ISR function?

Constraints

  • Using C++17
  • Using the C++ Standard Library () implementation (provided by Arduino-ESP32).
  • The mechanism must be functioning, when simulating with Wokwi. This is crucial, as it is the base for testing without hardware.
  • But with no further dependencies, if possible.

The software architecture aims to rely on the least possible platform specific dependencies. Vanilla C++17 and std are accepted dependencies.

Background

I want to define and use a concurrent running timer whose execution (start/reset) is controlled by the ISR.

I have tried to create a std::thread or notify a waiting one with std::condition_variable. Both occasionally lead to deadlocks when the hardware triggers the interrupt. When the program is executed in Wokwi it happens every time. Thus I assume the button bouncing (thus ISR call) is more frequent in the simulator.

Here is an online simulation of the problem. Push the button to trigger the ISR and observe the backtrace in the serial terminal.

Debugging in VS Code: screenshot of the call stack in VS Code when program runs into abort

I assume some (or all) concurrency methods which require some kind of synchronization (lock) are likely to deadlock if used within an ISR?

  • I found a comment which suggests that one can not use locks within ISRs
  • Calling std functions may be a problem as I can not know if their code will be copied into RAM (IRAM_ATTR)
  • If an ISR is called with highest priority and the synchronization mechanism needs to wait for a lock, a deadlock will occur (is it priority inversion?).

Summary

How can the interaction with a concurrently running thread (timer) be achieved from within an ISR?

  • Is it correct that I can not reliably use synchronization mechanisms from std within an ISR for this?
  • What are the alternatives (preferably independent of ESP/Espressif or additional dependencies like using FreeRTOS)?
0

There are 0 answers