1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only 2 /* 3 * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc. 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 #if defined (__cplusplus) 16 extern "C" { 17 #endif 18 19 20 #include <stddef.h> /* size_t */ 21 #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_customMem */ 22 #include "../zstd.h" 23 24 typedef struct POOL_ctx_s POOL_ctx; 25 26 /*! POOL_create() : 27 * Create a thread pool with at most `numThreads` threads. 28 * `numThreads` must be at least 1. 29 * The maximum number of queued jobs before blocking is `queueSize`. 30 * @return : POOL_ctx pointer on success, else NULL. 31 */ 32 POOL_ctx* POOL_create(size_t numThreads, size_t queueSize); 33 34 POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize, 35 ZSTD_customMem customMem); 36 37 /*! POOL_free() : 38 * Free a thread pool returned by POOL_create(). 39 */ 40 void POOL_free(POOL_ctx* ctx); 41 42 /*! 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 re-use 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 */ 51 int POOL_resize(POOL_ctx* ctx, size_t numThreads); 52 53 /*! POOL_sizeof() : 54 * @return threadpool memory usage 55 * note : compatible with NULL (returns 0 in this case) 56 */ 57 size_t POOL_sizeof(POOL_ctx* ctx); 58 59 /*! POOL_function : 60 * The function type that can be added to a thread pool. 61 */ 62 typedef void (*POOL_function)(void*); 63 64 /*! 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 */ 70 void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque); 71 72 73 /*! POOL_tryAdd() : 74 * Add the job `function(opaque)` to thread pool _if_ a worker is available. 75 * Returns immediately even if not (does not block). 76 * @return : 1 if successful, 0 if not. 77 */ 78 int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque); 79 80 81 #if defined (__cplusplus) 82 } 83 #endif 84 85 #endif 86