Minecraft Community Edition 0.1.0
Loading...
Searching...
No Matches
ThreadManager.hpp
Go to the documentation of this file.
1#pragma once
2#include "Thread.hpp"
3#include <EASTL/unique_ptr.h>
4#include <EASTL/string.h>
5#include <SFML/System.hpp>
6#include "IEvent.hpp"
7
16
17namespace mce::core {
18
20 public:
27 struct ThreadInfo {
31 eastl::string name;
33 std::chrono::steady_clock::time_point startTime, endTime;
35 bool finished = false;
36 };
37
45 explicit ThreadManager(QEventBus& qBus);
46
49
61 template<typename F>
62 Thread* createThread(const eastl::string& name, F func);
63
74 template<typename C>
75 Thread* createThread(const eastl::string& name, void(C::* func)(), C* object);
76
83 void launch(Thread* th);
84
92 void launch(const eastl::string& name);
93
99 void waitAll();
100
107 void cleanupFinished();
108
115 void printStats();
116
121 size_t activeCount() const;
122
123 private:
124
132 void onThreadFinished(const event::ThreadFinished& e);
133
139 void cleanupFinishedUnlocked();
140 private:
142 mutable std::mutex mutex;
144 eastl::unordered_map<Thread*, ThreadInfo> threads;
146 eastl::vector<eastl::unique_ptr<Thread>> threadStorage;
148 QEventBus& qBus;
149 };
150 template<typename F>
151 inline Thread* ThreadManager::createThread(const eastl::string& name, F func) {
152 auto thread = eastl::make_unique<Thread>(qBus, [this, func, name]() {
153 qBus.post(event::ThreadStarted{ nullptr, name });
154 func();
155 });
156
157 Thread* rawThread = thread.get();
158
159 std::lock_guard<std::mutex> lock(mutex);
160 ThreadInfo ti;
161 ti.startTime = std::chrono::steady_clock::now();
162 ti.name = name;
163 ti.thread = rawThread;
164 ti.endTime = {};
165 ti.finished = false;
166 threads.emplace(rawThread, ti);
167
168 threadStorage.push_back(eastl::move(thread));
169 return rawThread;
170 }
171 template<typename C>
172 inline Thread* ThreadManager::createThread(const eastl::string& name, void(C::* func)(), C* object) {
173 qBus.post(event::ThreadStarted{ nullptr, name });
174 auto thread = eastl::make_unique<Thread>(
175 qBus,
176 func,
177 object
178 );
179
180 Thread* rawThread = thread.get();
181 {
182 std::lock_guard<std::mutex> lock(mutex);
183 threads.emplace(rawThread, ThreadInfo{
184 thread,
185 name,
186 std::chrono::steady_clock::now(),
187 {},
188 false
189 });
190 }
191
192 threadStorage.push_back(eastl::move(thread));
193 return rawThread;
194 }
195}
High-level Thread wrapper and callable adapters used by the project.
Thread-safe event bus.
Definition QEventBus.hpp:58
Lightweight high-level thread wrapper.
Definition Thread.hpp:134
Thread * createThread(const eastl::string &name, F func)
Create (and store) a Thread from a generic callable.
Definition ThreadManager.hpp:151
ThreadManager(QEventBus &qBus)
Construct a ThreadManager bound to a QEventBus.
Definition ThreadManager.cpp:5
~ThreadManager()
Destructor cleans up managed threads and subscriptions.
Definition ThreadManager.cpp:14
void waitAll()
Block until all managed threads have finished executing.
Definition ThreadManager.cpp:39
void launch(Thread *th)
Launch a previously created Thread instance.
Definition ThreadManager.cpp:17
size_t activeCount() const
Count of currently active (non-finished) threads.
Definition ThreadManager.cpp:62
void printStats()
Print statistics (e.g. runtime durations) for managed threads.
Definition ThreadManager.cpp:52
void cleanupFinished()
Remove and cleanup finished threads from internal storage.
Definition ThreadManager.cpp:47
Metadata stored per managed thread.
Definition ThreadManager.hpp:27
bool finished
True when the thread has finished execution.
Definition ThreadManager.hpp:35
std::chrono::steady_clock::time_point startTime
Time point when the thread was started.
Definition ThreadManager.hpp:33
eastl::string name
Human readable thread name.
Definition ThreadManager.hpp:31
Thread * thread
Non-owning pointer to the tracked Thread.
Definition ThreadManager.hpp:29
Definition IEvent.hpp:41
Definition IEvent.hpp:47