Crimson Engine 1.0.0
A high-performance C++20 game engine prototype showcasing a custom ECS, Vulkan renderer, and ImGui editor.
Loading...
Searching...
No Matches
Engine::JobSystem Class Reference

A lightweight thread pool scheduler for processing concurrent jobs in parallel. More...

#include <JobSystem.hpp>

Classes

struct  Cell

Public Member Functions

void initialize (unsigned int threadCount=0)
 Spawns worker threads.
void shutdown ()
 Shuts down all worker threads and waits for them to join.
void pushJob (std::function< void()> job)
 Enqueues a single void-returning task.
void parallelFor (int count, std::function< void(int)> func)
 Splits a total loop count into chunks and evaluates them in parallel.
unsigned int getThreadCount () const
 Returns active thread count.
bool tryExecuteOneJob ()

Static Public Member Functions

static JobSystem & getInstance ()
 Get the singleton instance.

Private Member Functions

 JobSystem ()=default
 ~JobSystem ()
void workerThreadLoop ()

Private Attributes

std::vector< std::thread > workers
std::unique_ptr< Cell[]> buffer
size_t bufferMask {0}
std::atomic< size_t > enqueuePos {0}
std::atomic< size_t > dequeuePos {0}
std::counting_semaphore< 65536 > jobSemaphore {0}
std::atomic< bool > stop {false}
std::atomic< bool > initialized {false}

Detailed Description

A lightweight thread pool scheduler for processing concurrent jobs in parallel.

Constructor & Destructor Documentation

◆ JobSystem()

Engine::JobSystem::JobSystem ( )
privatedefault

◆ ~JobSystem()

Engine::JobSystem::~JobSystem ( )
private

Member Function Documentation

◆ getInstance()

JobSystem & Engine::JobSystem::getInstance ( )
inlinestatic

Get the singleton instance.

◆ getThreadCount()

unsigned int Engine::JobSystem::getThreadCount ( ) const
inline

Returns active thread count.

◆ initialize()

void Engine::JobSystem::initialize ( unsigned int threadCount = 0)

Spawns worker threads.

Parameters
threadCountNumber of worker threads. Defaults to std::thread::hardware_concurrency() - 1.

◆ parallelFor()

void Engine::JobSystem::parallelFor ( int count,
std::function< void(int)> func )

Splits a total loop count into chunks and evaluates them in parallel.

◆ pushJob()

void Engine::JobSystem::pushJob ( std::function< void()> job)

Enqueues a single void-returning task.

◆ shutdown()

void Engine::JobSystem::shutdown ( )

Shuts down all worker threads and waits for them to join.

◆ tryExecuteOneJob()

bool Engine::JobSystem::tryExecuteOneJob ( )

◆ workerThreadLoop()

void Engine::JobSystem::workerThreadLoop ( )
private

Member Data Documentation

◆ buffer

std::unique_ptr<Cell[]> Engine::JobSystem::buffer
private

◆ bufferMask

size_t Engine::JobSystem::bufferMask {0}
private

◆ dequeuePos

std::atomic<size_t> Engine::JobSystem::dequeuePos {0}
private

◆ enqueuePos

std::atomic<size_t> Engine::JobSystem::enqueuePos {0}
private

◆ initialized

std::atomic<bool> Engine::JobSystem::initialized {false}
private

◆ jobSemaphore

std::counting_semaphore<65536> Engine::JobSystem::jobSemaphore {0}
private

◆ stop

std::atomic<bool> Engine::JobSystem::stop {false}
private

◆ workers

std::vector<std::thread> Engine::JobSystem::workers
private

The documentation for this class was generated from the following files: