2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
5 * 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).
8 * You may select, at your option, one of the above-listed licenses.
12 /* ====== Dependencies ======= */
13 #include <stddef.h> /* size_t */
16 /* ====== Compiler specifics ====== */
18 # pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */
22 #ifdef ZSTD_MULTITHREAD
24 #include "threading.h" /* pthread adaptation */
26 /* A job is a function and an opaque argument */
27 typedef struct POOL_job_s {
28 POOL_function function;
33 ZSTD_customMem customMem;
34 /* Keep track of the threads */
35 ZSTD_pthread_t *threads;
38 /* The queue is a circular buffer */
44 /* The number of threads working on jobs */
45 size_t numThreadsBusy;
46 /* Indicates if the queue is empty */
49 /* The mutex protects the queue */
50 ZSTD_pthread_mutex_t queueMutex;
51 /* Condition variable for pushers to wait on when the queue is full */
52 ZSTD_pthread_cond_t queuePushCond;
53 /* Condition variables for poppers to wait on when the queue is empty */
54 ZSTD_pthread_cond_t queuePopCond;
55 /* Indicates if the queue is shutting down */
60 Work thread for the thread pool.
61 Waits for jobs and executes them.
62 @returns : NULL on failure else non-null.
64 static void* POOL_thread(void* opaque) {
65 POOL_ctx* const ctx = (POOL_ctx*)opaque;
66 if (!ctx) { return NULL; }
68 /* Lock the mutex and wait for a non-empty queue or until shutdown */
69 ZSTD_pthread_mutex_lock(&ctx->queueMutex);
71 while (ctx->queueEmpty && !ctx->shutdown) {
72 ZSTD_pthread_cond_wait(&ctx->queuePopCond, &ctx->queueMutex);
74 /* empty => shutting down: so stop */
75 if (ctx->queueEmpty) {
76 ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
79 /* Pop a job off the queue */
80 { POOL_job const job = ctx->queue[ctx->queueHead];
81 ctx->queueHead = (ctx->queueHead + 1) % ctx->queueSize;
82 ctx->numThreadsBusy++;
83 ctx->queueEmpty = ctx->queueHead == ctx->queueTail;
84 /* Unlock the mutex, signal a pusher, and run the job */
85 ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
86 ZSTD_pthread_cond_signal(&ctx->queuePushCond);
88 job.function(job.opaque);
90 /* If the intended queue size was 0, signal after finishing job */
91 if (ctx->queueSize == 1) {
92 ZSTD_pthread_mutex_lock(&ctx->queueMutex);
93 ctx->numThreadsBusy--;
94 ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
95 ZSTD_pthread_cond_signal(&ctx->queuePushCond);
101 POOL_ctx* POOL_create(size_t numThreads, size_t queueSize) {
102 return POOL_create_advanced(numThreads, queueSize, ZSTD_defaultCMem);
105 POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize, ZSTD_customMem customMem) {
107 /* Check the parameters */
108 if (!numThreads) { return NULL; }
109 /* Allocate the context and zero initialize */
110 ctx = (POOL_ctx*)ZSTD_calloc(sizeof(POOL_ctx), customMem);
111 if (!ctx) { return NULL; }
112 /* Initialize the job queue.
113 * It needs one extra space since one space is wasted to differentiate empty
116 ctx->queueSize = queueSize + 1;
117 ctx->queue = (POOL_job*)ZSTD_malloc(ctx->queueSize * sizeof(POOL_job), customMem);
120 ctx->numThreadsBusy = 0;
122 (void)ZSTD_pthread_mutex_init(&ctx->queueMutex, NULL);
123 (void)ZSTD_pthread_cond_init(&ctx->queuePushCond, NULL);
124 (void)ZSTD_pthread_cond_init(&ctx->queuePopCond, NULL);
126 /* Allocate space for the thread handles */
127 ctx->threads = (ZSTD_pthread_t*)ZSTD_malloc(numThreads * sizeof(ZSTD_pthread_t), customMem);
129 ctx->customMem = customMem;
130 /* Check for errors */
131 if (!ctx->threads || !ctx->queue) { POOL_free(ctx); return NULL; }
132 /* Initialize the threads */
134 for (i = 0; i < numThreads; ++i) {
135 if (ZSTD_pthread_create(&ctx->threads[i], NULL, &POOL_thread, ctx)) {
140 ctx->numThreads = numThreads;
146 Shutdown the queue, wake any sleeping threads, and join all of the threads.
148 static void POOL_join(POOL_ctx* ctx) {
149 /* Shut down the queue */
150 ZSTD_pthread_mutex_lock(&ctx->queueMutex);
152 ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
153 /* Wake up sleeping threads */
154 ZSTD_pthread_cond_broadcast(&ctx->queuePushCond);
155 ZSTD_pthread_cond_broadcast(&ctx->queuePopCond);
156 /* Join all of the threads */
158 for (i = 0; i < ctx->numThreads; ++i) {
159 ZSTD_pthread_join(ctx->threads[i], NULL);
163 void POOL_free(POOL_ctx *ctx) {
164 if (!ctx) { return; }
166 ZSTD_pthread_mutex_destroy(&ctx->queueMutex);
167 ZSTD_pthread_cond_destroy(&ctx->queuePushCond);
168 ZSTD_pthread_cond_destroy(&ctx->queuePopCond);
169 ZSTD_free(ctx->queue, ctx->customMem);
170 ZSTD_free(ctx->threads, ctx->customMem);
171 ZSTD_free(ctx, ctx->customMem);
174 size_t POOL_sizeof(POOL_ctx *ctx) {
175 if (ctx==NULL) return 0; /* supports sizeof NULL */
177 + ctx->queueSize * sizeof(POOL_job)
178 + ctx->numThreads * sizeof(ZSTD_pthread_t);
182 * Returns 1 if the queue is full and 0 otherwise.
184 * If the queueSize is 1 (the pool was created with an intended queueSize of 0),
185 * then a queue is empty if there is a thread free and no job is waiting.
187 static int isQueueFull(POOL_ctx const* ctx) {
188 if (ctx->queueSize > 1) {
189 return ctx->queueHead == ((ctx->queueTail + 1) % ctx->queueSize);
191 return ctx->numThreadsBusy == ctx->numThreads ||
196 void POOL_add(void* ctxVoid, POOL_function function, void *opaque) {
197 POOL_ctx* const ctx = (POOL_ctx*)ctxVoid;
198 if (!ctx) { return; }
200 ZSTD_pthread_mutex_lock(&ctx->queueMutex);
201 { POOL_job const job = {function, opaque};
203 /* Wait until there is space in the queue for the new job */
204 while (isQueueFull(ctx) && !ctx->shutdown) {
205 ZSTD_pthread_cond_wait(&ctx->queuePushCond, &ctx->queueMutex);
207 /* The queue is still going => there is space */
208 if (!ctx->shutdown) {
210 ctx->queue[ctx->queueTail] = job;
211 ctx->queueTail = (ctx->queueTail + 1) % ctx->queueSize;
214 ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
215 ZSTD_pthread_cond_signal(&ctx->queuePopCond);
218 #else /* ZSTD_MULTITHREAD not defined */
219 /* No multi-threading support */
221 /* We don't need any data, but if it is empty malloc() might return NULL. */
225 static POOL_ctx g_ctx;
227 POOL_ctx* POOL_create(size_t numThreads, size_t queueSize) {
228 return POOL_create_advanced(numThreads, queueSize, ZSTD_defaultCMem);
231 POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize, ZSTD_customMem customMem) {
238 void POOL_free(POOL_ctx* ctx) {
239 assert(!ctx || ctx == &g_ctx);
243 void POOL_add(void* ctx, POOL_function function, void* opaque) {
248 size_t POOL_sizeof(POOL_ctx* ctx) {
249 if (ctx==NULL) return 0; /* supports sizeof NULL */
250 assert(ctx == &g_ctx);
254 #endif /* ZSTD_MULTITHREAD */