Loading...
Searching...
No Matches
ThreadManager.hpp
Go to the documentation of this file.
#pragma once
#include "Thread.hpp"
#include <EASTL/unique_ptr.h>
#include <EASTL/string.h>
#include <SFML/System.hpp>
#include "IEvent.hpp"
namespace mce::core {
class ThreadManager {
public:
struct ThreadInfo {
Thread* thread;
eastl::string name;
std::chrono::steady_clock::time_point startTime, endTime;
bool finished = false;
};
explicit ThreadManager(QEventBus& qBus);
~ThreadManager();
template<typename F>
Thread* createThread(const eastl::string& name, F func);
template<typename C>
Thread* createThread(const eastl::string& name, void(C::* func)(), C* object);
void launch(Thread* th);
void launch(const eastl::string& name);
void waitAll();
void cleanupFinished();
void printStats();
size_t activeCount() const;
private:
void onThreadFinished(const event::ThreadFinished& e);
void cleanupFinishedUnlocked();
private:
mutable std::mutex mutex;
eastl::unordered_map<Thread*, ThreadInfo> threads;
eastl::vector<eastl::unique_ptr<Thread>> threadStorage;
QEventBus& qBus;
};
template<typename F>
inline Thread* ThreadManager::createThread(const eastl::string& name, F func) {
auto thread = eastl::make_unique<Thread>(qBus, [this, func, name]() {
qBus.post(event::ThreadStarted{ nullptr, name });
func();
});
Thread* rawThread = thread.get();
std::lock_guard<std::mutex> lock(mutex);
ThreadInfo ti;
ti.startTime = std::chrono::steady_clock::now();
ti.name = name;
ti.thread = rawThread;
ti.endTime = {};
ti.finished = false;
threads.emplace(rawThread, ti);
threadStorage.push_back(eastl::move(thread));
return rawThread;
}
template<typename C>
inline Thread* ThreadManager::createThread(const eastl::string& name, void(C::* func)(), C* object) {
qBus.post(event::ThreadStarted{ nullptr, name });
auto thread = eastl::make_unique<Thread>(
qBus,
func,
object
);
Thread* rawThread = thread.get();
{
std::lock_guard<std::mutex> lock(mutex);
threads.emplace(rawThread, ThreadInfo{
thread,
name,
std::chrono::steady_clock::now(),
{},
false
});
}
threadStorage.push_back(eastl::move(thread));
return rawThread;
}
}
Thread.hpp
High-level Thread wrapper and callable adapters used by the project.
mce::core::QEventBus
Thread-safe event bus.
Definition QEventBus.hpp:58
mce::core::Thread
Lightweight high-level thread wrapper.
Definition Thread.hpp:134
mce::core::ThreadManager::createThread
Thread * createThread(const eastl::string &name, F func)
Create (and store) a Thread from a generic callable.
Definition ThreadManager.hpp:151
mce::core::ThreadManager::ThreadManager
ThreadManager(QEventBus &qBus)
Construct a ThreadManager bound to a QEventBus.
Definition ThreadManager.cpp:5
mce::core::ThreadManager::~ThreadManager
~ThreadManager()
Destructor cleans up managed threads and subscriptions.
Definition ThreadManager.cpp:14
mce::core::ThreadManager::waitAll
void waitAll()
Block until all managed threads have finished executing.
Definition ThreadManager.cpp:39
mce::core::ThreadManager::launch
void launch(Thread *th)
Launch a previously created Thread instance.
Definition ThreadManager.cpp:17
mce::core::ThreadManager::activeCount
size_t activeCount() const
Count of currently active (non-finished) threads.
Definition ThreadManager.cpp:62
mce::core::ThreadManager::printStats
void printStats()
Print statistics (e.g. runtime durations) for managed threads.
Definition ThreadManager.cpp:52
mce::core::ThreadManager::cleanupFinished
void cleanupFinished()
Remove and cleanup finished threads from internal storage.
Definition ThreadManager.cpp:47
mce::core::ThreadManager::ThreadInfo
Metadata stored per managed thread.
Definition ThreadManager.hpp:27
mce::core::ThreadManager::ThreadInfo::finished
bool finished
True when the thread has finished execution.
Definition ThreadManager.hpp:35
mce::core::ThreadManager::ThreadInfo::startTime
std::chrono::steady_clock::time_point startTime
Time point when the thread was started.
Definition ThreadManager.hpp:33
mce::core::ThreadManager::ThreadInfo::name
eastl::string name
Human readable thread name.
Definition ThreadManager.hpp:31
mce::core::ThreadManager::ThreadInfo::thread
Thread * thread
Non-owning pointer to the tracked Thread.
Definition ThreadManager.hpp:29
mce::event::ThreadFinished
Definition IEvent.hpp:41
mce::event::ThreadStarted
Definition IEvent.hpp:47