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