Skip to content

Commit

Permalink
ESP32 implementation of OnBackgroundThread()
Browse files Browse the repository at this point in the history
  • Loading branch information
snej committed Oct 23, 2023
1 parent 1397c24 commit 31958ac
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/io/esp32/ESPEventLoop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//

#include "EventLoop.hh"
#include "Future.hh"
#include "util/Logging.hh"
#include "Task.hh"

Expand All @@ -26,6 +27,7 @@

#include <cmath>
#include <functional>
#include <thread>

namespace crouton {
using namespace std;
Expand All @@ -36,7 +38,7 @@ namespace crouton {
static constexpr size_t kQueueLength = 16;

/** The struct that goes in the FreeRTOS event queue.
Can't use any fancy C++ RIAA stuff because these get memcpy'd by FreeRTOS. */
Can't use any fancy C++ RAII stuff because these structs get memcpy'd by FreeRTOS. */
struct Event {
enum Type : uint8_t {
Interrupt,
Expand Down Expand Up @@ -212,7 +214,28 @@ namespace crouton {


Future<void> OnBackgroundThread(std::function<void()> fn) {
Error(CroutonError::Unimplemented).raise(); // TODO
static Scheduler* sBGScheduler;
static std::thread bgThread([] {
sBGScheduler = &Scheduler::current();
while (true)
sBGScheduler->run();
});

while (!sBGScheduler)
vTaskDelay(10);

auto provider = Future<void>::provider();
sBGScheduler->onEventLoop([provider, fn] {
try {
fn();
provider->setResult();
} catch (std::exception const& x) {
LLoop->error("*** Caught unexpected exception in OnBackgroundThread fn: {} ***",
x.what());
provider->setError(Error(x));
}
});
return Future(provider);
}

}

0 comments on commit 31958ac

Please sign in to comment.