-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaskController.h
49 lines (44 loc) · 1.27 KB
/
TaskController.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* Copyright (C) 2021 Ilya Entin
*/
#pragma once
#include <barrier>
#include <queue>
#include "ThreadPoolBase.h"
using TaskPtr = std::shared_ptr<class Task>;
using TaskControllerPtr = std::shared_ptr<class TaskController>;
using TaskControllerWeakPtr = std::weak_ptr<class TaskController>;
class TaskController {
enum Phase { PREPROCESSTASK, PROCESSTASK };
class Worker : public Runnable {
bool start() override { return true; }
void stop() override {}
void run() noexcept override;
TaskControllerWeakPtr _taskController;
public:
explicit Worker(TaskControllerWeakPtr taskController);
};
using CompletionFunction = void (*) () noexcept;
bool start();
void stop();
void push(TaskPtr task);
void setNextTask();
static void onTaskCompletion() noexcept;
void onCompletion();
std::atomic<bool> _stopped = false;
std::barrier<CompletionFunction> _barrier;
ThreadPoolBase _threadPool;
std::mutex _queueMutex;
std::condition_variable _queueCondition;
std::queue<TaskPtr> _queue;
TaskPtr _task;
static TaskControllerPtr _single;
static Phase _phase;
public:
TaskController();
~TaskController() = default;
void processTask(TaskPtr task);
static bool create();
static void destroy();
static TaskControllerWeakPtr getWeakPtr();
};