1
0
forked from blue/pica
pica/taskmanager/scheduler.h

53 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 <mutex>
#include <condition_variable>
#include <set>
2023-12-31 17:10:04 +00:00
#include "manager.h"
2024-01-03 01:11:56 +00:00
#include "function.h"
#include "record.h"
2023-12-31 17:10:04 +00:00
#include "utils/helpers.h"
namespace TM {
class Scheduler {
public:
using Delay = std::chrono::milliseconds;
using Time = Record::Time;
using Task = Record::Task;
2023-12-31 17:10:04 +00:00
Scheduler (std::weak_ptr<Manager> manager);
~Scheduler ();
void start ();
void stop ();
Record::ID schedule (const Task& task, Delay delay);
bool cancel (Record::ID id);
2023-12-31 17:10:04 +00:00
static const Record::ID none;
2023-12-31 17:10:04 +00:00
private:
void loop ();
2023-12-31 17:10:04 +00:00
private:
2024-01-03 01:11:56 +00:00
PriorityQueue<
2023-12-31 17:10:04 +00:00
Record,
std::vector<Record>,
std::greater<>
2023-12-31 17:10:04 +00:00
> queue;
std::set<Record::ID> scheduled;
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;
Record::ID idCounter;
2023-12-31 17:10:04 +00:00
};
}