forked from blue/mlc
1
0
Fork 0
mlc/src/taskmanager.h

53 lines
1.3 KiB
C
Raw Normal View History

2023-07-21 21:32:24 +00:00
#pragma once
#include <mutex>
#include <condition_variable>
#include <thread>
#include <functional>
2023-10-07 22:36:20 +00:00
#include <filesystem>
2023-07-21 21:32:24 +00:00
#include <vector>
2023-07-23 12:04:26 +00:00
#include <list>
2023-07-21 21:32:24 +00:00
#include <queue>
#include <string>
#include <atomic>
2023-07-23 12:04:26 +00:00
#include <iostream>
#include <array>
2023-10-07 22:36:20 +00:00
#include <memory>
2023-07-23 12:04:26 +00:00
2023-10-07 22:36:20 +00:00
#include "settings.h"
#include "logger/printer.h"
2023-07-21 21:32:24 +00:00
class TaskManager {
typedef std::pair<bool, std::list<Logger::Message>> JobResult;
2023-07-21 21:32:24 +00:00
public:
TaskManager(const std::shared_ptr<Settings>& settings, const std::shared_ptr<Printer>& logger);
2023-07-21 21:32:24 +00:00
~TaskManager();
void start();
2023-10-07 22:36:20 +00:00
void queueJob(const std::filesystem::path& source, const std::filesystem::path& destination);
2023-07-21 21:32:24 +00:00
void stop();
bool busy() const;
void wait();
unsigned int getCompleteTasks() const;
2023-07-21 21:32:24 +00:00
private:
void loop();
static JobResult mp3Job(const std::filesystem::path& source, const std::filesystem::path& destination, Logger::Severity logLevel);
2023-07-21 21:32:24 +00:00
private:
2023-10-07 22:36:20 +00:00
std::shared_ptr<Settings> settings;
std::shared_ptr<Printer> logger;
unsigned int busyThreads;
unsigned int maxTasks;
unsigned int completeTasks;
2023-07-21 21:32:24 +00:00
bool terminate;
bool running;
2023-07-21 21:32:24 +00:00
mutable std::mutex queueMutex;
std::condition_variable loopConditional;
std::condition_variable waitConditional;
std::vector<std::thread> threads;
2023-10-07 22:36:20 +00:00
std::queue<std::pair<std::filesystem::path, std::filesystem::path>> jobs;
2023-07-21 21:32:24 +00:00
};