summaryrefslogtreecommitdiff
path: root/src/database/env_esp.cpp
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-04-27 12:55:30 +1000
committerjacqueline <me@jacqueline.id.au>2023-04-27 12:55:30 +1000
commit5d7cbec34cd5e473d5768b39054d99bc72ddad62 (patch)
treef83040e527dc025345b3786a298f11e914b5eb37 /src/database/env_esp.cpp
parentfbe047a35fff100cb5f42d10984bccde137f586e (diff)
downloadtangara-fw-5d7cbec34cd5e473d5768b39054d99bc72ddad62.tar.gz
Move DB interactions to a background thread
Diffstat (limited to 'src/database/env_esp.cpp')
-rw-r--r--src/database/env_esp.cpp39
1 files changed, 6 insertions, 33 deletions
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 <cstdio>
#include <cstdlib>
#include <cstring>
+#include <functional>
#include <limits>
#include <mutex>
#include <queue>
@@ -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<TaskFunction_t>(BackgroundThreadEntryPoint),
- "LVL_ONEOFF", 16 * 1024, reinterpret_cast<void*>(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<BackgroundWorkItem*>(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