Provisional multithreading framework

This commit is contained in:
Dynamitos committed 2021-03-31 12:18:16 +02:00
1 parent fb3c66cc4c
commit 28f0f88c03
37 files changed
+788 -186

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
target_sources(SeeleEngine
PRIVATE
Fibers.h
Fibers.cpp
JobQueue.h
JobQueue.cpp)
+36
View File
@@ -0,0 +1,36 @@
#include "Fibers.h"
using namespace Seele;
using namespace Seele::Fibers;
std::atomic_uint64_t FiberJob::jobIDGenerator;
Counter::Counter()
{
count.store(0);
}
Counter::Counter(uint64 initialValue)
{
count.store(initialValue);
}
Counter::~Counter()
{
}
AwaitCounter&& FiberTask::promise_type::await_transform(AwaitCounter&& awaitCounter)
{
counter = awaitCounter.counter;
target = awaitCounter.target;
priority = awaitCounter.priority;
return std::move(awaitCounter);
}
FiberJob::FiberJob()
{
}
FiberJob::~FiberJob()
{
}
+205
View File
@@ -0,0 +1,205 @@
#pragma once
#include "MinimalEngine.h"
#include "JobQueue.h"
#include <atomic>
#include <functional>
namespace Seele
{
namespace Fibers
{
class Counter
{
public:
Counter();
Counter(uint64 initialValue);
~Counter();
inline void increment()
{
count++;
}
inline void decrement()
{
count--;
}
inline void setValue(uint64 value)
{
count.store(value);
}
inline uint64 getValue()
{
return count.load();
}
inline bool greaterEqual(uint64 target)
{
return count.load() >= target;
}
inline bool greaterThan(uint64 target)
{
return count.load() > target;
}
inline bool lessEqual(uint64 target)
{
return count.load() <= target;
}
inline bool lessThan(uint64 target)
{
return count.load() < target;
}
inline bool equals(uint64 target)
{
return count.load() == target;
}
friend std::strong_ordering operator<=>(const Counter& counter, uint64 other)
{
return counter.count.load() <=> other;
}
friend std::ostream& operator<<(std::ostream& stream, const Counter* counter)
{
stream << counter->count;
return stream;
}
friend std::ostream& operator<<(std::ostream& stream, PCounter counter)
{
stream << counter->count;
return stream;
}
private:
std::atomic_uint64_t count;
};
DEFINE_REF(Counter);
struct AwaitCounter
{
PCounter counter;
uint64 target;
JobPriority priority = JobPriority::MEDIUM;
bool await_ready() { return counter->greaterEqual(target); }
void await_suspend(std::coroutine_handle<> h) {
}
void await_resume() {}
};
struct AwaitCounter;
class FiberTask
{
public:
struct promise_type;
using handle_type = std::coroutine_handle<promise_type>;
private:
promise_type* promise;
public:
FiberTask() = default;
explicit FiberTask(promise_type* promise)
: promise(promise) {}
FiberTask(const FiberTask& other)
: promise(other.promise) {}
FiberTask(FiberTask&& other)
: promise(std::move(other.promise)) {}
FiberTask& operator=(const FiberTask& other)
{
promise = other.promise;
return *this;
}
FiberTask& operator=(FiberTask&& other)
{
promise = std::move(other.promise);
return *this;
}
inline void resume() { promise->resume(); }
inline void destroy() { promise->destroy(); }
inline bool done() { return promise->done(); }
inline PCounter getCounter() const { return promise->counter; }
inline uint64 getTarget() const { return promise->target; }
inline JobPriority getPriority() const { return promise->priority; }
struct promise_type
{
promise_type()
{
handle = handle_type::from_promise(*this);
}
~promise_type() = default;
promise_type(const promise_type&) = delete;
promise_type(promise_type&&) = delete;
promise_type &operator=(const promise_type&) = delete;
promise_type &operator=(promise_type&&) = delete;
FiberTask get_return_object()
{
return FiberTask(this);
}
auto initial_suspend() { return std::suspend_never{}; }
auto final_suspend() noexcept { return std::suspend_always{}; }
auto return_void() {}
void unhandled_exception() { std::exit(1); }
AwaitCounter&& await_transform(AwaitCounter&& awaitCounter);
inline void resume() { handle.resume(); }
inline void destroy() { handle.destroy(); }
inline bool done() { return handle.done(); }
private:
handle_type handle;
PCounter counter;
uint64 target;
JobPriority priority;
friend class FiberTask;
};
};
class FiberJob
{
public:
FiberJob();
template<typename func, typename... args>
FiberJob(PCounter counter, func function, args... arg)
: counter(counter), function(std::bind(function, arg...))
{
jobID = jobIDGenerator.fetch_add(1);
}
template<typename func, typename...args>
FiberJob(PCounter counter, JobPriority priority, func function, args... arg)
: counter(counter), priority(priority), function(std::bind(function, arg...))
{
}
FiberJob(const FiberJob& other) = delete;
FiberJob(FiberJob&& other)
: counter(std::move(other.counter))
, jobID(std::move(other.jobID))
, priority(std::move(other.priority))
, function(std::move(other.function))
{
}
~FiberJob();
FiberJob& operator=(const FiberJob& other) = delete;
FiberJob& operator=(FiberJob&& other)
{
if(this != &other)
{
counter = std::move(other.counter);
jobID = std::move(other.jobID);
priority = std::move(other.priority);
function = std::move(other.function);
}
return *this;
}
void operator()()
{
FiberTask task = function();
if(!task.done())
{
JobQueue::suspendJob(std::move(task));
}
else
{
counter->increment();
task.destroy();
}
}
inline JobPriority getPriority()
{
return priority;
}
private:
PCounter counter;
uint64 jobID;
static std::atomic_uint64_t jobIDGenerator;
JobPriority priority = JobPriority::MEDIUM;
std::function<FiberTask()> function;
};
} // namespace Fibers
} // namespace Seele
+134
View File
@@ -0,0 +1,134 @@
#include "JobQueue.h"
#include "Fibers.h"
#include <Windows.h>
#include <processthreadsapi.h>
using namespace Seele;
using namespace Seele::Fibers;
std::wstring toString(JobPriority priority)
{
switch (priority)
{
case JobPriority::HIGH:
return L"HIGH";
case JobPriority::MEDIUM:
return L"MEDIUM";
case JobPriority::LOW:
return L"LOW";
case JobPriority::IO:
return L"IO";
default:
return L"Illegal Priority";
}
}
Array<JobQueue*> JobQueue::workers;
StaticArray<JobPriorityInfo, (size_t)JobPriority::NUM_PRIORITIES> JobQueue::priorityQueues;
JobQueue::JobQueue(JobPriority priority)
: priority(priority)
{
threadHandle = std::thread(&JobQueue::work, this);
SetThreadDescription(threadHandle.native_handle(), toString(priority).c_str());
}
JobQueue::~JobQueue()
{
running = false;
threadHandle.join();
}
void JobQueue::initJobQueues()
{
// TODO: make this dependent on actual cores
JobPriority priorities[16] = {
JobPriority::IO, JobPriority::IO,
JobPriority::LOW, JobPriority::LOW, JobPriority::LOW, JobPriority::LOW,
JobPriority::MEDIUM, JobPriority::MEDIUM, JobPriority::MEDIUM, JobPriority::MEDIUM, JobPriority::MEDIUM,
JobPriority::HIGH, JobPriority::HIGH, JobPriority::HIGH, JobPriority::HIGH, JobPriority::HIGH};
for(uint32 i = 0; i < 16; ++i)
{
workers.add(new JobQueue(priorities[i]));
}
}
void JobQueue::cleanupJobQueues()
{
for(auto worker : workers)
{
delete worker;
}
}
void JobQueue::runJobs(FiberJob* jobs, uint32 numJobs)
{
for(uint32 i = 0; i < numJobs; ++i)
{
JobPriorityInfo& info = priorityQueues[(size_t)jobs[i].getPriority()];
std::unique_lock lock(info.jobQueueLock);
info.jobQueue.add(std::move(jobs[i]));
//info.jobQueueCV.notify_one();
}
}
void JobQueue::waitForCounter(PCounter counter, uint32 waitFor)
{
while(counter->lessThan(waitFor))
std::this_thread::yield();
}
void JobQueue::suspendJob(FiberTask&& task)
{
JobPriorityInfo& info = priorityQueues[(size_t)task.getPriority()];
std::unique_lock lock(info.jobQueueLock);
info.waiterQueue[task.getCounter()].add(task);
}
void JobQueue::refreshWaiterQueue()
{
JobPriorityInfo& info = priorityQueues[(size_t)priority];
//shortcut to avoid iterator creation
if(info.waiterQueue.empty())
return;
for(auto it : info.waiterQueue)
{
auto& entries = info.waiterQueue[it.key];
for(uint32 i = 0; i < entries.size(); ++i)
{
if(it.key->greaterEqual(entries[i].getTarget()))
{
info.resumableTasks.add(std::move(entries[i]));
entries.remove(i);
i--;
}
}
}
}
void JobQueue::work()
{
JobPriorityInfo& targetInfo = priorityQueues[(size_t)priority];
auto& jobQueue = targetInfo.jobQueue;
auto& resumableTasks = targetInfo.resumableTasks;
while(running)
{
std::unique_lock lock(targetInfo.jobQueueLock);
refreshWaiterQueue();
if(resumableTasks.size() > 0)
{
FiberTask task = std::move(resumableTasks.front());
resumableTasks.popFront();
lock.unlock();
task.resume();
lock.lock();
}
if(jobQueue.size() > 0)
{
FiberJob job = std::move(jobQueue.front());
jobQueue.popFront();
lock.unlock();
job();
lock.lock();
}
}
}
+56
View File
@@ -0,0 +1,56 @@
#pragma once
#include "MinimalEngine.h"
#include "Containers/List.h"
#include <coroutine>
namespace Seele
{
namespace Fibers
{
class FiberJob;
DECLARE_REF(Counter);
enum class JobPriority : size_t
{
HIGH = 0,
MEDIUM,
LOW,
IO,
NUM_PRIORITIES
};
struct AtomicJobQueue
{
};
class FiberTask;
struct JobPriorityInfo
{
List<FiberJob> jobQueue;
Map<PCounter, Array<FiberTask>> waiterQueue;
List<FiberTask> resumableTasks;
std::mutex jobQueueLock;
};
class JobQueue
{
public:
JobQueue(JobPriority priority);
JobQueue(const JobQueue& other) = delete;
JobQueue(JobQueue&& other) = delete;
JobQueue& operator=(const JobQueue& other) = delete;
JobQueue& operator=(JobQueue&& other) = delete;
~JobQueue();
static void initJobQueues();
static void cleanupJobQueues();
static void runJobs(FiberJob* jobs, uint32 numJobs);
//This does in fact not do any coroutine stuff, but is a simple busy wait
//For coroutine waits, you have to be in a coroutine, and
static void waitForCounter(PCounter counter, uint32 waitFor);
static void suspendJob(FiberTask&& task);
void refreshWaiterQueue();
private:
void work();
static Array<JobQueue*> workers;
static StaticArray<JobPriorityInfo, (size_t)JobPriority::NUM_PRIORITIES> priorityQueues;
std::thread threadHandle;
JobPriority priority;
bool running = true;
};
} // namespace Fibers
} // namespace Seele