pica/taskmanager/scheduler.h

49 lines
1.0 KiB
C
Raw Normal View History

2023-12-31 17:10:04 +00:00
//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <memory>
#include <thread>
#include <chrono>
#include <functional>
#include <mutex>
#include <condition_variable>
#include "manager.h"
2024-01-03 01:11:56 +00:00
#include "function.h"
2023-12-31 17:10:04 +00:00
#include "utils/helpers.h"
namespace TM {
class Scheduler {
public:
using Delay = std::chrono::milliseconds;
Scheduler (std::weak_ptr<Manager> manager);
~Scheduler ();
void start();
void stop();
2024-01-03 01:11:56 +00:00
void schedule(const std::function<void()>& task, Delay delay);
2023-12-31 17:10:04 +00:00
private:
void loop();
private:
2024-01-03 01:11:56 +00:00
using Task = std::unique_ptr<Function>;
2023-12-31 17:10:04 +00:00
using Time = std::chrono::time_point<std::chrono::steady_clock>;
using Record = std::pair<Time, Task>;
2024-01-03 01:11:56 +00:00
PriorityQueue<
2023-12-31 17:10:04 +00:00
Record,
std::vector<Record>,
FirstGreater<Record>
> queue;
2024-01-03 01:11:56 +00:00
std::weak_ptr<Manager> manager;
2023-12-31 17:10:04 +00:00
std::mutex mutex;
std::condition_variable cond;
std::unique_ptr<std::thread> thread;
bool running;
};
}