Something went wrong. Try again.
fork of gitlab.com/devnw/zig/thread-pool for zig 0.17
Something went wrong. Try again.
5.2 kB · 132 lines
Markdown
at main
API Reference #
This page summarizes the public surface exported from src/root.zig.
Primary Types #
thread_pool.ThreadPool: owned pool handle with submission, waiting, shutdown, and stats methodsthread_pool.Config: pool configurationthread_pool.SubmitOptions: per-submit behavior such as backpressure, lane, waitability, and timeoutthread_pool.TaskHandle: opaque handle returned by waitable submissionsthread_pool.WaitGroup: task-group completion primitivethread_pool.CancellationToken: cooperative cancellation flagthread_pool.Stats: snapshot of task counts, latencies, queue depths, memory, and worker-local stats
ThreadPool Methods #
ThreadPool.init(allocator, config): create a poolThreadPool.deinit(): shut down and release all owned resourcesThreadPool.submit(task_fn, ctx, options): enqueue a single task, optionally blocking on backpressureThreadPool.trySubmit(task_fn, ctx, options): never blocks; returnserror.QueueFullorerror.TaskPoolExhaustedinsteadThreadPool.submitBatch(tasks, options): enqueue a slice ofthread_pool.BatchTaskThreadPool.wait(handle): wait for a handle created withSubmitOptions.waitable = trueThreadPool.waitGroup(): create a fresh wait group for grouping submissionsThreadPool.shutdown(mode, timeout_ns): stop the pool with either drain or cancel semanticsThreadPool.statsSnapshot(): read a consistent stats viewThreadPool.purgeWorkerCaches(): purge per-worker scratch buffers when the pool is quiescentThreadPool.registerSubmitter(): claim a per-submitter queue slot for this thread to enable the per-submitter fast path on dedicated submitter threads. Returns!usize(the slot index) orerror.NoSubmitterSlotswhen the 64-slot bitmap is full. Idempotent per-thread — callers should pair eachregisterSubmitter()with exactly onereleaseSubmitter()per registration. TLS state is pool-scoped (F-001) so the same thread can safely register on multiple pools.ThreadPool.releaseSubmitter(idx): release a previously-registered submitter slot. Idempotent (F-005) — a double-release on the sameidxbails via an atomic CAS on the free-mask rather than racing the drain. Out-of-rangeidxis silently ignored. Only clears the caller thread's TLS slot if this thread was the original owner.
Config Highlights #
Common fields:
min_threads,max_threadsqueue_capacity,local_queue_capacityinject_batch_sizescheduler_variantallow_blocking_lane,blocking_workersworker_scratch_cap_bytesidle_park_after_ns,idle_purge_after_nstimestamp_modesteal_batch_size
Important constraint: Config.validate() is intended for normalized configs.
If you are validating a hand-built config outside ThreadPool.init(), call
config.normalized().validate() rather than validating the raw struct.
Scheduler Variants #
global_queuework_stealinglane_extension
Submit Options #
SubmitOptions supports:
waitablewait_groupcancellation_tokenbackpressuretimeout_nslane
Backpressure policies:
return_errorblockblock_with_timeout
Task lanes:
defaultblocking
Queue Implementations #
Public queue types are exported for low-level testing and experimentation:
thread_pool.BoundedRingthread_pool.WorkDequethread_pool.RingQueue
Stats Snapshot #
Stats includes:
- lifecycle counts:
submitted,started,completed,cancelled - queue state:
queue_depth_current,queue_depth_peak - worker activity:
steals,steal_failures,wakeups,parks - memory: process RSS plus pool allocation counters
- latencies: task wait and execution summaries
- per-worker scratch and queue stats
See DESIGN.md for the implementation-level meaning of each field.
Build Options #
Passed as -D<name>[=<value>] to zig build. All are optional; defaults
are the safe production settings.
| Option | Default | Purpose |
|---|---|---|
-Dtsan |
false |
Enable ThreadSanitizer on unit, integration, stress, and fuzz artifacts. Forces the stress suite from ReleaseFast to Debug so races are not optimised away before TSan observes them. |
-Dsingle-pool |
false |
Elide the F-001 per-pool TLS identity check on the hot submit path at compile time. UNSAFE when a process hosts more than one ThreadPool — restores the pre-F-001 cross-pool OOB hazard. Reclaims 6–17% throughput on contended_spawn_8x / cpu_work_100us / mixed_fast_slow for genuinely single-pool workloads. See AppSec FINAL.md § Optimisation pass. |
-Doptimize={Debug,ReleaseSafe,ReleaseFast,ReleaseSmall} |
Debug |
Standard Zig optimisation mode. Benchmarks are configured to always build under ReleaseFast regardless of this flag. |
Two build steps surface TSan directly:
zig build test-all -Dtsan— all test suites under TSan (Debug only).zig build fuzz -Dtsan [-Dsingle-pool=true]— fuzz harness under TSan; drive iteration count withFUZZ_SEED/FUZZ_ITERATIONSenv vars.