9#include "gaia/core/utility.h"
10#include "gaia/mem/mem_alloc.h"
11#include "gaia/mt/event.h"
12#include "gaia/mt/jobqueue.h"
13#include "gaia/util/small_func.h"
18 enum class JobPriority : uint8_t {
25 static inline constexpr uint32_t JobPriorityCnt = 2;
28 enum JobCreationFlags : uint8_t {
52 JobCreationFlags
flags = JobCreationFlags::Default;
65 static constexpr uint32_t BufferSize = 24;
67 enum class Op : uint8_t { Invoke, Destroy, Move };
70 OpFn m_func =
nullptr;
71 alignas(std::max_align_t) uint8_t m_storage[BufferSize];
74 if (m_func !=
nullptr) {
75 m_func(Op::Destroy,
this,
nullptr,
nullptr);
82 using Fn = std::decay_t<F>;
83 static_assert(std::is_invocable_r_v<void, Fn&, const JobArgs&>,
"JobArgsFunc requires a compatible callable");
84 static_assert(std::is_move_constructible_v<Fn>,
"Callable must be move-constructible");
86 alignof(Fn) <=
alignof(std::max_align_t),
"Over-aligned callables are not supported for JobArgsFunc");
88 if constexpr (
sizeof(Fn) <= BufferSize) {
89 new (m_storage) Fn(GAIA_FWD(f));
92 auto* pFn =
reinterpret_cast<Fn*
>(dst->m_storage);
95 GAIA_ASSERT(pArgs !=
nullptr);
99 if constexpr (!std::is_trivially_destructible_v<Fn>)
103 GAIA_ASSERT(src !=
nullptr);
104 auto* pSrcFn =
reinterpret_cast<Fn*
>(src->m_storage);
105 new (dst->m_storage) Fn(GAIA_MOV(*pSrcFn));
106 if constexpr (!std::is_trivially_destructible_v<Fn>)
108 dst->m_func = src->m_func;
109 src->m_func =
nullptr;
115 auto* pStorage = mem::AllocHelper::alloc<Fn>();
116 GAIA_ASSERT((uintptr_t)pStorage %
alignof(Fn) == 0);
117 auto* pFunc =
new (pStorage) Fn(GAIA_FWD(f));
118 *
reinterpret_cast<Fn**
>(m_storage) = pFunc;
121 auto*& pFn = *
reinterpret_cast<Fn**
>(dst->m_storage);
124 GAIA_ASSERT(pArgs !=
nullptr);
125 GAIA_ASSERT(pFn !=
nullptr);
129 GAIA_ASSERT(pFn !=
nullptr);
130 if constexpr (!std::is_trivially_destructible_v<Fn>)
136 GAIA_ASSERT(src !=
nullptr);
137 *
reinterpret_cast<Fn**
>(dst->m_storage) = *
reinterpret_cast<Fn**
>(src->m_storage);
138 dst->m_func = src->m_func;
139 *
reinterpret_cast<Fn**
>(src->m_storage) =
nullptr;
140 src->m_func =
nullptr;
157 if (other.m_func !=
nullptr)
158 other.m_func(Op::Move,
this, &other,
nullptr);
162 if (
this != &other) {
164 if (other.m_func !=
nullptr)
165 other.m_func(Op::Move,
this, &other,
nullptr);
170 template <
typename F,
typename = std::enable_if_t<!std::is_same_v<std::decay_t<F>, JobArgsFunc>>>
175 template <
typename F,
typename = std::enable_if_t<!std::is_same_v<std::decay_t<F>, JobArgsFunc>>>
182 template <
typename F>
185 func.init(GAIA_FWD(f));
189 void exec(
const JobArgs& args)
const {
190 GAIA_ASSERT(m_func !=
nullptr);
191 m_func(Op::Invoke,
const_cast<JobArgsFunc*
>(
this),
nullptr, &args);
194 void operator()(
const JobArgs& args)
const {
202 explicit operator bool()
const {
203 return m_func !=
nullptr;
Auto-reset synchronization primitive for waking one waiting thread. A successful wait consumes the si...
Definition event.h:20
Move-only callback wrapper specialized for parallel job ranges.
Definition jobcommon.h:64
Process-wide worker pool for dependent frame and background jobs.
Definition threadpool.h:88
Move-only function wrapper with inline storage for small callables. Callables too large for the inlin...
Definition small_func.h:19
static void free(void *ptr)
Releases storage allocated through an adaptor.
Definition mem_alloc.h:276
Allocation metadata used when reserving a job slot.
Definition jobcommon.h:40
JobPriority priority
Priority encoded into the allocated job handle.
Definition jobcommon.h:42
Half-open item range passed to a parallel job callback.
Definition jobcommon.h:56
uint32_t idxStart
First item index processed by this invocation.
Definition jobcommon.h:58
uint32_t idxEnd
One-past-the-last item index processed by this invocation.
Definition jobcommon.h:60
Non-owning callback descriptor for parallel jobs.
Definition jobcommon.h:217
void(* invoke)(void *, const JobArgs &)
Function that invokes the callback stored in the context.
Definition jobcommon.h:221
void * pCtx
Non-owning callback context.
Definition jobcommon.h:219
JobPriority priority
Queue priority used for each range job.
Definition jobcommon.h:223
Callable and priority for a range-partitioned parallel job.
Definition jobcommon.h:208
JobPriority priority
Queue priority used for each range job.
Definition jobcommon.h:212
JobArgsFunc func
Callable invoked once for each scheduled range.
Definition jobcommon.h:210
Callable and scheduling options for a single job.
Definition jobcommon.h:46
JobCreationFlags flags
Creation and lifetime options.
Definition jobcommon.h:52
JobPriority priority
Queue priority used to schedule the callable.
Definition jobcommon.h:50
util::SmallFunc func
Callable executed by the worker.
Definition jobcommon.h:48
Per-thread execution state owned by ThreadPool.
Definition jobcommon.h:229
bool background
True when the worker executes background jobs.
Definition jobcommon.h:237
JobPriority prio
Job priority.
Definition jobcommon.h:235
uint32_t workerIdx
Worker index.
Definition jobcommon.h:233
Event event
Event signaled when a job is executed.
Definition jobcommon.h:241
ThreadPool * tp
Thread pool pointer.
Definition jobcommon.h:231
bool threadCreated
True when the worker thread has been successfully created.
Definition jobcommon.h:239
JobQueue< 512 > jobQueue
Lock-free work stealing queue for the jobs.
Definition jobcommon.h:243