2 * Copyright (c) Meta Platforms, Inc. and affiliates.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
14 #if defined (__cplusplus)
19 #include "zstd_deps.h"
20 #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_customMem */
23 typedef struct POOL_ctx_s POOL_ctx;
26 * Create a thread pool with at most `numThreads` threads.
27 * `numThreads` must be at least 1.
28 * The maximum number of queued jobs before blocking is `queueSize`.
29 * @return : POOL_ctx pointer on success, else NULL.
31 POOL_ctx* POOL_create(size_t numThreads, size_t queueSize);
33 POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize,
34 ZSTD_customMem customMem);
37 * Free a thread pool returned by POOL_create().
39 void POOL_free(POOL_ctx* ctx);
43 * Waits for all queued jobs to finish executing.
45 void POOL_joinJobs(POOL_ctx* ctx);
48 * Expands or shrinks pool's number of threads.
49 * This is more efficient than releasing + creating a new context,
50 * since it tries to preserve and reuse existing threads.
51 * `numThreads` must be at least 1.
52 * @return : 0 when resize was successful,
53 * !0 (typically 1) if there is an error.
54 * note : only numThreads can be resized, queueSize remains unchanged.
56 int POOL_resize(POOL_ctx* ctx, size_t numThreads);
59 * @return threadpool memory usage
60 * note : compatible with NULL (returns 0 in this case)
62 size_t POOL_sizeof(const POOL_ctx* ctx);
65 * The function type that can be added to a thread pool.
67 typedef void (*POOL_function)(void*);
70 * Add the job `function(opaque)` to the thread pool. `ctx` must be valid.
71 * Possibly blocks until there is room in the queue.
72 * Note : The function may be executed asynchronously,
73 * therefore, `opaque` must live until function has been completed.
75 void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque);
79 * Add the job `function(opaque)` to thread pool _if_ a queue slot is available.
80 * Returns immediately even if not (does not block).
81 * @return : 1 if successful, 0 if not.
83 int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque);
86 #if defined (__cplusplus)