/*1* Copyright (c) Meta Platforms, Inc. and affiliates.2* All rights reserved.3*4* This source code is licensed under both the BSD-style license (found in the5* LICENSE file in the root directory of this source tree) and the GPLv2 (found6* in the COPYING file in the root directory of this source tree).7* You may select, at your option, one of the above-listed licenses.8*/910#ifndef POOL_H11#define POOL_H1213#if defined (__cplusplus)14extern "C" {15#endif161718#include "zstd_deps.h"19#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_customMem */20#include "../zstd.h"2122typedef struct POOL_ctx_s POOL_ctx;2324/*! POOL_create() :25* Create a thread pool with at most `numThreads` threads.26* `numThreads` must be at least 1.27* The maximum number of queued jobs before blocking is `queueSize`.28* @return : POOL_ctx pointer on success, else NULL.29*/30POOL_ctx* POOL_create(size_t numThreads, size_t queueSize);3132POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize,33ZSTD_customMem customMem);3435/*! POOL_free() :36* Free a thread pool returned by POOL_create().37*/38void POOL_free(POOL_ctx* ctx);394041/*! POOL_joinJobs() :42* Waits for all queued jobs to finish executing.43*/44void POOL_joinJobs(POOL_ctx* ctx);4546/*! POOL_resize() :47* Expands or shrinks pool's number of threads.48* This is more efficient than releasing + creating a new context,49* since it tries to preserve and re-use existing threads.50* `numThreads` must be at least 1.51* @return : 0 when resize was successful,52* !0 (typically 1) if there is an error.53* note : only numThreads can be resized, queueSize remains unchanged.54*/55int POOL_resize(POOL_ctx* ctx, size_t numThreads);5657/*! POOL_sizeof() :58* @return threadpool memory usage59* note : compatible with NULL (returns 0 in this case)60*/61size_t POOL_sizeof(const POOL_ctx* ctx);6263/*! POOL_function :64* The function type that can be added to a thread pool.65*/66typedef void (*POOL_function)(void*);6768/*! POOL_add() :69* Add the job `function(opaque)` to the thread pool. `ctx` must be valid.70* Possibly blocks until there is room in the queue.71* Note : The function may be executed asynchronously,72* therefore, `opaque` must live until function has been completed.73*/74void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque);757677/*! POOL_tryAdd() :78* Add the job `function(opaque)` to thread pool _if_ a queue slot is available.79* Returns immediately even if not (does not block).80* @return : 1 if successful, 0 if not.81*/82int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque);838485#if defined (__cplusplus)86}87#endif8889#endif909192