Veritable Lasagna
An Allocator & Data Structure Library for C.
Loading...
Searching...
No Matches
vl_thread_pool.h
Go to the documentation of this file.
1
14#ifndef VL_THREAD_POOL_H
15#define VL_THREAD_POOL_H
16
17#include "vl_async_queue.h"
18#include "vl_atomic.h"
19#include "vl_condition.h"
20#include "vl_mutex.h"
21#include "vl_numtypes.h"
22#include "vl_semaphore.h"
23#include "vl_thread.h"
24
122typedef enum
123{
124 VL_THREAD_POOL_PRIORITY_HIGH = 0,
125 VL_THREAD_POOL_PRIORITY_MEDIUM = 1,
126 VL_THREAD_POOL_PRIORITY_LOW = 2,
127 VL_THREAD_POOL_PRIORITY_COUNT = 3
128} vl_thread_pool_priority;
129
136
142typedef void (*vl_thread_pool_task_proc)(void* user_data);
143
155
159typedef struct vl_thread_pool_
160{
161 /* Work queues per priority tier */
162 vl_async_queue* workQueues[VL_THREAD_POOL_PRIORITY_COUNT];
163
164 /* Semaphore per tier: counts available work at that priority */
165 vl_semaphore semaphores[VL_THREAD_POOL_PRIORITY_COUNT];
166
167 /* Worker thread management */
168 vl_thread* workers;
170
171 /* State & statistics */
174
175 /* Idle synchronization: for vlThreadPoolWait() */
176 vl_condition all_idle;
180
190typedef struct
191{
193 vl_uint32_t tasksPending[VL_THREAD_POOL_PRIORITY_COUNT];
196
223VL_API vl_thread_pool* vlThreadPoolNew(vl_uint_t worker_count);
224
248VL_API void vlThreadPoolDelete(vl_thread_pool* pool);
249
278VL_API vl_bool_t vlThreadPoolEnqueuePriority(vl_thread_pool* pool, vl_thread_pool_priority priority,
279 const vl_thread_pool_task* task);
280
308VL_API vl_uint_t vlThreadPoolEnqueueBatchPriority(vl_thread_pool* pool, vl_thread_pool_priority priority,
309 const vl_thread_pool_task* tasks, vl_uint_t count);
310
323static inline vl_bool_t vlThreadPoolEnqueue(vl_thread_pool* pool, const vl_thread_pool_task* task)
324{
325 return vlThreadPoolEnqueuePriority(pool, VL_THREAD_POOL_PRIORITY_MEDIUM, task);
326}
327
341static inline vl_uint_t vlThreadPoolEnqueueBatch(vl_thread_pool* pool, const vl_thread_pool_task* tasks,
342 vl_uint_t count)
343{
344 return vlThreadPoolEnqueueBatchPriority(pool, VL_THREAD_POOL_PRIORITY_MEDIUM, tasks, count);
345}
346
374VL_API vl_bool_t vlThreadPoolWait(vl_thread_pool* pool, vl_uint_t timeout_ms);
375
403VL_API void vlThreadPoolShutdown(vl_thread_pool* pool);
404
428VL_API void vlThreadPoolGetStats(vl_thread_pool* pool, vl_thread_pool_stats* out_stats);
429
440static inline vl_uint32_t vlThreadPoolQueueDepth(vl_thread_pool* pool)
441{
443 vlThreadPoolGetStats(pool, &stats);
444 return stats.tasksPending[0] + stats.tasksPending[1] + stats.tasksPending[2];
445}
446
447#endif // VL_THREAD_POOL_H
#define VL_ATOMIC
Definition vl_atomic.h:40
struct vl_mutex_ * vl_mutex
Definition vl_mutex.h:23
VL_UINT_T vl_uint_t
Standard unsigned integer type.
Definition vl_numtypes.h:158
VL_BOOL_T vl_bool_t
Definition vl_numtypes.h:191
VL_ULARGE_T vl_ularge_t
Largest available unsigned integer type.
Definition vl_numtypes.h:136
void * user_data
Definition vl_thread_pool.h:153
vl_uint_t workerCount
Definition vl_thread_pool.h:169
vl_ularge_t tasks_completed
Definition vl_thread_pool.h:192
VL_ATOMIC vl_thread_pool_state state
Definition vl_thread_pool.h:172
VL_API vl_bool_t vlThreadPoolEnqueuePriority(vl_thread_pool *pool, vl_thread_pool_priority priority, const vl_thread_pool_task *task)
Enqueues a single work item at the specified priority level.
Definition vl_thread_pool.c:266
void(* vl_thread_pool_task_proc)(void *user_data)
Function signature for worker thread task procedures.
Definition vl_thread_pool.h:142
vl_thread_pool_task_proc proc
Definition vl_thread_pool.h:152
VL_ATOMIC vl_uint_t active_workers
Definition vl_thread_pool.h:178
VL_API vl_thread_pool * vlThreadPoolNew(vl_uint_t worker_count)
Creates a new priority-aware thread pool with work-stealing.
Definition vl_thread_pool.c:94
VL_API vl_uint_t vlThreadPoolEnqueueBatchPriority(vl_thread_pool *pool, vl_thread_pool_priority priority, const vl_thread_pool_task *tasks, vl_uint_t count)
Enqueues multiple work items at the same priority level in a batch.
Definition vl_thread_pool.c:290
vl_uint_t worker_count
Definition vl_thread_pool.h:194
VL_API void vlThreadPoolGetStats(vl_thread_pool *pool, vl_thread_pool_stats *out_stats)
Retrieves a snapshot of current thread pool statistics.
Definition vl_thread_pool.c:401
vl_mutex idle_lock
Definition vl_thread_pool.h:177
VL_API void vlThreadPoolDelete(vl_thread_pool *pool)
Deletes a thread pool and frees all associated resources.
Definition vl_thread_pool.c:231
VL_ATOMIC vl_ularge_t tasksCompleted
Definition vl_thread_pool.h:173
vl_thread * workers
Definition vl_thread_pool.h:168
VL_API vl_bool_t vlThreadPoolWait(vl_thread_pool *pool, vl_uint_t timeout_ms)
Waits until all enqueued tasks across all priority tiers have completed.
Definition vl_thread_pool.c:318
VL_API void vlThreadPoolShutdown(vl_thread_pool *pool)
Initiates graceful shutdown of the thread pool.
Definition vl_thread_pool.c:375
vl_thread_pool_state
Definition vl_thread_pool.h:131
@ VL_THREAD_POOL_SHUTTING_DOWN
Definition vl_thread_pool.h:133
@ VL_THREAD_POOL_RUNNING
Definition vl_thread_pool.h:132
@ VL_THREAD_POOL_SHUT_DOWN
Definition vl_thread_pool.h:134
vl_uint32_t tasksPending[VL_THREAD_POOL_PRIORITY_COUNT]
Definition vl_thread_pool.h:193
vl_condition all_idle
Definition vl_thread_pool.h:176
Opaque thread pool handle.
Definition vl_thread_pool.h:160
Statistics snapshot from a thread pool.
Definition vl_thread_pool.h:191
A single work item for the thread pool.
Definition vl_thread_pool.h:151