background_task.h 650 B

1234567891011121314151617181920212223242526272829
  1. #ifndef BACKGROUND_TASK_H
  2. #define BACKGROUND_TASK_H
  3. #include <freertos/FreeRTOS.h>
  4. #include <freertos/task.h>
  5. #include <mutex>
  6. #include <list>
  7. #include <condition_variable>
  8. #include <atomic>
  9. class BackgroundTask {
  10. public:
  11. BackgroundTask(uint32_t stack_size = 4096 * 2);
  12. ~BackgroundTask();
  13. void Schedule(std::function<void()> callback);
  14. void WaitForCompletion();
  15. private:
  16. std::mutex mutex_;
  17. std::list<std::function<void()>> main_tasks_;
  18. std::condition_variable condition_variable_;
  19. TaskHandle_t background_task_handle_ = nullptr;
  20. std::atomic<size_t> active_tasks_{0};
  21. void BackgroundTaskLoop();
  22. };
  23. #endif