| Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 1 | /* |
| W. Felix Handte | 5d693cc | 2022-12-20 12:49:47 -0500 | [diff] [blame] | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
| Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 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). |
| Yann Collet | 3128e03 | 2017-09-08 00:09:23 -0700 | [diff] [blame] | 8 | * You may select, at your option, one of the above-listed licenses. |
| Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 9 | */ |
| Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 10 | |
| Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 11 | #ifndef POOL_H |
| 12 | #define POOL_H |
| 13 | |
| Nick Terrell | b42dd27 | 2017-01-27 16:00:19 -0800 | [diff] [blame] | 14 | |
| Nick Terrell | 80f577b | 2020-08-06 20:18:05 -0700 | [diff] [blame] | 15 | #include "zstd_deps.h" |
| Yann Collet | 997e4d0 | 2018-01-18 14:39:51 -0800 | [diff] [blame] | 16 | #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_customMem */ |
| W. Felix Handte | 6028827 | 2020-05-01 16:07:57 -0400 | [diff] [blame] | 17 | #include "../zstd.h" |
| Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 18 | |
| 19 | typedef struct POOL_ctx_s POOL_ctx; |
| 20 | |
| 21 | /*! POOL_create() : |
| Yann Collet | 16261e6 | 2017-07-11 14:14:07 -0700 | [diff] [blame] | 22 | * Create a thread pool with at most `numThreads` threads. |
| 23 | * `numThreads` must be at least 1. |
| 24 | * The maximum number of queued jobs before blocking is `queueSize`. |
| Yann Collet | 16261e6 | 2017-07-11 14:14:07 -0700 | [diff] [blame] | 25 | * @return : POOL_ctx pointer on success, else NULL. |
| Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 26 | */ |
| Yann Collet | 997e4d0 | 2018-01-18 14:39:51 -0800 | [diff] [blame] | 27 | POOL_ctx* POOL_create(size_t numThreads, size_t queueSize); |
| Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 28 | |
| Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 29 | POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize, |
| 30 | ZSTD_customMem customMem); |
| Nick Terrell | 26dc040 | 2017-08-24 17:01:41 -0700 | [diff] [blame] | 31 | |
| Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 32 | /*! POOL_free() : |
| Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 33 | * Free a thread pool returned by POOL_create(). |
| 34 | */ |
| Yann Collet | 997e4d0 | 2018-01-18 14:39:51 -0800 | [diff] [blame] | 35 | void POOL_free(POOL_ctx* ctx); |
| Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 36 | |
| Yonatan Komornik | 1598e6c | 2022-01-21 13:55:41 -0800 | [diff] [blame] | 37 | |
| 38 | /*! POOL_joinJobs() : |
| 39 | * Waits for all queued jobs to finish executing. |
| 40 | */ |
| 41 | void POOL_joinJobs(POOL_ctx* ctx); |
| 42 | |
| Yann Collet | 4567c57 | 2018-06-19 16:03:12 -0700 | [diff] [blame] | 43 | /*! POOL_resize() : |
| 44 | * Expands or shrinks pool's number of threads. |
| Yann Collet | fbd5dfc | 2018-06-22 12:14:59 -0700 | [diff] [blame] | 45 | * This is more efficient than releasing + creating a new context, |
| Dimitri Papadopoulos | fe34776 | 2023-09-23 18:56:01 +0200 | [diff] [blame] | 46 | * since it tries to preserve and reuse existing threads. |
| Yann Collet | fbd5dfc | 2018-06-22 12:14:59 -0700 | [diff] [blame] | 47 | * `numThreads` must be at least 1. |
| 48 | * @return : 0 when resize was successful, |
| 49 | * !0 (typically 1) if there is an error. |
| 50 | * note : only numThreads can be resized, queueSize remains unchanged. |
| Yann Collet | 4567c57 | 2018-06-19 16:03:12 -0700 | [diff] [blame] | 51 | */ |
| Yann Collet | fbd5dfc | 2018-06-22 12:14:59 -0700 | [diff] [blame] | 52 | int POOL_resize(POOL_ctx* ctx, size_t numThreads); |
| Yann Collet | 4567c57 | 2018-06-19 16:03:12 -0700 | [diff] [blame] | 53 | |
| Yann Collet | c4a5a21 | 2017-06-01 17:56:14 -0700 | [diff] [blame] | 54 | /*! POOL_sizeof() : |
| Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 55 | * @return threadpool memory usage |
| 56 | * note : compatible with NULL (returns 0 in this case) |
| 57 | */ |
| Yann Collet | b1978d6 | 2021-12-30 14:08:51 -0800 | [diff] [blame] | 58 | size_t POOL_sizeof(const POOL_ctx* ctx); |
| Yann Collet | c4a5a21 | 2017-06-01 17:56:14 -0700 | [diff] [blame] | 59 | |
| Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 60 | /*! POOL_function : |
| Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 61 | * The function type that can be added to a thread pool. |
| 62 | */ |
| Yann Collet | 997e4d0 | 2018-01-18 14:39:51 -0800 | [diff] [blame] | 63 | typedef void (*POOL_function)(void*); |
| Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 64 | |
| 65 | /*! POOL_add() : |
| Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 66 | * Add the job `function(opaque)` to the thread pool. `ctx` must be valid. |
| 67 | * Possibly blocks until there is room in the queue. |
| 68 | * Note : The function may be executed asynchronously, |
| 69 | * therefore, `opaque` must live until function has been completed. |
| 70 | */ |
| Yann Collet | 997e4d0 | 2018-01-18 14:39:51 -0800 | [diff] [blame] | 71 | void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque); |
| 72 | |
| 73 | |
| 74 | /*! POOL_tryAdd() : |
| Yann Collet | b1978d6 | 2021-12-30 14:08:51 -0800 | [diff] [blame] | 75 | * Add the job `function(opaque)` to thread pool _if_ a queue slot is available. |
| Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 76 | * Returns immediately even if not (does not block). |
| 77 | * @return : 1 if successful, 0 if not. |
| 78 | */ |
| Yann Collet | 997e4d0 | 2018-01-18 14:39:51 -0800 | [diff] [blame] | 79 | int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque); |
| Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 80 | |
| Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 81 | #endif |