Trying out generic thread pool

This commit is contained in:
Dynamitos
2021-10-12 14:20:30 +02:00
parent a6f784e6a1
commit 99760724cd
6 changed files with 88 additions and 4 deletions
+1
View File
@@ -1,5 +1,6 @@
target_sources(SeeleEngine
PRIVATE
EngineTypes.h
MinimalEngine.h
MinimalEngine.cpp
main.cpp)
+20
View File
@@ -0,0 +1,20 @@
#include "ThreadPool.h"
using namespace Seele;
static ThreadPool gThreadPool;
Job JobPromise::get_return_object() noexcept {
return Job { std::coroutine_handle<JobPromise>::from_promise(*this) };
}
ThreadPool::ThreadPool(uint32 threadCount)
: workers(threadCount)
{
}
static ThreadPool& getGlobalThreadPool()
{
return gThreadPool;
}
+64
View File
@@ -0,0 +1,64 @@
#pragma once
#include <thread>
#include <coroutine>
#include "MinimalEngine.h"
namespace Seele
{
enum class JobStage
{
INPUT,
GAMELOGIC,
RENDER,
DONT_CARE
};
struct [[nodiscard]] Job;
struct JobPromise
{
Job get_return_object() noexcept;
std::suspend_never initial_suspend() const noexcept { return {}; }
std::suspend_never final_suspend() const noexcept { return {}; }
void return_void() noexcept {}
void unhandled_exception() noexcept {
std::cerr << "Unhandled exception caught" << std::endl;
exit(1);
};
};
struct [[nodiscard]] Job
{
public:
using promise_type = JobPromise;
explicit Job(std::coroutine_handle<task_promise> handle, JobStage stage = JobStage::DONT_CARE)
: handle(handle)
, stage(stage)
{}
~Job()
{
if(handle)
{
handle.destroy();
}
}
private:
std::coroutine_handle<JobPromise> handle;
JobStage stage;
};
class ThreadPool
{
public:
ThreadPool(uint32 threadCount = std::thread::hardware_concurrency);
virtual ~ThreadPool();
void schedule(std::function<void()> function)
{
}
private:
Array<std::thread> workers;
void threadLoop();
};
static ThreadPool& getGlobalThreadPool();
} // namespace Seele
+2 -2
View File
@@ -35,7 +35,7 @@ void RenderHierarchy::addElement(PElement addedElement)
updates.add(new AddElementRenderHierarchyUpdate{
addedElement.getHandle(),
addedElement->getParent().getHandle()
}));
});
}
void RenderHierarchy::removeElement(PElement elementToRemove)
@@ -68,6 +68,6 @@ void RenderHierarchy::updateHierarchy()
}
for(auto update : localUpdates)
{
update->apply(drawElements);
}
}
-1
View File
@@ -4,7 +4,6 @@
namespace Seele
{
DECLARE_REF(Window)
// A view is a part of the window, which can be anything from a scene viewport to an inspector
class View
{