2017-08-18 23:52:05 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016-present, 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-01-20 22:00:41 +00:00
|
|
|
|
|
|
|
/* ====== Dependencies ======= */
|
2016-12-30 07:39:44 +00:00
|
|
|
#include <stddef.h> /* size_t */
|
2017-01-20 22:00:41 +00:00
|
|
|
#include "pool.h"
|
|
|
|
|
|
|
|
/* ====== Compiler specifics ====== */
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
# pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */
|
|
|
|
#endif
|
|
|
|
|
2016-12-30 07:39:44 +00:00
|
|
|
|
2017-01-19 22:05:07 +00:00
|
|
|
#ifdef ZSTD_MULTITHREAD
|
2016-12-30 07:39:44 +00:00
|
|
|
|
2017-01-26 23:29:10 +00:00
|
|
|
#include "threading.h" /* pthread adaptation */
|
2016-12-30 07:39:44 +00:00
|
|
|
|
|
|
|
/* A job is a function and an opaque argument */
|
|
|
|
typedef struct POOL_job_s {
|
2017-08-24 23:48:32 +00:00
|
|
|
POOL_function function;
|
|
|
|
void *opaque;
|
2016-12-30 07:39:44 +00:00
|
|
|
} POOL_job;
|
|
|
|
|
|
|
|
struct POOL_ctx_s {
|
2017-08-25 00:01:41 +00:00
|
|
|
ZSTD_customMem customMem;
|
2016-12-30 07:39:44 +00:00
|
|
|
/* Keep track of the threads */
|
2017-09-27 18:16:24 +00:00
|
|
|
ZSTD_pthread_t *threads;
|
2016-12-30 07:39:44 +00:00
|
|
|
size_t numThreads;
|
|
|
|
|
|
|
|
/* The queue is a circular buffer */
|
|
|
|
POOL_job *queue;
|
|
|
|
size_t queueHead;
|
|
|
|
size_t queueTail;
|
|
|
|
size_t queueSize;
|
2017-07-31 17:10:16 +00:00
|
|
|
|
2017-08-01 19:24:55 +00:00
|
|
|
/* The number of threads working on jobs */
|
|
|
|
size_t numThreadsBusy;
|
|
|
|
/* Indicates if the queue is empty */
|
|
|
|
int queueEmpty;
|
2017-07-31 17:10:16 +00:00
|
|
|
|
2016-12-30 07:39:44 +00:00
|
|
|
/* The mutex protects the queue */
|
2017-09-27 18:16:24 +00:00
|
|
|
ZSTD_pthread_mutex_t queueMutex;
|
2016-12-30 07:39:44 +00:00
|
|
|
/* Condition variable for pushers to wait on when the queue is full */
|
2017-09-27 18:16:24 +00:00
|
|
|
ZSTD_pthread_cond_t queuePushCond;
|
2016-12-30 07:39:44 +00:00
|
|
|
/* Condition variables for poppers to wait on when the queue is empty */
|
2017-09-27 18:16:24 +00:00
|
|
|
ZSTD_pthread_cond_t queuePopCond;
|
2016-12-30 07:39:44 +00:00
|
|
|
/* Indicates if the queue is shutting down */
|
|
|
|
int shutdown;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* POOL_thread() :
|
|
|
|
Work thread for the thread pool.
|
|
|
|
Waits for jobs and executes them.
|
|
|
|
@returns : NULL on failure else non-null.
|
|
|
|
*/
|
2016-12-31 05:04:25 +00:00
|
|
|
static void* POOL_thread(void* opaque) {
|
|
|
|
POOL_ctx* const ctx = (POOL_ctx*)opaque;
|
2016-12-30 07:39:44 +00:00
|
|
|
if (!ctx) { return NULL; }
|
|
|
|
for (;;) {
|
|
|
|
/* Lock the mutex and wait for a non-empty queue or until shutdown */
|
2017-09-27 18:16:24 +00:00
|
|
|
ZSTD_pthread_mutex_lock(&ctx->queueMutex);
|
2017-08-02 03:12:06 +00:00
|
|
|
|
2017-08-01 19:24:55 +00:00
|
|
|
while (ctx->queueEmpty && !ctx->shutdown) {
|
2017-09-27 18:16:24 +00:00
|
|
|
ZSTD_pthread_cond_wait(&ctx->queuePopCond, &ctx->queueMutex);
|
2016-12-30 07:39:44 +00:00
|
|
|
}
|
|
|
|
/* empty => shutting down: so stop */
|
2017-08-01 19:24:55 +00:00
|
|
|
if (ctx->queueEmpty) {
|
2017-09-27 18:16:24 +00:00
|
|
|
ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
|
2016-12-30 07:39:44 +00:00
|
|
|
return opaque;
|
|
|
|
}
|
2016-12-31 02:31:26 +00:00
|
|
|
/* Pop a job off the queue */
|
2017-08-19 22:07:54 +00:00
|
|
|
{ POOL_job const job = ctx->queue[ctx->queueHead];
|
2016-12-30 07:39:44 +00:00
|
|
|
ctx->queueHead = (ctx->queueHead + 1) % ctx->queueSize;
|
2017-07-31 17:10:16 +00:00
|
|
|
ctx->numThreadsBusy++;
|
2017-08-01 19:24:55 +00:00
|
|
|
ctx->queueEmpty = ctx->queueHead == ctx->queueTail;
|
2016-12-30 07:39:44 +00:00
|
|
|
/* Unlock the mutex, signal a pusher, and run the job */
|
2017-09-27 18:16:24 +00:00
|
|
|
ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
|
|
|
|
ZSTD_pthread_cond_signal(&ctx->queuePushCond);
|
2017-08-02 03:12:06 +00:00
|
|
|
|
2016-12-30 07:39:44 +00:00
|
|
|
job.function(job.opaque);
|
2017-07-31 17:10:16 +00:00
|
|
|
|
2017-08-02 03:12:06 +00:00
|
|
|
/* If the intended queue size was 0, signal after finishing job */
|
|
|
|
if (ctx->queueSize == 1) {
|
2017-09-27 18:16:24 +00:00
|
|
|
ZSTD_pthread_mutex_lock(&ctx->queueMutex);
|
2017-08-02 03:12:06 +00:00
|
|
|
ctx->numThreadsBusy--;
|
2017-09-27 18:16:24 +00:00
|
|
|
ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
|
|
|
|
ZSTD_pthread_cond_signal(&ctx->queuePushCond);
|
2017-08-19 22:07:54 +00:00
|
|
|
} }
|
|
|
|
} /* for (;;) */
|
2016-12-30 07:39:44 +00:00
|
|
|
/* Unreachable */
|
|
|
|
}
|
|
|
|
|
2017-09-29 01:14:28 +00:00
|
|
|
POOL_ctx* POOL_create(size_t numThreads, size_t queueSize) {
|
2017-08-25 00:01:41 +00:00
|
|
|
return POOL_create_advanced(numThreads, queueSize, ZSTD_defaultCMem);
|
|
|
|
}
|
|
|
|
|
2017-09-29 01:14:28 +00:00
|
|
|
POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize, ZSTD_customMem customMem) {
|
|
|
|
POOL_ctx* ctx;
|
2016-12-30 07:39:44 +00:00
|
|
|
/* Check the parameters */
|
2017-07-31 17:10:16 +00:00
|
|
|
if (!numThreads) { return NULL; }
|
2016-12-30 07:39:44 +00:00
|
|
|
/* Allocate the context and zero initialize */
|
2017-09-29 01:14:28 +00:00
|
|
|
ctx = (POOL_ctx*)ZSTD_calloc(sizeof(POOL_ctx), customMem);
|
2016-12-30 07:39:44 +00:00
|
|
|
if (!ctx) { return NULL; }
|
|
|
|
/* Initialize the job queue.
|
|
|
|
* It needs one extra space since one space is wasted to differentiate empty
|
|
|
|
* and full queues.
|
|
|
|
*/
|
|
|
|
ctx->queueSize = queueSize + 1;
|
2017-11-01 00:43:24 +00:00
|
|
|
ctx->queue = (POOL_job*)ZSTD_malloc(ctx->queueSize * sizeof(POOL_job), customMem);
|
2016-12-30 07:39:44 +00:00
|
|
|
ctx->queueHead = 0;
|
|
|
|
ctx->queueTail = 0;
|
2017-07-31 17:10:16 +00:00
|
|
|
ctx->numThreadsBusy = 0;
|
2017-08-01 19:24:55 +00:00
|
|
|
ctx->queueEmpty = 1;
|
2017-09-27 18:16:24 +00:00
|
|
|
(void)ZSTD_pthread_mutex_init(&ctx->queueMutex, NULL);
|
|
|
|
(void)ZSTD_pthread_cond_init(&ctx->queuePushCond, NULL);
|
|
|
|
(void)ZSTD_pthread_cond_init(&ctx->queuePopCond, NULL);
|
2016-12-30 07:39:44 +00:00
|
|
|
ctx->shutdown = 0;
|
|
|
|
/* Allocate space for the thread handles */
|
2017-09-27 18:16:24 +00:00
|
|
|
ctx->threads = (ZSTD_pthread_t*)ZSTD_malloc(numThreads * sizeof(ZSTD_pthread_t), customMem);
|
2016-12-30 07:39:44 +00:00
|
|
|
ctx->numThreads = 0;
|
2017-08-25 00:01:41 +00:00
|
|
|
ctx->customMem = customMem;
|
2016-12-30 07:39:44 +00:00
|
|
|
/* Check for errors */
|
2017-01-01 00:10:47 +00:00
|
|
|
if (!ctx->threads || !ctx->queue) { POOL_free(ctx); return NULL; }
|
2016-12-30 07:39:44 +00:00
|
|
|
/* Initialize the threads */
|
|
|
|
{ size_t i;
|
|
|
|
for (i = 0; i < numThreads; ++i) {
|
2017-09-27 18:16:24 +00:00
|
|
|
if (ZSTD_pthread_create(&ctx->threads[i], NULL, &POOL_thread, ctx)) {
|
2016-12-30 07:39:44 +00:00
|
|
|
ctx->numThreads = i;
|
|
|
|
POOL_free(ctx);
|
|
|
|
return NULL;
|
2016-12-31 02:31:26 +00:00
|
|
|
} }
|
2016-12-30 07:39:44 +00:00
|
|
|
ctx->numThreads = numThreads;
|
|
|
|
}
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! POOL_join() :
|
|
|
|
Shutdown the queue, wake any sleeping threads, and join all of the threads.
|
|
|
|
*/
|
2017-09-29 01:14:28 +00:00
|
|
|
static void POOL_join(POOL_ctx* ctx) {
|
2016-12-30 07:39:44 +00:00
|
|
|
/* Shut down the queue */
|
2017-09-27 18:16:24 +00:00
|
|
|
ZSTD_pthread_mutex_lock(&ctx->queueMutex);
|
2016-12-30 07:39:44 +00:00
|
|
|
ctx->shutdown = 1;
|
2017-09-27 18:16:24 +00:00
|
|
|
ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
|
2016-12-30 07:39:44 +00:00
|
|
|
/* Wake up sleeping threads */
|
2017-09-27 18:16:24 +00:00
|
|
|
ZSTD_pthread_cond_broadcast(&ctx->queuePushCond);
|
|
|
|
ZSTD_pthread_cond_broadcast(&ctx->queuePopCond);
|
2016-12-30 07:39:44 +00:00
|
|
|
/* Join all of the threads */
|
|
|
|
{ size_t i;
|
|
|
|
for (i = 0; i < ctx->numThreads; ++i) {
|
2017-09-27 18:16:24 +00:00
|
|
|
ZSTD_pthread_join(ctx->threads[i], NULL);
|
2016-12-31 02:31:26 +00:00
|
|
|
} }
|
2016-12-30 07:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void POOL_free(POOL_ctx *ctx) {
|
|
|
|
if (!ctx) { return; }
|
|
|
|
POOL_join(ctx);
|
2017-09-27 18:16:24 +00:00
|
|
|
ZSTD_pthread_mutex_destroy(&ctx->queueMutex);
|
|
|
|
ZSTD_pthread_cond_destroy(&ctx->queuePushCond);
|
|
|
|
ZSTD_pthread_cond_destroy(&ctx->queuePopCond);
|
2017-08-25 00:01:41 +00:00
|
|
|
ZSTD_free(ctx->queue, ctx->customMem);
|
|
|
|
ZSTD_free(ctx->threads, ctx->customMem);
|
|
|
|
ZSTD_free(ctx, ctx->customMem);
|
2016-12-30 07:39:44 +00:00
|
|
|
}
|
|
|
|
|
2017-06-02 00:56:14 +00:00
|
|
|
size_t POOL_sizeof(POOL_ctx *ctx) {
|
|
|
|
if (ctx==NULL) return 0; /* supports sizeof NULL */
|
|
|
|
return sizeof(*ctx)
|
|
|
|
+ ctx->queueSize * sizeof(POOL_job)
|
2017-09-27 18:16:24 +00:00
|
|
|
+ ctx->numThreads * sizeof(ZSTD_pthread_t);
|
2017-06-02 00:56:14 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 03:12:06 +00:00
|
|
|
/**
|
|
|
|
* Returns 1 if the queue is full and 0 otherwise.
|
|
|
|
*
|
|
|
|
* If the queueSize is 1 (the pool was created with an intended queueSize of 0),
|
|
|
|
* then a queue is empty if there is a thread free and no job is waiting.
|
|
|
|
*/
|
|
|
|
static int isQueueFull(POOL_ctx const* ctx) {
|
|
|
|
if (ctx->queueSize > 1) {
|
|
|
|
return ctx->queueHead == ((ctx->queueTail + 1) % ctx->queueSize);
|
|
|
|
} else {
|
|
|
|
return ctx->numThreadsBusy == ctx->numThreads ||
|
|
|
|
!ctx->queueEmpty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-11 21:14:07 +00:00
|
|
|
void POOL_add(void* ctxVoid, POOL_function function, void *opaque) {
|
|
|
|
POOL_ctx* const ctx = (POOL_ctx*)ctxVoid;
|
2016-12-30 07:39:44 +00:00
|
|
|
if (!ctx) { return; }
|
|
|
|
|
2017-09-27 18:16:24 +00:00
|
|
|
ZSTD_pthread_mutex_lock(&ctx->queueMutex);
|
2016-12-31 02:31:26 +00:00
|
|
|
{ POOL_job const job = {function, opaque};
|
2017-08-01 19:24:55 +00:00
|
|
|
|
2017-08-02 03:12:06 +00:00
|
|
|
/* Wait until there is space in the queue for the new job */
|
|
|
|
while (isQueueFull(ctx) && !ctx->shutdown) {
|
2017-09-27 18:16:24 +00:00
|
|
|
ZSTD_pthread_cond_wait(&ctx->queuePushCond, &ctx->queueMutex);
|
2016-12-30 07:39:44 +00:00
|
|
|
}
|
|
|
|
/* The queue is still going => there is space */
|
|
|
|
if (!ctx->shutdown) {
|
2017-08-01 19:24:55 +00:00
|
|
|
ctx->queueEmpty = 0;
|
2016-12-30 07:39:44 +00:00
|
|
|
ctx->queue[ctx->queueTail] = job;
|
2017-08-02 03:12:06 +00:00
|
|
|
ctx->queueTail = (ctx->queueTail + 1) % ctx->queueSize;
|
2016-12-30 07:39:44 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-27 18:16:24 +00:00
|
|
|
ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
|
|
|
|
ZSTD_pthread_cond_signal(&ctx->queuePopCond);
|
2016-12-30 07:39:44 +00:00
|
|
|
}
|
|
|
|
|
2017-01-19 22:05:07 +00:00
|
|
|
#else /* ZSTD_MULTITHREAD not defined */
|
2016-12-31 15:32:19 +00:00
|
|
|
/* No multi-threading support */
|
2016-12-30 07:39:44 +00:00
|
|
|
|
|
|
|
/* We don't need any data, but if it is empty malloc() might return NULL. */
|
2017-08-29 00:19:01 +00:00
|
|
|
struct POOL_ctx_s {
|
|
|
|
int dummy;
|
|
|
|
};
|
2017-08-25 00:01:41 +00:00
|
|
|
static POOL_ctx g_ctx;
|
2016-12-30 07:39:44 +00:00
|
|
|
|
2017-07-11 21:14:07 +00:00
|
|
|
POOL_ctx* POOL_create(size_t numThreads, size_t queueSize) {
|
2017-08-25 00:01:41 +00:00
|
|
|
return POOL_create_advanced(numThreads, queueSize, ZSTD_defaultCMem);
|
|
|
|
}
|
|
|
|
|
|
|
|
POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize, ZSTD_customMem customMem) {
|
2017-08-24 23:48:32 +00:00
|
|
|
(void)numThreads;
|
|
|
|
(void)queueSize;
|
2017-08-25 00:01:41 +00:00
|
|
|
(void)customMem;
|
|
|
|
return &g_ctx;
|
2016-12-30 07:39:44 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 21:14:07 +00:00
|
|
|
void POOL_free(POOL_ctx* ctx) {
|
2017-09-05 18:18:13 +00:00
|
|
|
assert(!ctx || ctx == &g_ctx);
|
2017-08-25 00:01:41 +00:00
|
|
|
(void)ctx;
|
2016-12-30 07:39:44 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 21:14:07 +00:00
|
|
|
void POOL_add(void* ctx, POOL_function function, void* opaque) {
|
2017-08-24 23:48:32 +00:00
|
|
|
(void)ctx;
|
|
|
|
function(opaque);
|
2016-12-30 07:39:44 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 21:14:07 +00:00
|
|
|
size_t POOL_sizeof(POOL_ctx* ctx) {
|
2017-06-02 18:36:47 +00:00
|
|
|
if (ctx==NULL) return 0; /* supports sizeof NULL */
|
2017-08-25 00:01:41 +00:00
|
|
|
assert(ctx == &g_ctx);
|
2017-06-02 18:36:47 +00:00
|
|
|
return sizeof(*ctx);
|
|
|
|
}
|
|
|
|
|
2017-01-19 22:05:07 +00:00
|
|
|
#endif /* ZSTD_MULTITHREAD */
|