xref: /freebsd/sys/contrib/zstd/lib/common/pool.h (revision 4f52dfbb8d6c4d446500c5b097e3806ec219fbd4)
1 /*
2  * Copyright (c) 2016-present, 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 #define ZSTD_STATIC_LINKING_ONLY   /* ZSTD_customMem */
21 #include "zstd.h"
22 
23 typedef struct POOL_ctx_s POOL_ctx;
24 
25 /*! POOL_create() :
26  *  Create a thread pool with at most `numThreads` threads.
27  * `numThreads` must be at least 1.
28  *  The maximum number of queued jobs before blocking is `queueSize`.
29  * @return : POOL_ctx pointer on success, else NULL.
30 */
31 POOL_ctx* POOL_create(size_t numThreads, size_t queueSize);
32 
33 POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize, ZSTD_customMem customMem);
34 
35 /*! POOL_free() :
36     Free a thread pool returned by POOL_create().
37 */
38 void POOL_free(POOL_ctx* ctx);
39 
40 /*! POOL_sizeof() :
41     return memory usage of pool returned by POOL_create().
42 */
43 size_t POOL_sizeof(POOL_ctx* ctx);
44 
45 /*! POOL_function :
46     The function type that can be added to a thread pool.
47 */
48 typedef void (*POOL_function)(void*);
49 /*! POOL_add_function :
50     The function type for a generic thread pool add function.
51 */
52 typedef void (*POOL_add_function)(void*, POOL_function, void*);
53 
54 /*! POOL_add() :
55     Add the job `function(opaque)` to the thread pool. `ctx` must be valid.
56     Possibly blocks until there is room in the queue.
57     Note : The function may be executed asynchronously, so `opaque` must live until the function has been completed.
58 */
59 void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque);
60 
61 
62 /*! POOL_tryAdd() :
63     Add the job `function(opaque)` to the thread pool if a worker is available.
64     return immediately otherwise.
65    @return : 1 if successful, 0 if not.
66 */
67 int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque);
68 
69 
70 #if defined (__cplusplus)
71 }
72 #endif
73 
74 #endif
75