-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskController.h
More file actions
53 lines (47 loc) · 1.43 KB
/
TaskController.h
File metadata and controls
53 lines (47 loc) · 1.43 KB
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
50
51
52
53
/*
* Copyright (C) 2021 Ilya Entin
*/
#pragma once
#include <barrier>
#include <queue>
#include <boost/core/noncopyable.hpp>
#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 : public std::enable_shared_from_this<TaskController>,
private boost::noncopyable {
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 _instance;
static Phase _phase;
static std::mutex _mutex;
public:
TaskController();
~TaskController() = default;
void processTask(TaskPtr task);
static bool create();
static void destroy();
static TaskControllerWeakPtr getWeakPtr();
};