mlc/src/taskmanager.h

69 lines
1.8 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 {
2023-10-13 01:00:16 +00:00
using JobResult = std::pair<bool, std::list<Logger::Message>>;
struct Job;
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-13 01:00:16 +00:00
void queueConvert(const std::filesystem::path& source, const std::filesystem::path& destination);
void queueCopy(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();
2023-10-13 19:10:08 +00:00
JobResult execute(Job& job);
2023-10-13 01:00:16 +00:00
void printResilt(const Job& job, const JobResult& result);
2023-10-13 19:10:08 +00:00
static JobResult mp3Job(const Job& job, const std::shared_ptr<Settings>& settings);
static JobResult copyJob(const Job& job, const std::shared_ptr<Settings>& settings);
2023-10-13 01:00:16 +00:00
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-13 01:00:16 +00:00
std::queue<Job> jobs;
2023-07-21 21:32:24 +00:00
};
2023-10-13 01:00:16 +00:00
struct TaskManager::Job {
enum Type {
copy,
convert
};
Job(Type type, const std::filesystem::path& source, std::filesystem::path destination);
Type type;
std::filesystem::path source;
std::filesystem::path destination;
};