Mercurial
diff seobeo/seobeo_worker.h @ 250:745fd127b2a1
[seobeo] Add bounded worker interface
Co-authored-by: Copilot <[email protected]>
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Tue, 04 Aug 2026 06:23:37 -0700 |
| parents | |
| children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/seobeo/seobeo_worker.h Tue Aug 04 06:23:37 2026 -0700 @@ -0,0 +1,72 @@ +#ifndef SEOBEO_WORKER_H +#define SEOBEO_WORKER_H + +#include "dowa/dowa.h" + +typedef void (*Seobeo_Work_Function)(void *p_context); +typedef void (*Seobeo_Work_Cleanup)(void *p_context); + +typedef enum { + SEOBEO_WORKER_OK = 0, + SEOBEO_WORKER_INVALID_ARGUMENT, + SEOBEO_WORKER_OUT_OF_MEMORY, + SEOBEO_WORKER_THREAD_ERROR, + SEOBEO_WORKER_QUEUE_FULL, + SEOBEO_WORKER_STOPPED, +} Seobeo_Worker_Result; + +typedef struct Seobeo_Thread Seobeo_Thread; +typedef struct Seobeo_Worker_Pool Seobeo_Worker_Pool; + +/* + * Start one joinable task. Join consumes and destroys the thread handle. + * The optional cleanup function runs after the work function. + */ +Seobeo_Thread *Seobeo_Thread_Start( + Seobeo_Work_Function function, + void *p_context, + Seobeo_Work_Cleanup cleanup); +Seobeo_Worker_Result Seobeo_Thread_Join(Seobeo_Thread *p_thread); + +/* + * Start a detached task. On success, Seobeo owns the context until the work + * and optional cleanup functions finish. + */ +Seobeo_Worker_Result Seobeo_Thread_Start_Detached( + Seobeo_Work_Function function, + void *p_context, + Seobeo_Work_Cleanup cleanup); + +uint64 Seobeo_Thread_Current_Id(void); + +/* + * Create a bounded reusable pool. Submit is non-blocking and transfers context + * ownership only when it returns SEOBEO_WORKER_OK. + */ +Seobeo_Worker_Pool *Seobeo_Worker_Pool_Create( + uint32 worker_count, + uint32 queue_capacity); +Seobeo_Worker_Result Seobeo_Worker_Pool_Submit( + Seobeo_Worker_Pool *p_pool, + Seobeo_Work_Function function, + void *p_context, + Seobeo_Work_Cleanup cleanup); +Seobeo_Worker_Result Seobeo_Worker_Pool_Wait( + Seobeo_Worker_Pool *p_pool); + +/* + * Stop accepting work. When drain is TRUE, queued tasks finish. When FALSE, + * queued tasks are discarded and their cleanup functions run. + * Wait, Shutdown, and Destroy must not be called from a task or cleanup + * callback belonging to the same pool. Wait and Shutdown reject those calls; + * Destroy leaves the pool unchanged. + */ +Seobeo_Worker_Result Seobeo_Worker_Pool_Shutdown( + Seobeo_Worker_Pool *p_pool, + boolean drain); +void Seobeo_Worker_Pool_Destroy(Seobeo_Worker_Pool *p_pool); + +uint32 Seobeo_Worker_Pool_Pending(Seobeo_Worker_Pool *p_pool); +uint32 Seobeo_Worker_Pool_Active(Seobeo_Worker_Pool *p_pool); + +#endif