Files
xiaozhi-esp32/main/background_task.h

31 lines
678 B
C
Raw Normal View History

2024-11-29 11:06:05 +08:00
#ifndef BACKGROUND_TASK_H
#define BACKGROUND_TASK_H
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <mutex>
#include <list>
#include <condition_variable>
#include <atomic>
class BackgroundTask {
public:
BackgroundTask(uint32_t stack_size = 4096 * 2);
~BackgroundTask();
2025-06-29 05:40:27 +08:00
bool Schedule(std::function<void()> callback);
2024-11-29 11:06:05 +08:00
void WaitForCompletion();
private:
std::mutex mutex_;
std::list<std::function<void()>> background_tasks_;
2024-11-29 11:06:05 +08:00
std::condition_variable condition_variable_;
TaskHandle_t background_task_handle_ = nullptr;
2025-06-29 05:40:27 +08:00
int active_tasks_ = 0;
int waiting_for_completion_ = 0;
2024-11-29 11:06:05 +08:00
void BackgroundTaskLoop();
};
#endif