blob: 0ebde1805db5f76ffb4e0cd761f41a7da099bb4a [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
Nick Terrella4943082021-03-29 14:23:36 -07002 * Copyright (c) Yann Collet, Facebook, Inc.
Nick Terrelle777a5b2016-12-29 23:39:44 -08003 * All rights reserved.
4 *
Yann Collet32fb4072017-08-18 16:52:05 -07005 * 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 Collet3128e032017-09-08 00:09:23 -07008 * You may select, at your option, one of the above-listed licenses.
Nick Terrelle777a5b2016-12-29 23:39:44 -08009 */
Yann Collet32fb4072017-08-18 16:52:05 -070010
Nick Terrelle777a5b2016-12-29 23:39:44 -080011#ifndef POOL_H
12#define POOL_H
13
Nick Terrellb42dd272017-01-27 16:00:19 -080014#if defined (__cplusplus)
15extern "C" {
16#endif
17
18
Nick Terrell80f577b2020-08-06 20:18:05 -070019#include "zstd_deps.h"
Yann Collet997e4d02018-01-18 14:39:51 -080020#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_customMem */
W. Felix Handte60288272020-05-01 16:07:57 -040021#include "../zstd.h"
Nick Terrelle777a5b2016-12-29 23:39:44 -080022
23typedef struct POOL_ctx_s POOL_ctx;
24
25/*! POOL_create() :
Yann Collet16261e62017-07-11 14:14:07 -070026 * 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`.
Yann Collet16261e62017-07-11 14:14:07 -070029 * @return : POOL_ctx pointer on success, else NULL.
Nick Terrelle777a5b2016-12-29 23:39:44 -080030*/
Yann Collet997e4d02018-01-18 14:39:51 -080031POOL_ctx* POOL_create(size_t numThreads, size_t queueSize);
Nick Terrelle777a5b2016-12-29 23:39:44 -080032
Yann Collet1c714fd2018-06-18 20:46:39 -070033POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize,
34 ZSTD_customMem customMem);
Nick Terrell26dc0402017-08-24 17:01:41 -070035
Nick Terrelle777a5b2016-12-29 23:39:44 -080036/*! POOL_free() :
Yann Collet1c714fd2018-06-18 20:46:39 -070037 * Free a thread pool returned by POOL_create().
38 */
Yann Collet997e4d02018-01-18 14:39:51 -080039void POOL_free(POOL_ctx* ctx);
Nick Terrelle777a5b2016-12-29 23:39:44 -080040
Yann Collet4567c572018-06-19 16:03:12 -070041/*! POOL_resize() :
42 * Expands or shrinks pool's number of threads.
Yann Colletfbd5dfc2018-06-22 12:14:59 -070043 * This is more efficient than releasing + creating a new context,
44 * since it tries to preserve and re-use existing threads.
45 * `numThreads` must be at least 1.
46 * @return : 0 when resize was successful,
47 * !0 (typically 1) if there is an error.
48 * note : only numThreads can be resized, queueSize remains unchanged.
Yann Collet4567c572018-06-19 16:03:12 -070049 */
Yann Colletfbd5dfc2018-06-22 12:14:59 -070050int POOL_resize(POOL_ctx* ctx, size_t numThreads);
Yann Collet4567c572018-06-19 16:03:12 -070051
Yann Colletc4a5a212017-06-01 17:56:14 -070052/*! POOL_sizeof() :
Yann Collet1c714fd2018-06-18 20:46:39 -070053 * @return threadpool memory usage
54 * note : compatible with NULL (returns 0 in this case)
55 */
Yann Colletb1978d62021-12-30 14:08:51 -080056size_t POOL_sizeof(const POOL_ctx* ctx);
Yann Colletc4a5a212017-06-01 17:56:14 -070057
Nick Terrelle777a5b2016-12-29 23:39:44 -080058/*! POOL_function :
Yann Collet1c714fd2018-06-18 20:46:39 -070059 * The function type that can be added to a thread pool.
60 */
Yann Collet997e4d02018-01-18 14:39:51 -080061typedef void (*POOL_function)(void*);
Nick Terrelle777a5b2016-12-29 23:39:44 -080062
63/*! POOL_add() :
Yann Collet1c714fd2018-06-18 20:46:39 -070064 * Add the job `function(opaque)` to the thread pool. `ctx` must be valid.
65 * Possibly blocks until there is room in the queue.
66 * Note : The function may be executed asynchronously,
67 * therefore, `opaque` must live until function has been completed.
68 */
Yann Collet997e4d02018-01-18 14:39:51 -080069void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque);
70
71
72/*! POOL_tryAdd() :
Yann Colletb1978d62021-12-30 14:08:51 -080073 * Add the job `function(opaque)` to thread pool _if_ a queue slot is available.
Yann Collet1c714fd2018-06-18 20:46:39 -070074 * Returns immediately even if not (does not block).
75 * @return : 1 if successful, 0 if not.
76 */
Yann Collet997e4d02018-01-18 14:39:51 -080077int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque);
Nick Terrelle777a5b2016-12-29 23:39:44 -080078
Nick Terrellb42dd272017-01-27 16:00:19 -080079
80#if defined (__cplusplus)
81}
82#endif
83
Nick Terrelle777a5b2016-12-29 23:39:44 -080084#endif