From 5d7cbec34cd5e473d5768b39054d99bc72ddad62 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 27 Apr 2023 12:55:30 +1000 Subject: Move DB interactions to a background thread --- src/database/env_esp.cpp | 39 ++++++--------------------------------- 1 file changed, 6 insertions(+), 33 deletions(-) (limited to 'src/database/env_esp.cpp') diff --git a/src/database/env_esp.cpp b/src/database/env_esp.cpp index 363421a7..71d4fcea 100644 --- a/src/database/env_esp.cpp +++ b/src/database/env_esp.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -28,6 +29,8 @@ #include "leveldb/slice.h" #include "leveldb/status.h" +#include "db_task.hpp" + namespace leveldb { std::string ErrToStr(FRESULT err) { @@ -447,43 +450,13 @@ void EspEnv::SleepForMicroseconds(int micros) { vTaskDelay(pdMS_TO_TICKS(micros / 1000)); } -extern "C" void BackgroundThreadEntryPoint(EspEnv* env) { - env->BackgroundThreadMain(); -} - -EspEnv::EspEnv() - : background_work_mutex_(), - started_background_thread_(false), - background_work_queue_(xQueueCreate(4, sizeof(BackgroundWorkItem))) {} +EspEnv::EspEnv() {} void EspEnv::Schedule( void (*background_work_function)(void* background_work_arg), void* background_work_arg) { - background_work_mutex_.lock(); - - // Start the background thread, if we haven't done so already. - if (!started_background_thread_) { - started_background_thread_ = true; - xTaskCreate(reinterpret_cast(BackgroundThreadEntryPoint), - "LVL_ONEOFF", 16 * 1024, reinterpret_cast(this), 3, NULL); - } - - BackgroundWorkItem item(background_work_function, background_work_arg); - xQueueSend(background_work_queue_, &item, portMAX_DELAY); - - background_work_mutex_.unlock(); -} - -void EspEnv::BackgroundThreadMain() { - while (true) { - uint8_t buf[sizeof(BackgroundWorkItem)]; - if (xQueueReceive(background_work_queue_, &buf, portMAX_DELAY)) { - BackgroundWorkItem* item = reinterpret_cast(buf); - auto background_work_function = item->function; - void* background_work_arg = item->arg; - background_work_function(background_work_arg); - } - } + database::SendToDbTask( + [=]() { std::invoke(background_work_function, background_work_arg); }); } } // namespace leveldb -- cgit v1.2.3