2017-08-18 23:52:05 +00:00
|
|
|
/*
|
2020-03-26 22:19:05 +00:00
|
|
|
* Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
|
2016-12-30 07:39:44 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2017-08-18 23:52:05 +00:00
|
|
|
* This source code is licensed under both the BSD-style license (found in the
|
|
|
|
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
|
|
|
* in the COPYING file in the root directory of this source tree).
|
2017-09-08 07:09:23 +00:00
|
|
|
* You may select, at your option, one of the above-listed licenses.
|
2016-12-30 07:39:44 +00:00
|
|
|
*/
|
2017-08-18 23:52:05 +00:00
|
|
|
|
2016-12-30 07:39:44 +00:00
|
|
|
#ifndef POOL_H
|
|
|
|
#define POOL_H
|
|
|
|
|
2017-01-28 00:00:19 +00:00
|
|
|
#if defined (__cplusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2016-12-30 07:39:44 +00:00
|
|
|
#include <stddef.h> /* size_t */
|
2018-01-18 22:39:51 +00:00
|
|
|
#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_customMem */
|
2020-05-01 20:07:57 +00:00
|
|
|
#include "../zstd.h"
|
2016-12-30 07:39:44 +00:00
|
|
|
|
|
|
|
typedef struct POOL_ctx_s POOL_ctx;
|
|
|
|
|
|
|
|
/*! POOL_create() :
|
2017-07-11 21:14:07 +00:00
|
|
|
* Create a thread pool with at most `numThreads` threads.
|
|
|
|
* `numThreads` must be at least 1.
|
|
|
|
* The maximum number of queued jobs before blocking is `queueSize`.
|
|
|
|
* @return : POOL_ctx pointer on success, else NULL.
|
2016-12-30 07:39:44 +00:00
|
|
|
*/
|
2018-01-18 22:39:51 +00:00
|
|
|
POOL_ctx* POOL_create(size_t numThreads, size_t queueSize);
|
2016-12-30 07:39:44 +00:00
|
|
|
|
2018-06-19 03:46:39 +00:00
|
|
|
POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize,
|
|
|
|
ZSTD_customMem customMem);
|
2017-08-25 00:01:41 +00:00
|
|
|
|
2016-12-30 07:39:44 +00:00
|
|
|
/*! POOL_free() :
|
2018-06-19 03:46:39 +00:00
|
|
|
* Free a thread pool returned by POOL_create().
|
|
|
|
*/
|
2018-01-18 22:39:51 +00:00
|
|
|
void POOL_free(POOL_ctx* ctx);
|
2016-12-30 07:39:44 +00:00
|
|
|
|
2018-06-19 23:03:12 +00:00
|
|
|
/*! POOL_resize() :
|
|
|
|
* Expands or shrinks pool's number of threads.
|
2018-06-22 19:14:59 +00:00
|
|
|
* This is more efficient than releasing + creating a new context,
|
|
|
|
* since it tries to preserve and re-use existing threads.
|
|
|
|
* `numThreads` must be at least 1.
|
|
|
|
* @return : 0 when resize was successful,
|
|
|
|
* !0 (typically 1) if there is an error.
|
|
|
|
* note : only numThreads can be resized, queueSize remains unchanged.
|
2018-06-19 23:03:12 +00:00
|
|
|
*/
|
2018-06-22 19:14:59 +00:00
|
|
|
int POOL_resize(POOL_ctx* ctx, size_t numThreads);
|
2018-06-19 23:03:12 +00:00
|
|
|
|
2017-06-02 00:56:14 +00:00
|
|
|
/*! POOL_sizeof() :
|
2018-06-19 03:46:39 +00:00
|
|
|
* @return threadpool memory usage
|
|
|
|
* note : compatible with NULL (returns 0 in this case)
|
|
|
|
*/
|
2018-01-18 22:39:51 +00:00
|
|
|
size_t POOL_sizeof(POOL_ctx* ctx);
|
2017-06-02 00:56:14 +00:00
|
|
|
|
2016-12-30 07:39:44 +00:00
|
|
|
/*! POOL_function :
|
2018-06-19 03:46:39 +00:00
|
|
|
* The function type that can be added to a thread pool.
|
|
|
|
*/
|
2018-01-18 22:39:51 +00:00
|
|
|
typedef void (*POOL_function)(void*);
|
2016-12-30 07:39:44 +00:00
|
|
|
|
|
|
|
/*! POOL_add() :
|
2018-06-19 03:46:39 +00:00
|
|
|
* Add the job `function(opaque)` to the thread pool. `ctx` must be valid.
|
|
|
|
* Possibly blocks until there is room in the queue.
|
|
|
|
* Note : The function may be executed asynchronously,
|
|
|
|
* therefore, `opaque` must live until function has been completed.
|
|
|
|
*/
|
2018-01-18 22:39:51 +00:00
|
|
|
void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque);
|
|
|
|
|
|
|
|
|
|
|
|
/*! POOL_tryAdd() :
|
2018-06-19 03:46:39 +00:00
|
|
|
* Add the job `function(opaque)` to thread pool _if_ a worker is available.
|
|
|
|
* Returns immediately even if not (does not block).
|
|
|
|
* @return : 1 if successful, 0 if not.
|
|
|
|
*/
|
2018-01-18 22:39:51 +00:00
|
|
|
int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque);
|
2016-12-30 07:39:44 +00:00
|
|
|
|
2017-01-28 00:00:19 +00:00
|
|
|
|
|
|
|
#if defined (__cplusplus)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-12-30 07:39:44 +00:00
|
|
|
#endif
|