blob: f39b7f1eb99e7f90485746df68275acfb93c959e [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
W. Felix Handte5d693cc2022-12-20 12:49:47 -05002 * Copyright (c) Meta Platforms, Inc. and affiliates.
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
Nick Terrell80f577b2020-08-06 20:18:05 -070015#include "zstd_deps.h"
Yann Collet997e4d02018-01-18 14:39:51 -080016#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_customMem */
W. Felix Handte60288272020-05-01 16:07:57 -040017#include "../zstd.h"
Nick Terrelle777a5b2016-12-29 23:39:44 -080018
19typedef struct POOL_ctx_s POOL_ctx;
20
21/*! POOL_create() :
Yann Collet16261e62017-07-11 14:14:07 -070022 * Create a thread pool with at most `numThreads` threads.
23 * `numThreads` must be at least 1.
24 * The maximum number of queued jobs before blocking is `queueSize`.
Yann Collet16261e62017-07-11 14:14:07 -070025 * @return : POOL_ctx pointer on success, else NULL.
Nick Terrelle777a5b2016-12-29 23:39:44 -080026*/
Yann Collet997e4d02018-01-18 14:39:51 -080027POOL_ctx* POOL_create(size_t numThreads, size_t queueSize);
Nick Terrelle777a5b2016-12-29 23:39:44 -080028
Yann Collet1c714fd2018-06-18 20:46:39 -070029POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize,
30 ZSTD_customMem customMem);
Nick Terrell26dc0402017-08-24 17:01:41 -070031
Nick Terrelle777a5b2016-12-29 23:39:44 -080032/*! POOL_free() :
Yann Collet1c714fd2018-06-18 20:46:39 -070033 * Free a thread pool returned by POOL_create().
34 */
Yann Collet997e4d02018-01-18 14:39:51 -080035void POOL_free(POOL_ctx* ctx);
Nick Terrelle777a5b2016-12-29 23:39:44 -080036
Yonatan Komornik1598e6c2022-01-21 13:55:41 -080037
38/*! POOL_joinJobs() :
39 * Waits for all queued jobs to finish executing.
40 */
41void POOL_joinJobs(POOL_ctx* ctx);
42
Yann Collet4567c572018-06-19 16:03:12 -070043/*! POOL_resize() :
44 * Expands or shrinks pool's number of threads.
Yann Colletfbd5dfc2018-06-22 12:14:59 -070045 * This is more efficient than releasing + creating a new context,
Dimitri Papadopoulosfe347762023-09-23 18:56:01 +020046 * since it tries to preserve and reuse existing threads.
Yann Colletfbd5dfc2018-06-22 12:14:59 -070047 * `numThreads` must be at least 1.
48 * @return : 0 when resize was successful,
49 * !0 (typically 1) if there is an error.
50 * note : only numThreads can be resized, queueSize remains unchanged.
Yann Collet4567c572018-06-19 16:03:12 -070051 */
Yann Colletfbd5dfc2018-06-22 12:14:59 -070052int POOL_resize(POOL_ctx* ctx, size_t numThreads);
Yann Collet4567c572018-06-19 16:03:12 -070053
Yann Colletc4a5a212017-06-01 17:56:14 -070054/*! POOL_sizeof() :
Yann Collet1c714fd2018-06-18 20:46:39 -070055 * @return threadpool memory usage
56 * note : compatible with NULL (returns 0 in this case)
57 */
Yann Colletb1978d62021-12-30 14:08:51 -080058size_t POOL_sizeof(const POOL_ctx* ctx);
Yann Colletc4a5a212017-06-01 17:56:14 -070059
Nick Terrelle777a5b2016-12-29 23:39:44 -080060/*! POOL_function :
Yann Collet1c714fd2018-06-18 20:46:39 -070061 * The function type that can be added to a thread pool.
62 */
Yann Collet997e4d02018-01-18 14:39:51 -080063typedef void (*POOL_function)(void*);
Nick Terrelle777a5b2016-12-29 23:39:44 -080064
65/*! POOL_add() :
Yann Collet1c714fd2018-06-18 20:46:39 -070066 * Add the job `function(opaque)` to the thread pool. `ctx` must be valid.
67 * Possibly blocks until there is room in the queue.
68 * Note : The function may be executed asynchronously,
69 * therefore, `opaque` must live until function has been completed.
70 */
Yann Collet997e4d02018-01-18 14:39:51 -080071void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque);
72
73
74/*! POOL_tryAdd() :
Yann Colletb1978d62021-12-30 14:08:51 -080075 * Add the job `function(opaque)` to thread pool _if_ a queue slot is available.
Yann Collet1c714fd2018-06-18 20:46:39 -070076 * Returns immediately even if not (does not block).
77 * @return : 1 if successful, 0 if not.
78 */
Yann Collet997e4d02018-01-18 14:39:51 -080079int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque);
Nick Terrelle777a5b2016-12-29 23:39:44 -080080
Nick Terrelle777a5b2016-12-29 23:39:44 -080081#endif