blob: 00ea76631f3c60d0144cf810b415118398d75898 [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
2 * Copyright (c) 2016-present, 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 Terrelle777a5b2016-12-29 23:39:44 -080019#include <stddef.h> /* size_t */
Yann Collet997e4d02018-01-18 14:39:51 -080020#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_customMem */
21#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.
43 * This is more efficient than releasing and creating a new context.
44 * @return : a new pool context on success, NULL on failure
45 * note : new pool context might have same address as original one, but it's not guaranteed.
46 * consider starting context as consumed, only rely on returned one.
47 * note 2 : only numThreads can be resized, queueSize is unchanged.
48 */
49POOL_ctx* POOL_resize(POOL_ctx* ctx, size_t numThreads);
50
Yann Colletc4a5a212017-06-01 17:56:14 -070051/*! POOL_sizeof() :
Yann Collet1c714fd2018-06-18 20:46:39 -070052 * @return threadpool memory usage
53 * note : compatible with NULL (returns 0 in this case)
54 */
Yann Collet997e4d02018-01-18 14:39:51 -080055size_t POOL_sizeof(POOL_ctx* ctx);
Yann Colletc4a5a212017-06-01 17:56:14 -070056
Nick Terrelle777a5b2016-12-29 23:39:44 -080057/*! POOL_function :
Yann Collet1c714fd2018-06-18 20:46:39 -070058 * The function type that can be added to a thread pool.
59 */
Yann Collet997e4d02018-01-18 14:39:51 -080060typedef void (*POOL_function)(void*);
Nick Terrelle777a5b2016-12-29 23:39:44 -080061
62/*! POOL_add() :
Yann Collet1c714fd2018-06-18 20:46:39 -070063 * Add the job `function(opaque)` to the thread pool. `ctx` must be valid.
64 * Possibly blocks until there is room in the queue.
65 * Note : The function may be executed asynchronously,
66 * therefore, `opaque` must live until function has been completed.
67 */
Yann Collet997e4d02018-01-18 14:39:51 -080068void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque);
69
70
71/*! POOL_tryAdd() :
Yann Collet1c714fd2018-06-18 20:46:39 -070072 * Add the job `function(opaque)` to thread pool _if_ a worker is available.
73 * Returns immediately even if not (does not block).
74 * @return : 1 if successful, 0 if not.
75 */
Yann Collet997e4d02018-01-18 14:39:51 -080076int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque);
Nick Terrelle777a5b2016-12-29 23:39:44 -080077
Nick Terrellb42dd272017-01-27 16:00:19 -080078
79#if defined (__cplusplus)
80}
81#endif
82
Nick Terrelle777a5b2016-12-29 23:39:44 -080083#endif