tur/threadpool
WorkQueue<T> and ThreadPool for task-based parallelism.
Since: Phase T20-B
WorkQueueHandle
(WorkQueueHandle)
ThreadPoolHandle
(ThreadPoolHandle)
DynThreadPoolHandle
(DynThreadPoolHandle)
FutureHandle
(FutureHandle)
work-queue-new
(work-queue-new :)
create a new unbounded work queue that grows dynamically.
A WorkQueueHandle to the WorkQueueBlock.
(let [q (work-queue-new)] ...) ; => WorkQueueHandle
Since: Phase T20-B
work-queue-new-bounded
(work-queue-new-bounded [cap : int] :)
create a new bounded work queue with a fixed capacity.
| cap | maximum number of items the queue can hold; push blocks when full |
A WorkQueueHandle to the WorkQueueBlock.
(let [q (work-queue-new-bounded 64)] ...) ; => WorkQueueHandle
Since: Phase T20-B
work-queue-push
(work-queue-push [q : WorkQueueHandle v : int] :)
push a value onto the work queue, blocking if the queue is bounded and full.
| q | queue handle returned by work-queue-new or work-queue-new-bounded | |
| v | int64_t value to enqueue |
(work-queue-push q 42)
Since: Phase T20-B
work-queue-pop
(work-queue-pop [q : WorkQueueHandle] :)
pop a value from the work queue, blocking until one is available.
| q | queue handle returned by work-queue-new or work-queue-new-bounded |
The next int64_t value, or INT64_MIN if the queue is closed and empty.
(work-queue-pop q) ; => int (INT64_MIN signals shutdown)
Since: Phase T20-B
work-queue-close
(work-queue-close [q : WorkQueueHandle] :)
close the work queue, waking all blocked producers and consumers.
| q | queue handle returned by work-queue-new or work-queue-new-bounded |
(work-queue-close q)
Since: Phase T20-B
work-queue-free
(work-queue-free [q : WorkQueueHandle] :)
destroy a work queue and release all associated memory.
| q | queue handle returned by work-queue-new or work-queue-new-bounded |
(work-queue-free q)
Since: Phase T20-B
tp-worker
(tp-worker [arg ptr<void>] :)
internal worker thread function for a fixed ThreadPool.
| arg | ptr<void> pointing to the ThreadPoolBlock |
NULL on exit.
Since: Phase T20-D
thread-pool-new
(thread-pool-new [n : int] :)
create a fixed-size thread pool with n worker threads.
| n | number of worker threads to spawn (must be > 0) |
A ThreadPoolHandle to the ThreadPoolBlock.
(let [tp (thread-pool-new 4)] ...) ; => ThreadPoolHandle
Since: Phase T20-D
thread-pool-submit
(thread-pool-submit [tp : ThreadPoolHandle task_fn ptr<void> task_arg ptr<void>] :)
submit a task to the thread pool and return a Future.
| tp | thread pool handle returned by thread-pool-new | |
| task_fn | C function pointer (void *(*)(void *)) to execute on a worker | |
| task_arg | opaque argument forwarded to task_fn |
A FutureHandle to a FutureCell that is fulfilled with task_fn's return value.
(let [fut (thread-pool-submit tp my-fn arg)] ...) ; => FutureHandle
Since: Phase T20-D
thread-pool-shutdown
(thread-pool-shutdown [tp : ThreadPoolHandle] :)
signal all workers to stop and join them.
| tp | thread pool handle returned by thread-pool-new |
(thread-pool-shutdown tp)
Since: Phase T20-D
thread-pool-free
(thread-pool-free [tp : ThreadPoolHandle] :)
free the thread pool (must be called after shutdown).
| tp | thread pool handle returned by thread-pool-new |
(thread-pool-free tp)
Since: Phase T20-D
thread-pool-new-dynamic
(thread-pool-new-dynamic [min_threads : int max_threads : int] :)
create an auto-scaling thread pool between min and max threads.
| min_threads | initial number of worker threads to spawn (must be > 0) | |
| max_threads | upper bound on workers; new threads are added when all are busy |
A DynThreadPoolHandle to the DynThreadPoolBlock.
(let [tp (thread-pool-new-dynamic 2 8)] ...) ; => DynThreadPoolHandle
Since: Phase T20-D
tp-dyn-worker
(tp-dyn-worker [arg ptr<void>] :)
internal worker thread function for a dynamic ThreadPool.
| arg | ptr<void> pointing to the DynThreadPoolBlock |
NULL on exit.
Since: Phase T20-D
thread-pool-dynamic-submit
(thread-pool-dynamic-submit [tp : DynThreadPoolHandle task_fn ptr<void> task_arg ptr<void>] :)
submit a task to the dynamic thread pool, scaling up if needed.
| tp | dynamic thread pool handle returned by thread-pool-new-dynamic | |
| task_fn | C function pointer (void *(*)(void *)) to execute on a worker | |
| task_arg | opaque argument forwarded to task_fn |
A FutureHandle to a FutureCell fulfilled with task_fn's return value.
(let [fut (thread-pool-dynamic-submit tp my-fn arg)] ...) ; => FutureHandle
Since: Phase T20-D
thread-pool-dynamic-shutdown
(thread-pool-dynamic-shutdown [tp : DynThreadPoolHandle] :)
signal all dynamic pool workers to stop and join them.
| tp | dynamic thread pool handle returned by thread-pool-new-dynamic |
(thread-pool-dynamic-shutdown tp)
Since: Phase T20-D
thread-pool-dynamic-free
(thread-pool-dynamic-free [tp : DynThreadPoolHandle] :)
free a dynamic thread pool (must be called after shutdown).
| tp | dynamic thread pool handle returned by thread-pool-new-dynamic |
(thread-pool-dynamic-free tp)
Since: Phase T20-D