-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComputation.h
124 lines (99 loc) · 2.68 KB
/
Computation.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#ifndef _COMPUTATION_H_
#define _COMPUTATION_H_
//#include "StandardIncludes.h"
#include "Types.h"
#include <thread>
#include <deque>
#include <mutex>
#include <condition_variable>
#include <memory>
#include <vector>
#include <chrono>
#include <functional>
using std::chrono::steady_clock;
class Task {
friend class Computation;
private:
virtual bool IsSynchronous();
public:
virtual ~Task();
virtual std::string_view GetDescription() const;
virtual void Execute() = 0;
class Cancellable;
};
class Task::Cancellable : public virtual Task {
public:
virtual ~Cancellable() {}
virtual void Cancel() = 0;
};
class ProgressTrackable {
public:
virtual ~ProgressTrackable() {}
virtual bool GetProgress(SRReal &Numerator, SRReal &Denoninator) const = 0;
};
class ParallelTask : public virtual Task {
public:
virtual void Execute() override;
virtual void Execute(size_t ThreadID);
};
class CooperativeTask : public ParallelTask {
friend class Computation;
protected:
const size_t ThreadCount;
CooperativeTask(size_t ThreadCount) : ThreadCount(ThreadCount) {}
};
class SynchronousTask : public virtual Task {
private:
virtual bool IsSynchronous() override final;
};
class ExecutionContext {
friend class Computation;
private:
std::atomic_uint32_t ReferenceCount = { 1 };
Task *task;
std::mutex Mutex;
std::condition_variable ConditionVariable;
steady_clock::time_point StartTime;
steady_clock::time_point EndTime;
size_t ExecutingThreadCount = 0;
bool AcceptNewThreads = true;
bool finished = false;
bool Cancelled = false;
bool InTaskQueue = true;
void RemoveFromQueue();
public:
using duration = steady_clock::duration;
ExecutionContext(Task *task) : task(task) {}
~ExecutionContext() { delete task; }
void Cancel();
bool Finished();
bool Terminated();
void Wait();
template <class Representation, class Period>
void Wait(std::chrono::duration<Representation, Period> duration) {
std::unique_lock lock(Mutex);
ConditionVariable.wait_for(lock, duration, [this]() { return ExecutingThreadCount == 0 && !AcceptNewThreads; });
}
Task *GetTask();
duration GetDuration();
size_t AddReference();
size_t Release();
size_t WaitAndRelease();
};
class Computation {
friend class ExecutionContext;
private:
static size_t WorkerCount;
static std::thread *Workers;
static std::deque<ExecutionContext> TaskQueue;
static std::mutex TaskQueueMutex;
static std::condition_variable TaskConditionVariable;
static size_t HardwareConcurrency;
static void WorkerFunction();
public:
static void Init();
static ExecutionContext *AddTask(Task *task);
static ExecutionContext *AddTask(const std::function<void()> &function);
static std::vector<ExecutionContext *> GetExecutionContexts();
};
#endif