/*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_H121314#include "zstd_deps.h"15#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_customMem */16#include "../zstd.h"1718typedef struct POOL_ctx_s POOL_ctx;1920/*! POOL_create() :21* Create a thread pool with at most `numThreads` threads.22* `numThreads` must be at least 1.23* The maximum number of queued jobs before blocking is `queueSize`.24* @return : POOL_ctx pointer on success, else NULL.25*/26POOL_ctx* POOL_create(size_t numThreads, size_t queueSize);2728POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize,29ZSTD_customMem customMem);3031/*! POOL_free() :32* Free a thread pool returned by POOL_create().33*/34void POOL_free(POOL_ctx* ctx);353637/*! POOL_joinJobs() :38* Waits for all queued jobs to finish executing.39*/40void POOL_joinJobs(POOL_ctx* ctx);4142/*! POOL_resize() :43* Expands or shrinks pool's number of threads.44* This is more efficient than releasing + creating a new context,45* since it tries to preserve and reuse existing threads.46* `numThreads` must be at least 1.47* @return : 0 when resize was successful,48* !0 (typically 1) if there is an error.49* note : only numThreads can be resized, queueSize remains unchanged.50*/51int POOL_resize(POOL_ctx* ctx, size_t numThreads);5253/*! POOL_sizeof() :54* @return threadpool memory usage55* note : compatible with NULL (returns 0 in this case)56*/57size_t POOL_sizeof(const POOL_ctx* ctx);5859/*! POOL_function :60* The function type that can be added to a thread pool.61*/62typedef void (*POOL_function)(void*);6364/*! POOL_add() :65* Add the job `function(opaque)` to the thread pool. `ctx` must be valid.66* Possibly blocks until there is room in the queue.67* Note : The function may be executed asynchronously,68* therefore, `opaque` must live until function has been completed.69*/70void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque);717273/*! POOL_tryAdd() :74* Add the job `function(opaque)` to thread pool _if_ a queue slot is available.75* Returns immediately even if not (does not block).76* @return : 1 if successful, 0 if not.77*/78int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque);7980#endif818283