pica/taskmanager/scheduler.h

53 lines
1.0 KiB
C++

//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>
#include "manager.h"
#include "function.h"
#include "record.h"
#include "utils/helpers.h"
namespace TM {
class Scheduler {
public:
using Delay = std::chrono::milliseconds;
using Time = Record::Time;
using Task = Record::Task;
Scheduler (std::weak_ptr<Manager> manager);
~Scheduler ();
void start ();
void stop ();
Record::ID schedule (const Task& task, Delay delay);
bool cancel (Record::ID id);
static const Record::ID none;
private:
void loop ();
private:
PriorityQueue<
Record,
std::vector<Record>,
std::greater<>
> queue;
std::set<Record::ID> scheduled;
std::weak_ptr<Manager> manager;
std::mutex mutex;
std::condition_variable cond;
std::unique_ptr<std::thread> thread;
bool running;
Record::ID idCounter;
};
}