xref: /freebsd/sys/contrib/zstd/lib/common/pool.h (revision 5ff13fbc199bdf5f0572845351c68ee5ca828e71)
10c16b537SWarner Losh /*
2*5ff13fbcSAllan Jude  * Copyright (c) Yann Collet, Facebook, Inc.
30c16b537SWarner Losh  * All rights reserved.
40c16b537SWarner Losh  *
50c16b537SWarner Losh  * This source code is licensed under both the BSD-style license (found in the
60c16b537SWarner Losh  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
70c16b537SWarner Losh  * in the COPYING file in the root directory of this source tree).
80c16b537SWarner Losh  * You may select, at your option, one of the above-listed licenses.
90c16b537SWarner Losh  */
100c16b537SWarner Losh 
110c16b537SWarner Losh #ifndef POOL_H
120c16b537SWarner Losh #define POOL_H
130c16b537SWarner Losh 
140c16b537SWarner Losh #if defined (__cplusplus)
150c16b537SWarner Losh extern "C" {
160c16b537SWarner Losh #endif
170c16b537SWarner Losh 
180c16b537SWarner Losh 
19f7cd7fe5SConrad Meyer #include "zstd_deps.h"
2019fcbaf1SConrad Meyer #define ZSTD_STATIC_LINKING_ONLY   /* ZSTD_customMem */
2137f1f268SConrad Meyer #include "../zstd.h"
220c16b537SWarner Losh 
230c16b537SWarner Losh typedef struct POOL_ctx_s POOL_ctx;
240c16b537SWarner Losh 
250c16b537SWarner Losh /*! POOL_create() :
260c16b537SWarner Losh  *  Create a thread pool with at most `numThreads` threads.
270c16b537SWarner Losh  * `numThreads` must be at least 1.
280c16b537SWarner Losh  *  The maximum number of queued jobs before blocking is `queueSize`.
290c16b537SWarner Losh  * @return : POOL_ctx pointer on success, else NULL.
300c16b537SWarner Losh */
310c16b537SWarner Losh POOL_ctx* POOL_create(size_t numThreads, size_t queueSize);
320c16b537SWarner Losh 
330f743729SConrad Meyer POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize,
340f743729SConrad Meyer                                ZSTD_customMem customMem);
350c16b537SWarner Losh 
360c16b537SWarner Losh /*! POOL_free() :
370f743729SConrad Meyer  *  Free a thread pool returned by POOL_create().
380c16b537SWarner Losh  */
390c16b537SWarner Losh void POOL_free(POOL_ctx* ctx);
400c16b537SWarner Losh 
410f743729SConrad Meyer /*! POOL_resize() :
420f743729SConrad Meyer  *  Expands or shrinks pool's number of threads.
430f743729SConrad Meyer  *  This is more efficient than releasing + creating a new context,
440f743729SConrad Meyer  *  since it tries to preserve and re-use existing threads.
450f743729SConrad Meyer  * `numThreads` must be at least 1.
460f743729SConrad Meyer  * @return : 0 when resize was successful,
470f743729SConrad Meyer  *           !0 (typically 1) if there is an error.
480f743729SConrad Meyer  *    note : only numThreads can be resized, queueSize remains unchanged.
490f743729SConrad Meyer  */
500f743729SConrad Meyer int POOL_resize(POOL_ctx* ctx, size_t numThreads);
510f743729SConrad Meyer 
520c16b537SWarner Losh /*! POOL_sizeof() :
530f743729SConrad Meyer  * @return threadpool memory usage
540f743729SConrad Meyer  *  note : compatible with NULL (returns 0 in this case)
550c16b537SWarner Losh  */
56*5ff13fbcSAllan Jude size_t POOL_sizeof(const POOL_ctx* ctx);
570c16b537SWarner Losh 
580c16b537SWarner Losh /*! POOL_function :
590f743729SConrad Meyer  *  The function type that can be added to a thread pool.
600c16b537SWarner Losh  */
610c16b537SWarner Losh typedef void (*POOL_function)(void*);
620c16b537SWarner Losh 
630c16b537SWarner Losh /*! POOL_add() :
640f743729SConrad Meyer  *  Add the job `function(opaque)` to the thread pool. `ctx` must be valid.
650f743729SConrad Meyer  *  Possibly blocks until there is room in the queue.
660f743729SConrad Meyer  *  Note : The function may be executed asynchronously,
670f743729SConrad Meyer  *         therefore, `opaque` must live until function has been completed.
680c16b537SWarner Losh  */
6919fcbaf1SConrad Meyer void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque);
7019fcbaf1SConrad Meyer 
7119fcbaf1SConrad Meyer 
7219fcbaf1SConrad Meyer /*! POOL_tryAdd() :
73*5ff13fbcSAllan Jude  *  Add the job `function(opaque)` to thread pool _if_ a queue slot is available.
740f743729SConrad Meyer  *  Returns immediately even if not (does not block).
750f743729SConrad Meyer  * @return : 1 if successful, 0 if not.
7619fcbaf1SConrad Meyer  */
7719fcbaf1SConrad Meyer int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque);
780c16b537SWarner Losh 
790c16b537SWarner Losh 
800c16b537SWarner Losh #if defined (__cplusplus)
810c16b537SWarner Losh }
820c16b537SWarner Losh #endif
830c16b537SWarner Losh 
840c16b537SWarner Losh #endif
85