comparison 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
comparison
equal deleted inserted replaced
249:c5129452493e 250:745fd127b2a1
1 #ifndef SEOBEO_WORKER_H
2 #define SEOBEO_WORKER_H
3
4 #include "dowa/dowa.h"
5
6 typedef void (*Seobeo_Work_Function)(void *p_context);
7 typedef void (*Seobeo_Work_Cleanup)(void *p_context);
8
9 typedef enum {
10 SEOBEO_WORKER_OK = 0,
11 SEOBEO_WORKER_INVALID_ARGUMENT,
12 SEOBEO_WORKER_OUT_OF_MEMORY,
13 SEOBEO_WORKER_THREAD_ERROR,
14 SEOBEO_WORKER_QUEUE_FULL,
15 SEOBEO_WORKER_STOPPED,
16 } Seobeo_Worker_Result;
17
18 typedef struct Seobeo_Thread Seobeo_Thread;
19 typedef struct Seobeo_Worker_Pool Seobeo_Worker_Pool;
20
21 /*
22 * Start one joinable task. Join consumes and destroys the thread handle.
23 * The optional cleanup function runs after the work function.
24 */
25 Seobeo_Thread *Seobeo_Thread_Start(
26 Seobeo_Work_Function function,
27 void *p_context,
28 Seobeo_Work_Cleanup cleanup);
29 Seobeo_Worker_Result Seobeo_Thread_Join(Seobeo_Thread *p_thread);
30
31 /*
32 * Start a detached task. On success, Seobeo owns the context until the work
33 * and optional cleanup functions finish.
34 */
35 Seobeo_Worker_Result Seobeo_Thread_Start_Detached(
36 Seobeo_Work_Function function,
37 void *p_context,
38 Seobeo_Work_Cleanup cleanup);
39
40 uint64 Seobeo_Thread_Current_Id(void);
41
42 /*
43 * Create a bounded reusable pool. Submit is non-blocking and transfers context
44 * ownership only when it returns SEOBEO_WORKER_OK.
45 */
46 Seobeo_Worker_Pool *Seobeo_Worker_Pool_Create(
47 uint32 worker_count,
48 uint32 queue_capacity);
49 Seobeo_Worker_Result Seobeo_Worker_Pool_Submit(
50 Seobeo_Worker_Pool *p_pool,
51 Seobeo_Work_Function function,
52 void *p_context,
53 Seobeo_Work_Cleanup cleanup);
54 Seobeo_Worker_Result Seobeo_Worker_Pool_Wait(
55 Seobeo_Worker_Pool *p_pool);
56
57 /*
58 * Stop accepting work. When drain is TRUE, queued tasks finish. When FALSE,
59 * queued tasks are discarded and their cleanup functions run.
60 * Wait, Shutdown, and Destroy must not be called from a task or cleanup
61 * callback belonging to the same pool. Wait and Shutdown reject those calls;
62 * Destroy leaves the pool unchanged.
63 */
64 Seobeo_Worker_Result Seobeo_Worker_Pool_Shutdown(
65 Seobeo_Worker_Pool *p_pool,
66 boolean drain);
67 void Seobeo_Worker_Pool_Destroy(Seobeo_Worker_Pool *p_pool);
68
69 uint32 Seobeo_Worker_Pool_Pending(Seobeo_Worker_Pool *p_pool);
70 uint32 Seobeo_Worker_Pool_Active(Seobeo_Worker_Pool *p_pool);
71
72 #endif