1 /* 2 * Copyright (c) 2016-2020 Yann Collet, Facebook, Inc. 3 * All rights reserved. 4 * 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. 9 */ 10 11 #ifndef POOL_H 12 #define POOL_H 13 14 #if defined (__cplusplus) 15 extern "C" { 16 #endif 17 18 19 #include <stddef.h> /* size_t */ 20 21 typedef struct POOL_ctx_s POOL_ctx; 22 23 /*! POOL_create() : 24 * Create a thread pool with at most `numThreads` threads. 25 * `numThreads` must be at least 1. 26 * The maximum number of queued jobs before blocking is `queueSize`. 27 * @return : POOL_ctx pointer on success, else NULL. 28 */ 29 POOL_ctx* POOL_create(size_t numThreads, size_t queueSize); 30 31 /*! POOL_free() : 32 * Free a thread pool returned by POOL_create(). 33 */ 34 void POOL_free(POOL_ctx* ctx); 35 36 /*! POOL_resize() : 37 * Expands or shrinks pool's number of threads. 38 * This is more efficient than releasing + creating a new context, 39 * since it tries to preserve and re-use existing threads. 40 * `numThreads` must be at least 1. 41 * @return : 0 when resize was successful, 42 * !0 (typically 1) if there is an error. 43 * note : only numThreads can be resized, queueSize remains unchanged. 44 */ 45 int POOL_resize(POOL_ctx* ctx, size_t numThreads); 46 47 /*! POOL_sizeof() : 48 * @return threadpool memory usage 49 * note : compatible with NULL (returns 0 in this case) 50 */ 51 size_t POOL_sizeof(POOL_ctx* ctx); 52 53 /*! POOL_function : 54 * The function type that can be added to a thread pool. 55 */ 56 typedef void (*POOL_function)(void*); 57 58 /*! POOL_add() : 59 * Add the job `function(opaque)` to the thread pool. `ctx` must be valid. 60 * Possibly blocks until there is room in the queue. 61 * Note : The function may be executed asynchronously, 62 * therefore, `opaque` must live until function has been completed. 63 */ 64 void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque); 65 66 67 /*! POOL_tryAdd() : 68 * Add the job `function(opaque)` to thread pool _if_ a worker is available. 69 * Returns immediately even if not (does not block). 70 * @return : 1 if successful, 0 if not. 71 */ 72 int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque); 73 74 75 76 #if defined (__cplusplus) 77 } 78 #endif 79 80 #endif 81