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

58 lines
1.7 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
#include "loggable.h"
2023-10-07 22:36:20 +00:00
#include "settings.h"
2023-07-21 21:32:24 +00:00
class TaskManager {
2023-07-23 12:04:26 +00:00
typedef std::pair<bool, std::list<Loggable::Message>> JobResult;
2023-07-21 21:32:24 +00:00
public:
2023-10-07 22:36:20 +00:00
TaskManager(const std::shared_ptr<Settings>& settings);
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();
void printProgress() const;
2023-10-07 22:36:20 +00:00
void printProgress(const JobResult& result, const std::filesystem::path& source, const std::filesystem::path& destination) const;
2023-07-21 21:32:24 +00:00
private:
void loop();
bool loopCondition() const;
bool waitCondition() const;
2023-10-07 22:36:20 +00:00
static JobResult mp3Job(const std::filesystem::path& source, const std::filesystem::path& destination);
static void printLog(const JobResult& result, const std::filesystem::path& source, const std::filesystem::path& destination);
2023-07-21 21:32:24 +00:00
private:
2023-10-07 22:36:20 +00:00
std::shared_ptr<Settings> settings;
2023-07-21 21:32:24 +00:00
std::atomic<uint32_t> busyThreads;
std::atomic<uint32_t> maxTasks;
std::atomic<uint32_t> completeTasks;
bool terminate;
2023-07-23 12:04:26 +00:00
mutable std::mutex printMutex;
2023-07-21 21:32:24 +00:00
mutable std::mutex queueMutex;
std::mutex busyMutex;
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
std::function<bool()> boundLoopCondition;
std::function<bool()> boundWaitCondition;
};