zero_pool/
lib.rs

1// Zero-Pool: Ultra-High Performance Thread Pool
2// A thread pool designed for maximum performance through:
3// - Zero-overhead task submission via raw pointers
4// - Result-via-parameters pattern (no result transport)
5// - Per-worker queues to minimize contention
6// - Function pointer dispatch (no trait objects)
7//
8// Safety
9// This library uses raw pointers for maximum performance. You must ensure:
10// - Parameter structs live until `future.wait()` completes
11// - Result pointers remain valid until task completion
12// - No data races in your task functions
13mod future;
14mod macros;
15mod padded_type;
16mod pool;
17mod queue;
18mod work_batch;
19mod worker;
20
21use std::num::NonZeroUsize;
22
23pub use pool::ThreadPool;
24
25// task function signature, takes raw pointer to parameters
26pub type TaskFnPointer = fn(*const ());
27
28// pointer to task parameter struct
29pub type TaskParamPointer = *const ();
30
31// tuple of the two creates one work item
32pub type WorkItem = (TaskFnPointer, TaskParamPointer);
33
34// convenience function to create a new thread pool
35pub fn new() -> ThreadPool {
36    let worker_count = std::thread::available_parallelism()
37        .map(NonZeroUsize::get)
38        .unwrap_or(1);
39    ThreadPool::new(worker_count)
40}
41
42// create thread pool with specific worker count
43pub fn with_workers(worker_count: usize) -> ThreadPool {
44    ThreadPool::new(worker_count)
45}
46
47// Convert uniform tasks to pointer format
48#[inline]
49pub fn uniform_tasks_to_pointers<T>(task_fn: TaskFnPointer, params_vec: &[T]) -> Vec<WorkItem> {
50    params_vec
51        .iter()
52        .map(|params| (task_fn, params as *const T as TaskParamPointer))
53        .collect()
54}