SatDump 2.0.0-alpha-76a915210
Loading...
Searching...
No Matches
task_scheduler.h
Go to the documentation of this file.
1#pragma once
2
7
8#include <condition_variable>
9#include <map>
10#include <mutex>
11#include <string>
12#include <thread>
13
14namespace satdump
15{
27 {
28 std::shared_ptr<void> evt = nullptr;
29 std::string evt_name;
30 time_t last_run = 0;
31 time_t run_interval = 0;
32 };
33
50 class TaskScheduler
51 {
52 private:
53 bool running = true;
54 bool needs_update = false;
55 std::map<std::string, ScheduledTask> scheduled_tasks;
56 std::thread task_thread;
57 std::mutex task_mtx;
58 std::condition_variable cv;
59
63 void thread_func();
64
65 public:
66 TaskScheduler();
67 ~TaskScheduler();
68
80 template <typename T>
81 void add_task(std::string task_name, std::shared_ptr<T> evt, time_t last_run, time_t run_interval)
82 {
83 // Add Task
84 {
85 std::lock_guard<std::mutex> lock(task_mtx);
86 scheduled_tasks[task_name] = {evt, typeid(T).name(), last_run, run_interval};
87 needs_update = true;
88 }
89
90 // Notify thread
91 cv.notify_one();
92 }
93
100 void del_task(std::string task_name)
101 {
102 // Delete Task
103 {
104 std::lock_guard<std::mutex> lock(task_mtx);
105 scheduled_tasks.erase(task_name);
106 needs_update = true;
107 }
108
109 // Notify thread
110 cv.notify_one();
111 }
112 };
113} // namespace satdump
void del_task(std::string task_name)
Remove a scheduled task. Does nothing if no task by the given name is registered.
Definition task_scheduler.h:100
void add_task(std::string task_name, std::shared_ptr< T > evt, time_t last_run, time_t run_interval)
Add a new scheduled task.
Definition task_scheduler.h:81
Struct holding a scheduled task.
Definition task_scheduler.h:27