|
BluFedora Job System v1.0.0
This is a C++ job system library for use in game engines.
|
Namespaces | |
| namespace | internal |
Classes | |
| struct | Counter |
| The only syncronization mechanism. Allows you to wait on tasks you asssociated with this counter. More... | |
| struct | Ctx |
| struct | InitializationLock |
| struct | JobSystemContext |
| struct | JobSystemCreateOptions |
| The runtime configuration for the Job System. More... | |
| struct | JobSystemMemoryRequirements |
The memory requirements for a given configuration JobSystemCreateOptions. More... | |
| class | LockedQueue |
| class | MPMCQueue |
| struct | Splitter |
| class | SPMCDeque |
| class | SPSCQueue |
| struct | Task |
| union | TaskMemoryBlock |
| struct | TaskPool |
| struct | TaskPtr |
| struct | ThreadLocalState |
Typedefs | |
| using | WorkerID = std::uint16_t |
| The id type of each worker thread. More... | |
| using | TaskHandle = std::uint16_t |
| using | TaskHandleType = TaskHandle |
| using | AtomicTaskHandleType = std::atomic< TaskHandle > |
| using | WorkerIDType = WorkerID |
| using | AtomicInt32 = std::atomic_int32_t |
| using | Byte = unsigned char |
| using | AtomicTaskPtr = std::atomic< job::TaskPtr > |
Enumerations | |
| enum class | QueueMode : std::uint8_t { Default , WorkerOnly } |
| Determines which threads the task will be allowed to run on. More... | |
| enum class | SPMCDequeStatus { SUCCESS , FAILED_RACE , FAILED_SIZE } |
Functions | |
| std::size_t | NumSystemThreads () noexcept |
| Makes system calls to grab the number threads / processors on the device. This function can be called by any thread concurrently. More... | |
| void | Initialize (const JobSystemMemoryRequirements &memory_requirements={}, void *const memory=nullptr) noexcept |
| Sets up the Job system and creates all the worker threads. The thread that calls 'job::Initialize' is considered the main thread. More... | |
| const char * | ProcessorArchitectureName () noexcept |
| An implementation defined name for the CPU architecture of the device. This function can be called by any thread concurrently. More... | |
| std::uint16_t | NumWorkers () noexcept |
| Returns the number of workers created by the system. This function can be called by any thread concurrently. More... | |
| WorkerID | CurrentWorker () noexcept |
| The current id of the current thread. This function can be called by any thread concurrently. More... | |
| bool | IsMainThread () noexcept |
| Allows for querying if we are currently executing in the main thread. More... | |
| void | Shutdown () noexcept |
| This will deallocate any memory used by the system and shutdown any threads created by 'bfjob::initialize'. More... | |
| template<typename Closure > | |
| void | Dispatch (const char *const name, Counter *const counter, const Closure &Callback, const QueueMode queue=QueueMode::Default) noexcept |
| Main API entrypoint, Pushes a task onto the queue. More... | |
| void | WaitOn (const Counter &counter) noexcept |
Blocks until all tasks associated with counter are done while This function will block but do work while being blocked. More... | |
| void | PauseProcessor () noexcept |
| CPU pause instruction to indicate when you are in a spin wait loop. More... | |
| void | YieldTimeSlice () noexcept |
| Asks the OS to yield this threads execution to another thread on the current cpu core. More... | |
| template<typename F , typename S > | |
| void | ParallelFor (const char *const name, Counter *const counter, const std::size_t start, const std::size_t count, S &&splitter, F &&fn, const QueueMode queue=QueueMode::Default) |
Parallel for algorithm, splits the work up recursively splitting based on the splitter passed in. More... | |
| template<typename T , typename F , typename S > | |
| void | ParallelFor (const char *const name, Counter *const counter, T *const data, const std::size_t count, S &&splitter, F &&fn, const QueueMode queue=QueueMode::Default) |
| template<typename... F> | |
| void | ParallelInvoke (const char *const name, Counter *const counter, const QueueMode queue, F &&... fns) |
| Invokes each passed in function object in parallel. More... | |
| template<typename Splitter , typename Reducer > | |
| void | ParallelReduce (const char *const name, Counter *const counter, const std::size_t start, const std::size_t count, Splitter &&splitter, Reducer &&reduce, const QueueMode queue=QueueMode::Default) |
Variables | |
| static constexpr std::size_t | k_FalseSharingPadSize = std::hardware_destructive_interference_size |
| static constexpr std::size_t | k_CachelineSize = 64u |
| static constexpr std::size_t | k_ExpectedTaskSize = std::max(std::size_t(128u), k_CachelineSize) |
| static constexpr TaskHandle | NullTaskHandle = std::numeric_limits<TaskHandle>::max() |
| struct job::Counter |
The only syncronization mechanism. Allows you to wait on tasks you asssociated with this counter.
Definition at line 48 of file job_api.hpp.
| Class Members | ||
|---|---|---|
| atomic_uint64_t | unfinished_tasks | |
| struct job::InitializationLock |
Definition at line 175 of file job_system.cpp.
| Class Members | ||
|---|---|---|
| mutex | init_mutex | |
| condition_variable | init_cv | |
| atomic_uint32_t | num_workers_ready | |
| struct job::JobSystemContext |
Definition at line 182 of file job_system.cpp.
| Class Members | ||
|---|---|---|
| ThreadLocalState * | workers | |
| uint32_t | num_workers | |
| atomic_uint32_t | num_user_threads_setup | |
| uint32_t | num_tasks_per_worker | |
| InitializationLock | init_lock | |
| const char * | sys_arch_str | |
| size_t | system_alloc_size | |
| size_t | system_alloc_alignment | |
| bool | needs_delete | |
| atomic_bool | is_running | |
| mutex | worker_sleep_mutex | |
| condition_variable | worker_sleep_cv | |
| atomic_uint32_t | num_available_jobs | |
| struct job::JobSystemCreateOptions |
The runtime configuration for the Job System.
Definition at line 86 of file job_api.hpp.
| union job::TaskMemoryBlock |
Definition at line 150 of file job_system.cpp.
| Class Members | ||
|---|---|---|
| TaskMemoryBlock * | next | |
| unsigned char | storage[sizeof(Task)] | |
| struct job::TaskPool |
Definition at line 157 of file job_system.cpp.
| Class Members | ||
|---|---|---|
| TaskMemoryBlock * | memory | |
| TaskMemoryBlock * | freelist | |
| struct job::ThreadLocalState |
Definition at line 163 of file job_system.cpp.
| Class Members | ||
|---|---|---|
| SPMCDeque< TaskPtr > | normal_queue | |
| SPMCDeque< TaskPtr > | worker_queue | |
| TaskPool | task_allocator | |
| TaskHandle * | allocated_tasks | |
| TaskHandleType | num_allocated_tasks | |
| ThreadLocalState * | last_stolen_worker | |
| pcg_state_setseq_64 | rng_state | |
| thread | thread_id | |
| using job::WorkerID = typedef std::uint16_t |
The id type of each worker thread.
Definition at line 39 of file job_api.hpp.
| using job::TaskHandle = typedef std::uint16_t |
Definition at line 79 of file job_system.cpp.
| using job::TaskHandleType = typedef TaskHandle |
Definition at line 80 of file job_system.cpp.
| using job::AtomicTaskHandleType = typedef std::atomic<TaskHandle> |
Definition at line 81 of file job_system.cpp.
| using job::WorkerIDType = typedef WorkerID |
Definition at line 82 of file job_system.cpp.
| using job::AtomicInt32 = typedef std::atomic_int32_t |
Definition at line 83 of file job_system.cpp.
| using job::Byte = typedef unsigned char |
Definition at line 84 of file job_system.cpp.
| using job::AtomicTaskPtr = typedef std::atomic<job::TaskPtr> |
Definition at line 112 of file job_system.cpp.
|
strong |
Determines which threads the task will be allowed to run on.
| Enumerator | |
|---|---|
| Default | Tasks in this queue will run on either the main or worker threads. |
| WorkerOnly | Tasks in this queue will never run on the main thread. |
Definition at line 64 of file job_api.hpp.
|
strong |
| Enumerator | |
|---|---|
| SUCCESS | Returned from Push, Pop and Steal. |
| FAILED_RACE | Returned from Pop and Steal. |
| FAILED_SIZE | Returned from Push, Pop and Steal. |
Definition at line 228 of file job_queue.hpp.
|
noexcept |
Makes system calls to grab the number threads / processors on the device. This function can be called by any thread concurrently.
Can be called even if the job system has not been initialized.
Definition at line 811 of file job_system.cpp.
|
noexcept |
Sets up the Job system and creates all the worker threads. The thread that calls 'job::Initialize' is considered the main thread.
| memory_requirements | The customization parameters to initialize the system with. To be gotten from job::MemRequirementsForConfig. |
| memory | Must be memory_requirements.byte_size in size and with alignment memory_requirements.alignment. If nullptr then the system heap will be used. |
Definition at line 703 of file job_system.cpp.
References g_CurrentWorker, g_JobSystem, job::JobSystemContext::init_lock, Initialize(), job::JobSystemCreateOptions::job_steal_rng_seed, JobAssert, job::JobSystemContext::needs_delete, job::JobSystemCreateOptions::normal_queue_size, job::JobSystemContext::num_available_jobs, job::JobSystemContext::num_tasks_per_worker, job::JobSystemContext::num_user_threads_setup, job::JobSystemContext::num_workers, job::InitializationLock::num_workers_ready, job::JobSystemContext::sys_arch_str, job::JobSystemContext::system_alloc_alignment, job::JobSystemContext::system_alloc_size, job::JobSystemCreateOptions::worker_queue_size, and job::JobSystemContext::workers.
Referenced by Initialize().
|
noexcept |
An implementation defined name for the CPU architecture of the device. This function can be called by any thread concurrently.
Definition at line 868 of file job_system.cpp.
References g_JobSystem, and job::JobSystemContext::sys_arch_str.
|
noexcept |
Returns the number of workers created by the system. This function can be called by any thread concurrently.
Definition at line 863 of file job_system.cpp.
References g_JobSystem, and job::JobSystemContext::num_workers.
Referenced by job::internal::DispatchImpl(), and job::Splitter::EvenSplit().
|
noexcept |
The current id of the current thread. This function can be called by any thread concurrently.
The main thread will always be 0.
Definition at line 873 of file job_system.cpp.
References g_CurrentWorker, g_JobSystem, JobAssert, and job::JobSystemContext::workers.
Referenced by WaitOn().
|
noexcept |
Allows for querying if we are currently executing in the main thread.
Definition at line 879 of file job_system.cpp.
References g_CurrentWorker, and IsMainThread().
Referenced by IsMainThread().
|
noexcept |
This will deallocate any memory used by the system and shutdown any threads created by 'bfjob::initialize'.
Definition at line 884 of file job_system.cpp.
References g_CurrentWorker, g_JobSystem, job::JobSystemContext::is_running, JobAssert, job::JobSystemContext::needs_delete, job::JobSystemContext::num_workers, job::JobSystemContext::system_alloc_size, job::JobSystemContext::worker_sleep_mutex, and job::JobSystemContext::workers.
|
noexcept |
Main API entrypoint, Pushes a task onto the queue.
| Closure | Callable Type with void Closure(const job::Ctx& ctx);. |
| name | Optional name for the task, can be null. Not used internally for anything. |
| counter | The counter to associate the job with. |
| Callback | Callable with [](const job::Ctx& ctx) -> void {}. |
| queue | Which queue to push the task to. |
queue mode. Definition at line 251 of file job_api.hpp.
References job::internal::DispatchImpl().
Referenced by ParallelFor(), ParallelInvoke(), and ParallelReduce().
|
noexcept |
Blocks until all tasks associated with counter are done while This function will block but do work while being blocked.
| counter | The counter to wait for on. |
Definition at line 931 of file job_system.cpp.
References CurrentWorker(), and job::Counter::unfinished_tasks.
Referenced by ParallelReduce().
|
noexcept |
CPU pause instruction to indicate when you are in a spin wait loop.
Definition at line 1051 of file job_system.cpp.
References NativePause.
Referenced by job::MPMCQueue::Commit().
|
noexcept |
Asks the OS to yield this threads execution to another thread on the current cpu core.
Definition at line 1058 of file job_system.cpp.
| void job::ParallelFor | ( | const char *const | name, |
| Counter *const | counter, | ||
| const std::size_t | start, | ||
| const std::size_t | count, | ||
| S && | splitter, | ||
| F && | fn, | ||
| const QueueMode | queue = QueueMode::Default |
||
| ) |
Parallel for algorithm, splits the work up recursively splitting based on the splitter passed in.
Assumes all callable objects passed in can be invoked on multiple threads at the same time.
| F | Type of function object passed in. Must be callable like: fn(const job::Ctx& ctx, const std::size_t index) |
| S | Callable splitter, must be callable like: splitter(std::size_t count) |
| start | Start index for the range to be parallelized. |
| count | start + count defines the end range. |
| splitter | Callable splitter, must be callable like: splitter(std::size_t count) |
| fn | Function object must be callable like: fn(const job::Ctx& ctx, const std::size_t index) |
| parent | Parent task to add this task as a child of. |
Definition at line 354 of file job_api.hpp.
References Dispatch(), ParallelFor(), job::Ctx::task_counter, and job::Ctx::task_name.
Referenced by ParallelFor(), and ParallelReduce().
| void job::ParallelFor | ( | const char *const | name, |
| Counter *const | counter, | ||
| T *const | data, | ||
| const std::size_t | count, | ||
| S && | splitter, | ||
| F && | fn, | ||
| const QueueMode | queue = QueueMode::Default |
||
| ) |
Definition at line 375 of file job_api.hpp.
References ParallelFor().
| void job::ParallelInvoke | ( | const char *const | name, |
| Counter *const | counter, | ||
| const QueueMode | queue, | ||
| F &&... | fns | ||
| ) |
Invokes each passed in function object in parallel.
| ...F | The function objects types. Must be callable like: fn(Task* task) |
| parent | Parent task to add this task as a child of. |
| ...fns | Function objects must be callable like: fn(Task* task) |
Definition at line 398 of file job_api.hpp.
References Dispatch().
| void job::ParallelReduce | ( | const char *const | name, |
| Counter *const | counter, | ||
| const std::size_t | start, | ||
| const std::size_t | count, | ||
| Splitter && | splitter, | ||
| Reducer && | reduce, | ||
| const QueueMode | queue = QueueMode::Default |
||
| ) |
Definition at line 404 of file job_api.hpp.
References Dispatch(), ParallelFor(), and WaitOn().
|
staticconstexpr |
Definition at line 29 of file job_queue.hpp.
|
staticconstexpr |
Definition at line 72 of file job_system.cpp.
|
staticconstexpr |
Definition at line 75 of file job_system.cpp.
|
staticconstexpr |
Definition at line 86 of file job_system.cpp.
Referenced by job::TaskPtr::isNull(), and job::TaskPtr::job::TaskPtr().