Path: blob/main/sys/contrib/zstd/lib/common/pool.h
48375 views
/*1* Copyright (c) Yann Collet, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under both the BSD-style license (found in the5* LICENSE file in the root directory of this source tree) and the GPLv2 (found6* in the COPYING file in the root directory of this source tree).7* You may select, at your option, one of the above-listed licenses.8*/910#ifndef POOL_H11#define POOL_H1213#if defined (__cplusplus)14extern "C" {15#endif161718#include "zstd_deps.h"19#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_customMem */20#include "../zstd.h"2122typedef struct POOL_ctx_s POOL_ctx;2324/*! POOL_create() :25* Create a thread pool with at most `numThreads` threads.26* `numThreads` must be at least 1.27* The maximum number of queued jobs before blocking is `queueSize`.28* @return : POOL_ctx pointer on success, else NULL.29*/30POOL_ctx* POOL_create(size_t numThreads, size_t queueSize);3132POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize,33ZSTD_customMem customMem);3435/*! POOL_free() :36* Free a thread pool returned by POOL_create().37*/38void POOL_free(POOL_ctx* ctx);3940/*! POOL_resize() :41* Expands or shrinks pool's number of threads.42* This is more efficient than releasing + creating a new context,43* since it tries to preserve and re-use existing threads.44* `numThreads` must be at least 1.45* @return : 0 when resize was successful,46* !0 (typically 1) if there is an error.47* note : only numThreads can be resized, queueSize remains unchanged.48*/49int POOL_resize(POOL_ctx* ctx, size_t numThreads);5051/*! POOL_sizeof() :52* @return threadpool memory usage53* note : compatible with NULL (returns 0 in this case)54*/55size_t POOL_sizeof(const POOL_ctx* ctx);5657/*! POOL_function :58* The function type that can be added to a thread pool.59*/60typedef void (*POOL_function)(void*);6162/*! POOL_add() :63* 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*/68void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque);697071/*! POOL_tryAdd() :72* Add the job `function(opaque)` to thread pool _if_ a queue slot is available.73* Returns immediately even if not (does not block).74* @return : 1 if successful, 0 if not.75*/76int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque);777879#if defined (__cplusplus)80}81#endif8283#endif848586