1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only 2 /* 3 * Copyright (c) Meta Platforms, Inc. and affiliates. 4 * All rights reserved. 5 * 6 * This source code is licensed under both the BSD-style license (found in the 7 * LICENSE file in the root directory of this source tree) and the GPLv2 (found 8 * in the COPYING file in the root directory of this source tree). 9 * You may select, at your option, one of the above-listed licenses. 10 */ 11 12 #ifndef POOL_H 13 #define POOL_H 14 15 16 #include "zstd_deps.h" 17 #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_customMem */ 18 #include "../zstd.h" 19 20 typedef struct POOL_ctx_s POOL_ctx; 21 22 /*! POOL_create() : 23 * Create a thread pool with at most `numThreads` threads. 24 * `numThreads` must be at least 1. 25 * The maximum number of queued jobs before blocking is `queueSize`. 26 * @return : POOL_ctx pointer on success, else NULL. 27 */ 28 POOL_ctx* POOL_create(size_t numThreads, size_t queueSize); 29 30 POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize, 31 ZSTD_customMem customMem); 32 33 /*! POOL_free() : 34 * Free a thread pool returned by POOL_create(). 35 */ 36 void POOL_free(POOL_ctx* ctx); 37 38 39 /*! POOL_joinJobs() : 40 * Waits for all queued jobs to finish executing. 41 */ 42 void POOL_joinJobs(POOL_ctx* ctx); 43 44 /*! POOL_resize() : 45 * Expands or shrinks pool's number of threads. 46 * This is more efficient than releasing + creating a new context, 47 * since it tries to preserve and reuse existing threads. 48 * `numThreads` must be at least 1. 49 * @return : 0 when resize was successful, 50 * !0 (typically 1) if there is an error. 51 * note : only numThreads can be resized, queueSize remains unchanged. 52 */ 53 int POOL_resize(POOL_ctx* ctx, size_t numThreads); 54 55 /*! POOL_sizeof() : 56 * @return threadpool memory usage 57 * note : compatible with NULL (returns 0 in this case) 58 */ 59 size_t POOL_sizeof(const POOL_ctx* ctx); 60 61 /*! POOL_function : 62 * The function type that can be added to a thread pool. 63 */ 64 typedef void (*POOL_function)(void*); 65 66 /*! POOL_add() : 67 * Add the job `function(opaque)` to the thread pool. `ctx` must be valid. 68 * Possibly blocks until there is room in the queue. 69 * Note : The function may be executed asynchronously, 70 * therefore, `opaque` must live until function has been completed. 71 */ 72 void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque); 73 74 75 /*! POOL_tryAdd() : 76 * Add the job `function(opaque)` to thread pool _if_ a queue slot is available. 77 * Returns immediately even if not (does not block). 78 * @return : 1 if successful, 0 if not. 79 */ 80 int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque); 81 82 #endif 83