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

54 lines
1.5 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>
#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>
#include "loggable.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:
TaskManager();
~TaskManager();
void start();
void queueJob(const std::string& source, const std::string& destination);
void stop();
bool busy() const;
void wait();
void printProgress() const;
2023-07-23 12:04:26 +00:00
void printProgress(const JobResult& result, const std::string& source, const std::string& destination) const;
2023-07-21 21:32:24 +00:00
private:
void loop();
bool loopCondition() const;
bool waitCondition() const;
2023-07-23 12:04:26 +00:00
static JobResult job(const std::string& source, const std::string& destination);
static void printLog(const JobResult& result, const std::string& source, const std::string& destination);
2023-07-21 21:32:24 +00:00
private:
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;
std::queue<std::pair<std::string, std::string>> jobs;
std::function<bool()> boundLoopCondition;
std::function<bool()> boundWaitCondition;
};