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