Files
Seele/src/Engine/ThreadPool.cpp
T

182 lines
4.3 KiB
C++
Raw Normal View History

2021-10-12 14:20:30 +02:00
#include "ThreadPool.h"
2021-12-02 13:00:03 +01:00
#include <memory_resource>
2021-10-12 14:20:30 +02:00
using namespace Seele;
2021-12-27 15:04:53 +01:00
std::atomic_uint64_t Seele::globalCounter;
2021-11-01 20:25:16 +01:00
Event::Event()
2021-12-15 00:05:42 +01:00
: flag(new std::atomic_bool())
{
}
2021-10-23 00:22:35 +02:00
2021-12-02 13:00:03 +01:00
Event::Event(nullptr_t)
: flag(nullptr)
2021-12-15 00:05:42 +01:00
{
}
2021-12-02 13:00:03 +01:00
2021-12-15 00:05:42 +01:00
Event::Event(const std::string &name)
: name(name), flag(new std::atomic_bool())
{
}
2021-11-01 20:25:16 +01:00
void Event::raise()
2021-10-23 00:22:35 +02:00
{
2021-11-01 20:25:16 +01:00
flag->store(1);
2021-11-24 12:10:23 +01:00
getGlobalThreadPool().notify(*this);
2021-11-01 20:25:16 +01:00
}
void Event::reset()
2021-12-15 00:05:42 +01:00
{
2021-11-01 20:25:16 +01:00
flag->store(0);
2021-10-23 00:22:35 +02:00
}
2021-10-12 14:20:30 +02:00
2021-11-01 20:25:16 +01:00
bool Event::await_ready()
{
return flag->load();
2021-10-12 14:20:30 +02:00
}
ThreadPool::ThreadPool(uint32 threadCount)
: workers(threadCount)
{
2021-11-01 20:25:16 +01:00
running.store(true);
2021-12-15 00:05:42 +01:00
for (uint32 i = 0; i < threadCount; ++i)
2021-11-01 20:25:16 +01:00
{
2021-12-02 13:00:03 +01:00
workers[i] = std::thread(&ThreadPool::threadLoop, this, false);
2021-11-01 20:25:16 +01:00
}
2021-10-12 14:20:30 +02:00
}
2021-10-23 00:22:35 +02:00
ThreadPool::~ThreadPool()
2021-10-12 14:20:30 +02:00
{
running.store(false);
2021-12-15 00:05:42 +01:00
for (auto &thread : workers)
2021-10-23 00:22:35 +02:00
{
thread.join();
}
}
2021-12-15 00:05:42 +01:00
void ThreadPool::enqueueWaiting(Event &event, Promise* job)
2021-11-01 20:25:16 +01:00
{
2021-12-15 00:05:42 +01:00
assert(!job->done());
if(event == nullptr)
{
std::unique_lock lock(jobQueueLock);
2021-12-22 11:42:07 +01:00
//std::cout << "Queueing job " << job->finishedEvent.name << std::endl;
2021-12-15 00:05:42 +01:00
jobQueue.add(job);
jobQueueCV.notify_one();
}
else
{
std::unique_lock lock(waitingLock);
2021-12-22 11:42:07 +01:00
//std::cout << "Job " << job->finishedEvent.name << " waiting on event " << event.name << std::endl;
2021-12-15 00:05:42 +01:00
waitingJobs[event].add(job);
}
2021-11-01 20:25:16 +01:00
}
2021-12-15 00:05:42 +01:00
void ThreadPool::enqueueWaiting(Event &event, MainPromise* job)
2021-11-01 20:25:16 +01:00
{
2021-12-15 00:05:42 +01:00
assert(!job->done());
2021-12-27 15:04:53 +01:00
if(event == nullptr || event)
2021-12-15 00:05:42 +01:00
{
std::unique_lock lock(mainJobLock);
2021-12-22 11:42:07 +01:00
//std::cout << "Queueing job " << job->finishedEvent.name << std::endl;
2021-12-15 00:05:42 +01:00
mainJobs.add(job);
mainJobCV.notify_one();
}
else
{
std::unique_lock lock(waitingMainLock);
2021-12-22 11:42:07 +01:00
//std::cout << job->finishedEvent.name << " waiting on event " << event.name << std::endl;
2021-12-15 00:05:42 +01:00
waitingMainJobs[event].add(job);
}
2021-11-01 20:25:16 +01:00
}
2021-12-15 00:05:42 +01:00
void ThreadPool::notify(Event &event)
2021-11-01 20:25:16 +01:00
{
2021-12-22 11:42:07 +01:00
//std::cout << "Event " << event.name << " raised" << std::endl;
2021-11-01 20:25:16 +01:00
{
std::unique_lock lock(jobQueueLock);
std::unique_lock lock2(waitingLock);
2021-12-15 00:05:42 +01:00
List<Promise*> &jobs = waitingJobs[event];
for (auto &job : jobs)
2021-11-01 20:25:16 +01:00
{
2021-11-24 12:10:23 +01:00
//assert(job.id != -1ull);
2021-12-22 11:42:07 +01:00
//std::cout << "Waking up " << job->finishedEvent.name << std::endl;
2021-12-27 15:04:53 +01:00
job->state = Promise::State::SCHEDULED;
2021-12-15 00:05:42 +01:00
jobQueue.add(job);
2021-11-11 20:12:50 +01:00
jobQueueCV.notify_one();
2021-11-01 20:25:16 +01:00
}
2021-12-22 11:42:07 +01:00
waitingJobs.erase(event);
2021-11-01 20:25:16 +01:00
}
{
std::unique_lock lock(mainJobLock);
std::unique_lock lock2(waitingMainLock);
2021-12-15 00:05:42 +01:00
List<MainPromise*> &jobs = waitingMainJobs[event];
for (auto &job : jobs)
2021-11-01 20:25:16 +01:00
{
2021-11-24 12:10:23 +01:00
//assert(job.id != -1ull);
2021-12-22 11:42:07 +01:00
//std::cout << "Waking up main " << job->finishedEvent.name << std::endl;
2021-12-27 15:04:53 +01:00
job->state = MainPromise::State::SCHEDULED;
2021-12-15 00:05:42 +01:00
mainJobs.add(job);
2021-11-11 20:12:50 +01:00
mainJobCV.notify_one();
2021-11-01 20:25:16 +01:00
}
2021-12-22 11:42:07 +01:00
waitingMainJobs.erase(event);
2021-11-01 20:25:16 +01:00
}
}
2021-12-15 00:05:42 +01:00
void ThreadPool::threadLoop(const bool mainThread)
2021-11-19 15:08:56 +01:00
{
2021-12-15 00:05:42 +01:00
while (running.load())
2021-11-19 15:08:56 +01:00
{
2021-12-15 00:05:42 +01:00
if (mainThread)
2021-11-19 15:08:56 +01:00
{
2021-12-15 00:05:42 +01:00
MainPromise* job;
{
std::unique_lock lock(mainJobLock);
if(mainJobs.empty())
{
mainJobCV.wait(lock);
}
if (!mainJobs.empty())
{
job = mainJobs.retrieve();
}
else
{
continue;
}
}
job->resume();
if (job->done())
{
job->raise();
}
2021-11-19 15:08:56 +01:00
}
else
{
2021-12-15 00:05:42 +01:00
Promise* job;
2021-11-01 20:25:16 +01:00
{
2021-12-15 00:05:42 +01:00
std::unique_lock lock(jobQueueLock);
if (jobQueue.empty())
{
jobQueueCV.wait(lock);
}
if (!jobQueue.empty())
{
job = jobQueue.retrieve();
}
else
{
continue;
}
2021-11-19 15:08:56 +01:00
}
2021-12-15 00:05:42 +01:00
//std::cout << "Starting job " << job.id << std::endl;
job->resume();
if (job->done())
2021-11-19 15:08:56 +01:00
{
2021-12-15 00:05:42 +01:00
job->raise();
2021-11-19 15:08:56 +01:00
}
2021-11-24 12:10:23 +01:00
}
2021-11-01 20:25:16 +01:00
}
}
2021-10-23 00:22:35 +02:00
2021-12-15 00:05:42 +01:00
ThreadPool &Seele::getGlobalThreadPool()
2021-10-23 00:22:35 +02:00
{
static ThreadPool threadPool;
return threadPool;
2021-10-12 14:20:30 +02:00
}