Persistent pthread thread pool for CK-Engine inference. More...
#include <stdint.h>#include <stdatomic.h>#include <pthread.h>Go to the source code of this file.
Macros | |
| #define | CK_CACHE_LINE 64 |
| #define | CK_THREADPOOL_MAX_THREADS 64 |
| #define | CK_THREADPOOL_SPIN_COUNT 1024 |
Typedefs | |
| typedef void(* | ck_work_fn_t) (int ith, int nth, void *args) |
Functions | |
| void | ck_threadpool_barrier (ck_threadpool_t *pool) |
| ck_threadpool_t * | ck_threadpool_create (int n_threads) |
| void | ck_threadpool_destroy (ck_threadpool_t *pool) |
| void | ck_threadpool_dispatch (ck_threadpool_t *pool, ck_work_fn_t fn, void *args) |
| ck_threadpool_t * | ck_threadpool_global (void) |
| void | ck_threadpool_global_destroy (void) |
| int | ck_threadpool_n_threads (const ck_threadpool_t *pool) |
| void | ck_threadpool_pause (ck_threadpool_t *pool) |
| void | ck_threadpool_resume (ck_threadpool_t *pool) |
| int | ck_threadpool_thread_id (const ck_threadpool_t *pool) |
Persistent pthread thread pool for CK-Engine inference.
Design goals:
Usage: ck_threadpool_t *pool = ck_threadpool_create(4); // 4 threads total
// In decode loop: ck_threadpool_dispatch(pool, my_work_fn, args); // my_work_fn called on all threads with (ith, nth, args)
// Between batches: ck_threadpool_pause(pool); // workers sleep (0% CPU) ck_threadpool_resume(pool); // wake workers
ck_threadpool_destroy(pool);
Architecture: STARTUP: Main creates N-1 worker pthreads, all spin on atomic counter DISPATCH: Main writes work desc, bumps counter, all threads execute BARRIER: Atomic counter + spin-wait with _mm_pause() PAUSE: Workers sleep on pthread_cond_t (0% CPU between batches)
Definition in file ck_threadpool.h.
| #define CK_CACHE_LINE 64 |
Cache line size for alignment (x86-64)
Definition at line 54 of file ck_threadpool.h.
| #define CK_THREADPOOL_MAX_THREADS 64 |
Maximum threads supported (main + workers)
Definition at line 48 of file ck_threadpool.h.
| #define CK_THREADPOOL_SPIN_COUNT 1024 |
Number of spin iterations before falling back to condvar wait
Definition at line 51 of file ck_threadpool.h.
| typedef void(* ck_work_fn_t) (int ith, int nth, void *args) |
Work function signature. Called on ALL threads (including main thread 0).
| ith | Thread index (0 = main thread) |
| nth | Total number of threads |
| args | Opaque argument pointer (set via dispatch) |
Definition at line 68 of file ck_threadpool.h.
| void ck_threadpool_barrier | ( | ck_threadpool_t * | pool | ) |
Barrier synchronization within a dispatched work function.
ALL threads must call this at the same point. Threads spin-wait until all have arrived, then proceed.
Must only be called from within a work function (during dispatch).
| pool | Thread pool |
Definition at line 320 of file ck_threadpool.c.
References barrier_wait().
| ck_threadpool_t* ck_threadpool_create | ( | int | n_threads | ) |
Create a thread pool with n_threads total threads. Thread 0 is the calling (main) thread; n_threads-1 workers are spawned.
| n_threads | Total thread count (including main). Must be >= 1. Pass 0 for auto-detect (physical cores). |
Definition at line 183 of file ck_threadpool.c.
References barrier_init(), CK_CACHE_LINE, ck_get_physical_cores(), CK_THREADPOOL_MAX_THREADS, and worker_main().
Referenced by global_pool_init().
| void ck_threadpool_destroy | ( | ck_threadpool_t * | pool | ) |
Destroy the thread pool. Signals all workers to exit and joins them. Safe to call with NULL.
Definition at line 243 of file ck_threadpool.c.
Referenced by ck_threadpool_global_destroy().
| void ck_threadpool_dispatch | ( | ck_threadpool_t * | pool, |
| ck_work_fn_t | fn, | ||
| void * | args | ||
| ) |
Dispatch work to all threads and wait for completion.
This is a blocking call — returns when ALL threads have finished.
| pool | Thread pool |
| fn | Work function (called on each thread) |
| args | Argument passed to fn |
Definition at line 271 of file ck_threadpool.c.
References barrier_init(), CK_SPIN_PAUSE, and CK_THREADPOOL_SPIN_COUNT.
| ck_threadpool_t* ck_threadpool_global | ( | void | ) |
Get or create the global thread pool. Thread-safe (uses pthread_once internally). Uses ck_get_num_threads() for auto-detection.
Definition at line 383 of file ck_threadpool.c.
References g_threadpool, g_threadpool_once, and global_pool_init().
Referenced by ck_get_threadpool(), and ck_threadpool_init().
| void ck_threadpool_global_destroy | ( | void | ) |
Destroy the global thread pool. Called during engine shutdown.
Definition at line 389 of file ck_threadpool.c.
References ck_threadpool_destroy(), g_threadpool, and g_threadpool_once.
Referenced by ck_threadpool_shutdown().
| int ck_threadpool_n_threads | ( | const ck_threadpool_t * | pool | ) |
Get total thread count (including main thread)
Definition at line 351 of file ck_threadpool.c.
| void ck_threadpool_pause | ( | ck_threadpool_t * | pool | ) |
Pause workers — they sleep on condvar (0% CPU). Call between batches or during interactive waiting. Workers wake on next dispatch or resume.
Definition at line 330 of file ck_threadpool.c.
| void ck_threadpool_resume | ( | ck_threadpool_t * | pool | ) |
Resume workers — transition from sleep to spin-wait. Call before starting a new batch of work.
Definition at line 336 of file ck_threadpool.c.
| int ck_threadpool_thread_id | ( | const ck_threadpool_t * | pool | ) |
Get thread index for current thread (0 = main, -1 if not in pool)
Definition at line 356 of file ck_threadpool.c.