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"
17 enum class JobPriority : uint8_t {
23 static inline constexpr uint32_t JobPriorityCnt = 2;
25 enum JobCreationFlags : uint8_t {
41 JobPriority priority = JobPriority::High;
42 JobCreationFlags flags = JobCreationFlags::Default;
52 static constexpr uint32_t BufferSize = 24;
54 enum class Op : uint8_t { Invoke, Destroy, Move };
57 OpFn m_func =
nullptr;
58 alignas(std::max_align_t) uint8_t m_storage[BufferSize];
61 if (m_func !=
nullptr) {
62 m_func(Op::Destroy,
this,
nullptr,
nullptr);
69 using Fn = std::decay_t<F>;
70 static_assert(std::is_invocable_r_v<void, Fn&, const JobArgs&>,
"JobArgsFunc requires a compatible callable");
71 static_assert(std::is_move_constructible_v<Fn>,
"Callable must be move-constructible");
73 alignof(Fn) <=
alignof(std::max_align_t),
"Over-aligned callables are not supported for JobArgsFunc");
75 if constexpr (
sizeof(Fn) <= BufferSize) {
76 new (m_storage) Fn(GAIA_FWD(f));
79 auto* pFn =
reinterpret_cast<Fn*
>(dst->m_storage);
82 GAIA_ASSERT(pArgs !=
nullptr);
86 if constexpr (!std::is_trivially_destructible_v<Fn>)
90 GAIA_ASSERT(src !=
nullptr);
91 auto* pSrcFn =
reinterpret_cast<Fn*
>(src->m_storage);
92 new (dst->m_storage) Fn(GAIA_MOV(*pSrcFn));
93 if constexpr (!std::is_trivially_destructible_v<Fn>)
95 dst->m_func = src->m_func;
96 src->m_func =
nullptr;
102 auto* pStorage = mem::AllocHelper::alloc<Fn>();
103 GAIA_ASSERT((uintptr_t)pStorage %
alignof(Fn) == 0);
104 auto* pFunc =
new (pStorage) Fn(GAIA_FWD(f));
105 *
reinterpret_cast<Fn**
>(m_storage) = pFunc;
108 auto*& pFn = *
reinterpret_cast<Fn**
>(dst->m_storage);
111 GAIA_ASSERT(pArgs !=
nullptr);
112 GAIA_ASSERT(pFn !=
nullptr);
116 GAIA_ASSERT(pFn !=
nullptr);
117 if constexpr (!std::is_trivially_destructible_v<Fn>)
119 mem::AllocHelper::free(pFn);
123 GAIA_ASSERT(src !=
nullptr);
124 *
reinterpret_cast<Fn**
>(dst->m_storage) = *
reinterpret_cast<Fn**
>(src->m_storage);
125 dst->m_func = src->m_func;
126 *
reinterpret_cast<Fn**
>(src->m_storage) =
nullptr;
127 src->m_func =
nullptr;
144 if (other.m_func !=
nullptr)
145 other.m_func(Op::Move,
this, &other,
nullptr);
149 if (
this != &other) {
151 if (other.m_func !=
nullptr)
152 other.m_func(Op::Move,
this, &other,
nullptr);
157 template <
typename F,
typename = std::enable_if_t<!std::is_same_v<std::decay_t<F>, JobArgsFunc>>>
162 template <
typename F,
typename = std::enable_if_t<!std::is_same_v<std::decay_t<F>, JobArgsFunc>>>
169 template <
typename F>
172 func.init(GAIA_FWD(f));
176 void exec(
const JobArgs& args)
const {
177 GAIA_ASSERT(m_func !=
nullptr);
178 m_func(Op::Invoke,
const_cast<JobArgsFunc*
>(
this),
nullptr, &args);
181 void operator()(
const JobArgs& args)
const {
189 explicit operator bool()
const {
190 return m_func !=
nullptr;
196 JobPriority priority = JobPriority::High;
202 void* pCtx =
nullptr;
203 void (*invoke)(
void*,
const JobArgs&) =
nullptr;
204 JobPriority priority = JobPriority::High;
Manual-reset style synchronization primitive for waking a waiting thread. The event stays signaled un...
Definition event.h:20
Move-only callback wrapper specialized for parallel job ranges.
Definition jobcommon.h:51
Definition threadpool.h:87
Move-only function wrapper with inline storage and optional SmallBlockAllocator spill storage....
Definition small_func.h:19
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9
Definition jobcommon.h:35
Definition jobcommon.h:45
Non-owning callback descriptor for parallel jobs.
Definition jobcommon.h:201
Definition jobcommon.h:194
Definition jobcommon.h:39
Definition jobcommon.h:209
bool background
True when the worker executes background jobs.
Definition jobcommon.h:217
JobPriority prio
Job priority.
Definition jobcommon.h:215
uint32_t workerIdx
Worker index.
Definition jobcommon.h:213
Event event
Event signaled when a job is executed.
Definition jobcommon.h:221
ThreadPool * tp
Thread pool pointer.
Definition jobcommon.h:211
bool threadCreated
True when the worker thread has been successfully created.
Definition jobcommon.h:219
JobQueue< 512 > jobQueue
Lock-free work stealing queue for the jobs.
Definition jobcommon.h:223