Files
Seele/src/Engine/ThreadPool.cpp
T

108 lines
2.1 KiB
C++
Raw Normal View History

2021-10-12 14:20:30 +02:00
#include "ThreadPool.h"
using namespace Seele;
2021-11-01 20:25:16 +01:00
Event::Event()
: flag(new std::atomic_bool())
{}
2021-10-23 00:22:35 +02:00
2021-11-01 20:25:16 +01:00
Event::Event(const std::string& name)
: name(name)
, flag(new std::atomic_bool())
{}
void Event::raise()
2021-10-23 00:22:35 +02:00
{
2021-11-01 20:25:16 +01:00
flag->store(1);
getGlobalThreadPool().notify(this);
}
void Event::reset()
{
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);
for(uint32 i = 0; i < threadCount; ++i)
{
workers[i] = std::thread(&ThreadPool::threadLoop, this, i == 0);
}
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
{
2021-10-23 00:22:35 +02:00
for(auto& thread : workers)
{
thread.join();
}
}
2021-11-01 20:25:16 +01:00
void ThreadPool::addJob(Job&& job)
{
std::unique_lock lock(jobQueueLock);
jobQueue.add(job);
}
void ThreadPool::addJob(MainJob&& job)
{
std::unique_lock lock(mainJobLock);
mainJobs.add(job);
}
void ThreadPool::enqueueWaiting(Event* event, Job&& job)
{
std::unique_lock lock(waitingLock);
waitingJobs[event].add(std::move(job));
}
void ThreadPool::enqueueWaiting(Event* event, MainJob&& job)
{
std::unique_lock lock(waitingMainLock);
waitingMainJobs[event].add(std::move(job));
}
void ThreadPool::notify(Event* event)
{
{
std::unique_lock lock(jobQueueLock);
std::unique_lock lock2(waitingLock);
for(auto&& job : waitingJobs[event])
{
jobQueue.add(job);
}
waitingJobs[event].clear();
}
{
std::unique_lock lock(mainJobLock);
std::unique_lock lock2(waitingMainLock);
for(auto&& job : waitingMainJobs[event])
{
mainJobs.add(job);
}
waitingMainJobs[event].clear();
}
event->reset();
}
void ThreadPool::threadLoop(const bool mainThread)
{
while(running.load())
{
if(mainThread)
{
std::unique_lock lock(mainJobLock);
if(!mainJobs.empty())
{
MainJob&& job = mainJobs.retrieve();
job.resume();
}
}
}
}
2021-10-23 00:22:35 +02:00
ThreadPool& Seele::getGlobalThreadPool()
{
static ThreadPool threadPool;
return threadPool;
2021-10-12 14:20:30 +02:00
}