task manager, license formatting

This commit is contained in:
Blue 2023-12-30 19:42:11 -03:00
parent fe2fbb9ad0
commit f1a2006b4b
Signed by: blue
GPG key ID: 9B203B252A63EE38
69 changed files with 380 additions and 122 deletions

View file

@ -0,0 +1,16 @@
#SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
#SPDX-License-Identifier: GPL-3.0-or-later
set(HEADERS
manager.h
job.h
route.h
)
set(SOURCES
manager.cpp
job.cpp
route.cpp
)
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})

9
taskmanager/job.cpp Normal file
View file

@ -0,0 +1,9 @@
//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#include "job.h"
TM::Job::Job ()
{}
TM::Job::~Job () {}

19
taskmanager/job.h Normal file
View file

@ -0,0 +1,19 @@
//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
namespace TM {
class Job {
public:
Job();
Job(const Job& other) = delete;
Job(Job&& other) = delete;
virtual ~Job();
Job& operator = (const Job& other) = delete;
Job& operator = (Job&& other) = delete;
virtual void execute() = 0;
};
}

69
taskmanager/manager.cpp Normal file
View file

@ -0,0 +1,69 @@
//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#include "manager.h"
TM::Manager::Manager ():
terminating(false),
threads(),
queue(),
mtx(),
cond()
{}
TM::Manager::~Manager () {
std::unique_lock lock(mtx);
if (threads.empty())
return;
lock.unlock();
stop();
}
void TM::Manager::start () {
std::lock_guard lock(mtx);
std::size_t amount = std::thread::hardware_concurrency();
for (std::size_t i = 0; i < amount; ++i)
threads.emplace_back(std::thread(&Manager::loop, this));
}
void TM::Manager::stop () {
std::unique_lock lock(mtx);
terminating = true;
lock.unlock();
cond.notify_all();
for (std::thread& thread : threads)
thread.join();
lock.lock();
threads.clear();
terminating = false;
}
void TM::Manager::loop () {
while (true) {
std::unique_lock lock(mtx);
while (!terminating && queue.empty())
cond.wait(lock);
if (terminating)
return;
std::unique_ptr<Job> job = std::move(queue.front());
queue.pop();
lock.unlock();
job->execute();
}
}
void TM::Manager::schedule (std::unique_ptr<Job> job) {
std::unique_lock lock(mtx);
queue.emplace(std::move(job));
lock.unlock();
cond.notify_one();
}

40
taskmanager/manager.h Normal file
View file

@ -0,0 +1,40 @@
//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <queue>
#include <vector>
#include <thread>
#include <memory>
#include <mutex>
#include <condition_variable>
#include "job.h"
namespace TM {
class Manager {
public:
Manager();
Manager(const Manager&) = delete;
Manager(Manager&&) = delete;
~Manager();
Manager& operator = (const Manager&) = delete;
Manager& operator = (Manager&&) = delete;
void start();
void stop();
void schedule(std::unique_ptr<Job> job);
private:
void loop();
private:
bool terminating;
std::vector<std::thread> threads;
std::queue<std::unique_ptr<Job>> queue;
std::mutex mtx;
std::condition_variable cond;
};
}

13
taskmanager/route.cpp Normal file
View file

@ -0,0 +1,13 @@
//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#include "route.h"
TM::Route::Route (std::shared_ptr<Router> router, std::unique_ptr<Request> request):
router(router),
request(std::move(request))
{}
void TM::Route::execute () {
router->route(std::move(request));
}

23
taskmanager/route.h Normal file
View file

@ -0,0 +1,23 @@
//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <memory>
#include "job.h"
#include "server/router.h"
#include "request/request.h"
namespace TM {
class Route : public Job {
public:
Route(std::shared_ptr<Router> router, std::unique_ptr<Request> request);
void execute () override;
private:
std::shared_ptr<Router> router;
std::unique_ptr<Request> request;
};
}