Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 1 | /* |
Nick Terrell | 66e811d | 2021-01-04 17:53:52 -0500 | [diff] [blame] | 2 | * Copyright (c) 2016-2021, Yann Collet, Facebook, Inc. |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 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). |
Yann Collet | 3128e03 | 2017-09-08 00:09:23 -0700 | [diff] [blame] | 8 | * You may select, at your option, one of the above-listed licenses. |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 9 | */ |
| 10 | |
cyan4973 | 2e3b659 | 2017-01-20 14:00:41 -0800 | [diff] [blame] | 11 | |
| 12 | /* ====== Dependencies ======= */ |
Nick Terrell | 80f577b | 2020-08-06 20:18:05 -0700 | [diff] [blame] | 13 | #include "zstd_deps.h" /* size_t */ |
Yann Collet | 6b48eb1 | 2018-06-20 14:35:39 -0700 | [diff] [blame] | 14 | #include "debug.h" /* assert */ |
Nick Terrell | a686d30 | 2020-08-10 12:42:03 -0700 | [diff] [blame] | 15 | #include "zstd_internal.h" /* ZSTD_customMalloc, ZSTD_customFree */ |
Yann Collet | 6b48eb1 | 2018-06-20 14:35:39 -0700 | [diff] [blame] | 16 | #include "pool.h" |
cyan4973 | 2e3b659 | 2017-01-20 14:00:41 -0800 | [diff] [blame] | 17 | |
| 18 | /* ====== Compiler specifics ====== */ |
| 19 | #if defined(_MSC_VER) |
| 20 | # pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */ |
| 21 | #endif |
| 22 | |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 23 | |
Yann Collet | 0f984d9 | 2017-01-19 14:05:07 -0800 | [diff] [blame] | 24 | #ifdef ZSTD_MULTITHREAD |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 25 | |
Nick Terrell | e628eaf | 2017-01-26 15:29:10 -0800 | [diff] [blame] | 26 | #include "threading.h" /* pthread adaptation */ |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 27 | |
| 28 | /* A job is a function and an opaque argument */ |
| 29 | typedef struct POOL_job_s { |
Nick Terrell | 89dc856 | 2017-08-24 16:48:32 -0700 | [diff] [blame] | 30 | POOL_function function; |
| 31 | void *opaque; |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 32 | } POOL_job; |
| 33 | |
| 34 | struct POOL_ctx_s { |
Nick Terrell | 26dc040 | 2017-08-24 17:01:41 -0700 | [diff] [blame] | 35 | ZSTD_customMem customMem; |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 36 | /* Keep track of the threads */ |
Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 37 | ZSTD_pthread_t* threads; |
| 38 | size_t threadCapacity; |
| 39 | size_t threadLimit; |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 40 | |
| 41 | /* The queue is a circular buffer */ |
| 42 | POOL_job *queue; |
| 43 | size_t queueHead; |
| 44 | size_t queueTail; |
| 45 | size_t queueSize; |
Stella Lau | 5adceee | 2017-07-31 10:10:16 -0700 | [diff] [blame] | 46 | |
Stella Lau | 1d76da1 | 2017-08-01 12:24:55 -0700 | [diff] [blame] | 47 | /* The number of threads working on jobs */ |
| 48 | size_t numThreadsBusy; |
| 49 | /* Indicates if the queue is empty */ |
| 50 | int queueEmpty; |
Stella Lau | 5adceee | 2017-07-31 10:10:16 -0700 | [diff] [blame] | 51 | |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 52 | /* The mutex protects the queue */ |
Nick Terrell | 6c41adf | 2017-09-27 11:16:24 -0700 | [diff] [blame] | 53 | ZSTD_pthread_mutex_t queueMutex; |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 54 | /* Condition variable for pushers to wait on when the queue is full */ |
Nick Terrell | 6c41adf | 2017-09-27 11:16:24 -0700 | [diff] [blame] | 55 | ZSTD_pthread_cond_t queuePushCond; |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 56 | /* Condition variables for poppers to wait on when the queue is empty */ |
Nick Terrell | 6c41adf | 2017-09-27 11:16:24 -0700 | [diff] [blame] | 57 | ZSTD_pthread_cond_t queuePopCond; |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 58 | /* Indicates if the queue is shutting down */ |
| 59 | int shutdown; |
| 60 | }; |
| 61 | |
| 62 | /* POOL_thread() : |
Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 63 | * Work thread for the thread pool. |
| 64 | * Waits for jobs and executes them. |
| 65 | * @returns : NULL on failure else non-null. |
| 66 | */ |
Yann Collet | 3b29dbd | 2016-12-31 06:04:25 +0100 | [diff] [blame] | 67 | static void* POOL_thread(void* opaque) { |
| 68 | POOL_ctx* const ctx = (POOL_ctx*)opaque; |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 69 | if (!ctx) { return NULL; } |
| 70 | for (;;) { |
| 71 | /* Lock the mutex and wait for a non-empty queue or until shutdown */ |
Nick Terrell | 6c41adf | 2017-09-27 11:16:24 -0700 | [diff] [blame] | 72 | ZSTD_pthread_mutex_lock(&ctx->queueMutex); |
Stella Lau | 73ba589 | 2017-08-01 20:12:06 -0700 | [diff] [blame] | 73 | |
Yann Collet | 6b48eb1 | 2018-06-20 14:35:39 -0700 | [diff] [blame] | 74 | while ( ctx->queueEmpty |
| 75 | || (ctx->numThreadsBusy >= ctx->threadLimit) ) { |
Yann Collet | 818e72b | 2018-06-21 14:58:59 -0700 | [diff] [blame] | 76 | if (ctx->shutdown) { |
| 77 | /* even if !queueEmpty, (possible if numThreadsBusy >= threadLimit), |
| 78 | * a few threads will be shutdown while !queueEmpty, |
| 79 | * but enough threads will remain active to finish the queue */ |
| 80 | ZSTD_pthread_mutex_unlock(&ctx->queueMutex); |
| 81 | return opaque; |
| 82 | } |
Nick Terrell | 6c41adf | 2017-09-27 11:16:24 -0700 | [diff] [blame] | 83 | ZSTD_pthread_cond_wait(&ctx->queuePopCond, &ctx->queueMutex); |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 84 | } |
Yann Collet | c6a6417 | 2016-12-31 03:31:26 +0100 | [diff] [blame] | 85 | /* Pop a job off the queue */ |
Yann Collet | 7db5526 | 2017-08-19 15:07:54 -0700 | [diff] [blame] | 86 | { POOL_job const job = ctx->queue[ctx->queueHead]; |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 87 | ctx->queueHead = (ctx->queueHead + 1) % ctx->queueSize; |
Stella Lau | 5adceee | 2017-07-31 10:10:16 -0700 | [diff] [blame] | 88 | ctx->numThreadsBusy++; |
Stella Lau | 1d76da1 | 2017-08-01 12:24:55 -0700 | [diff] [blame] | 89 | ctx->queueEmpty = ctx->queueHead == ctx->queueTail; |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 90 | /* Unlock the mutex, signal a pusher, and run the job */ |
Nick Terrell | 6c41adf | 2017-09-27 11:16:24 -0700 | [diff] [blame] | 91 | ZSTD_pthread_cond_signal(&ctx->queuePushCond); |
Nick Terrell | a8daa2d | 2018-11-08 10:45:53 -0800 | [diff] [blame] | 92 | ZSTD_pthread_mutex_unlock(&ctx->queueMutex); |
Stella Lau | 73ba589 | 2017-08-01 20:12:06 -0700 | [diff] [blame] | 93 | |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 94 | job.function(job.opaque); |
Stella Lau | 5adceee | 2017-07-31 10:10:16 -0700 | [diff] [blame] | 95 | |
Stella Lau | 73ba589 | 2017-08-01 20:12:06 -0700 | [diff] [blame] | 96 | /* If the intended queue size was 0, signal after finishing job */ |
Yann Collet | 6de249c | 2018-06-20 17:18:57 -0700 | [diff] [blame] | 97 | ZSTD_pthread_mutex_lock(&ctx->queueMutex); |
| 98 | ctx->numThreadsBusy--; |
Stella Lau | 73ba589 | 2017-08-01 20:12:06 -0700 | [diff] [blame] | 99 | if (ctx->queueSize == 1) { |
Nick Terrell | 6c41adf | 2017-09-27 11:16:24 -0700 | [diff] [blame] | 100 | ZSTD_pthread_cond_signal(&ctx->queuePushCond); |
Yann Collet | 6de249c | 2018-06-20 17:18:57 -0700 | [diff] [blame] | 101 | } |
| 102 | ZSTD_pthread_mutex_unlock(&ctx->queueMutex); |
| 103 | } |
Yann Collet | 7db5526 | 2017-08-19 15:07:54 -0700 | [diff] [blame] | 104 | } /* for (;;) */ |
Yann Collet | 818e72b | 2018-06-21 14:58:59 -0700 | [diff] [blame] | 105 | assert(0); /* Unreachable */ |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Martin Liska | b684900 | 2020-09-25 14:12:14 +0200 | [diff] [blame] | 108 | POOL_ctx* ZSTD_createThreadPool(size_t numThreads) { |
| 109 | return POOL_create (numThreads, 0); |
| 110 | } |
| 111 | |
Yann Collet | 86b4fe5 | 2017-09-28 18:14:28 -0700 | [diff] [blame] | 112 | POOL_ctx* POOL_create(size_t numThreads, size_t queueSize) { |
Nick Terrell | 26dc040 | 2017-08-24 17:01:41 -0700 | [diff] [blame] | 113 | return POOL_create_advanced(numThreads, queueSize, ZSTD_defaultCMem); |
| 114 | } |
| 115 | |
Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 116 | POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize, |
| 117 | ZSTD_customMem customMem) { |
Yann Collet | 86b4fe5 | 2017-09-28 18:14:28 -0700 | [diff] [blame] | 118 | POOL_ctx* ctx; |
Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 119 | /* Check parameters */ |
Stella Lau | 5adceee | 2017-07-31 10:10:16 -0700 | [diff] [blame] | 120 | if (!numThreads) { return NULL; } |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 121 | /* Allocate the context and zero initialize */ |
Nick Terrell | a686d30 | 2020-08-10 12:42:03 -0700 | [diff] [blame] | 122 | ctx = (POOL_ctx*)ZSTD_customCalloc(sizeof(POOL_ctx), customMem); |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 123 | if (!ctx) { return NULL; } |
| 124 | /* Initialize the job queue. |
Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 125 | * It needs one extra space since one space is wasted to differentiate |
| 126 | * empty and full queues. |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 127 | */ |
| 128 | ctx->queueSize = queueSize + 1; |
Nick Terrell | a686d30 | 2020-08-10 12:42:03 -0700 | [diff] [blame] | 129 | ctx->queue = (POOL_job*)ZSTD_customMalloc(ctx->queueSize * sizeof(POOL_job), customMem); |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 130 | ctx->queueHead = 0; |
| 131 | ctx->queueTail = 0; |
Stella Lau | 5adceee | 2017-07-31 10:10:16 -0700 | [diff] [blame] | 132 | ctx->numThreadsBusy = 0; |
Stella Lau | 1d76da1 | 2017-08-01 12:24:55 -0700 | [diff] [blame] | 133 | ctx->queueEmpty = 1; |
Nick Terrell | 2438245 | 2019-10-18 12:33:45 -0700 | [diff] [blame] | 134 | { |
| 135 | int error = 0; |
| 136 | error |= ZSTD_pthread_mutex_init(&ctx->queueMutex, NULL); |
| 137 | error |= ZSTD_pthread_cond_init(&ctx->queuePushCond, NULL); |
| 138 | error |= ZSTD_pthread_cond_init(&ctx->queuePopCond, NULL); |
| 139 | if (error) { POOL_free(ctx); return NULL; } |
| 140 | } |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 141 | ctx->shutdown = 0; |
| 142 | /* Allocate space for the thread handles */ |
Nick Terrell | a686d30 | 2020-08-10 12:42:03 -0700 | [diff] [blame] | 143 | ctx->threads = (ZSTD_pthread_t*)ZSTD_customMalloc(numThreads * sizeof(ZSTD_pthread_t), customMem); |
Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 144 | ctx->threadCapacity = 0; |
Nick Terrell | 26dc040 | 2017-08-24 17:01:41 -0700 | [diff] [blame] | 145 | ctx->customMem = customMem; |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 146 | /* Check for errors */ |
Nick Terrell | bb13387 | 2016-12-31 19:10:47 -0500 | [diff] [blame] | 147 | if (!ctx->threads || !ctx->queue) { POOL_free(ctx); return NULL; } |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 148 | /* Initialize the threads */ |
| 149 | { size_t i; |
| 150 | for (i = 0; i < numThreads; ++i) { |
Nick Terrell | 6c41adf | 2017-09-27 11:16:24 -0700 | [diff] [blame] | 151 | if (ZSTD_pthread_create(&ctx->threads[i], NULL, &POOL_thread, ctx)) { |
Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 152 | ctx->threadCapacity = i; |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 153 | POOL_free(ctx); |
| 154 | return NULL; |
Yann Collet | c6a6417 | 2016-12-31 03:31:26 +0100 | [diff] [blame] | 155 | } } |
Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 156 | ctx->threadCapacity = numThreads; |
| 157 | ctx->threadLimit = numThreads; |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 158 | } |
| 159 | return ctx; |
| 160 | } |
| 161 | |
| 162 | /*! POOL_join() : |
| 163 | Shutdown the queue, wake any sleeping threads, and join all of the threads. |
| 164 | */ |
Yann Collet | 86b4fe5 | 2017-09-28 18:14:28 -0700 | [diff] [blame] | 165 | static void POOL_join(POOL_ctx* ctx) { |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 166 | /* Shut down the queue */ |
Nick Terrell | 6c41adf | 2017-09-27 11:16:24 -0700 | [diff] [blame] | 167 | ZSTD_pthread_mutex_lock(&ctx->queueMutex); |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 168 | ctx->shutdown = 1; |
Nick Terrell | 6c41adf | 2017-09-27 11:16:24 -0700 | [diff] [blame] | 169 | ZSTD_pthread_mutex_unlock(&ctx->queueMutex); |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 170 | /* Wake up sleeping threads */ |
Nick Terrell | 6c41adf | 2017-09-27 11:16:24 -0700 | [diff] [blame] | 171 | ZSTD_pthread_cond_broadcast(&ctx->queuePushCond); |
| 172 | ZSTD_pthread_cond_broadcast(&ctx->queuePopCond); |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 173 | /* Join all of the threads */ |
| 174 | { size_t i; |
Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 175 | for (i = 0; i < ctx->threadCapacity; ++i) { |
| 176 | ZSTD_pthread_join(ctx->threads[i], NULL); /* note : could fail */ |
Yann Collet | c6a6417 | 2016-12-31 03:31:26 +0100 | [diff] [blame] | 177 | } } |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | void POOL_free(POOL_ctx *ctx) { |
| 181 | if (!ctx) { return; } |
| 182 | POOL_join(ctx); |
Nick Terrell | 6c41adf | 2017-09-27 11:16:24 -0700 | [diff] [blame] | 183 | ZSTD_pthread_mutex_destroy(&ctx->queueMutex); |
| 184 | ZSTD_pthread_cond_destroy(&ctx->queuePushCond); |
| 185 | ZSTD_pthread_cond_destroy(&ctx->queuePopCond); |
Nick Terrell | a686d30 | 2020-08-10 12:42:03 -0700 | [diff] [blame] | 186 | ZSTD_customFree(ctx->queue, ctx->customMem); |
| 187 | ZSTD_customFree(ctx->threads, ctx->customMem); |
| 188 | ZSTD_customFree(ctx, ctx->customMem); |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Martin Liska | b684900 | 2020-09-25 14:12:14 +0200 | [diff] [blame] | 191 | void ZSTD_freeThreadPool (ZSTD_threadPool* pool) { |
| 192 | POOL_free (pool); |
| 193 | } |
Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 194 | |
Yann Collet | c4a5a21 | 2017-06-01 17:56:14 -0700 | [diff] [blame] | 195 | size_t POOL_sizeof(POOL_ctx *ctx) { |
| 196 | if (ctx==NULL) return 0; /* supports sizeof NULL */ |
| 197 | return sizeof(*ctx) |
| 198 | + ctx->queueSize * sizeof(POOL_job) |
Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 199 | + ctx->threadCapacity * sizeof(ZSTD_pthread_t); |
| 200 | } |
| 201 | |
| 202 | |
Yann Collet | fbd5dfc | 2018-06-22 12:14:59 -0700 | [diff] [blame] | 203 | /* @return : 0 on success, 1 on error */ |
| 204 | static int POOL_resize_internal(POOL_ctx* ctx, size_t numThreads) |
Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 205 | { |
Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 206 | if (numThreads <= ctx->threadCapacity) { |
Yann Collet | fbd5dfc | 2018-06-22 12:14:59 -0700 | [diff] [blame] | 207 | if (!numThreads) return 1; |
Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 208 | ctx->threadLimit = numThreads; |
Yann Collet | fbd5dfc | 2018-06-22 12:14:59 -0700 | [diff] [blame] | 209 | return 0; |
Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 210 | } |
| 211 | /* numThreads > threadCapacity */ |
Nick Terrell | a686d30 | 2020-08-10 12:42:03 -0700 | [diff] [blame] | 212 | { ZSTD_pthread_t* const threadPool = (ZSTD_pthread_t*)ZSTD_customMalloc(numThreads * sizeof(ZSTD_pthread_t), ctx->customMem); |
Yann Collet | fbd5dfc | 2018-06-22 12:14:59 -0700 | [diff] [blame] | 213 | if (!threadPool) return 1; |
Yann Collet | 4567c57 | 2018-06-19 16:03:12 -0700 | [diff] [blame] | 214 | /* replace existing thread pool */ |
Nick Terrell | c465f24 | 2020-08-10 12:46:38 -0700 | [diff] [blame] | 215 | ZSTD_memcpy(threadPool, ctx->threads, ctx->threadCapacity * sizeof(*threadPool)); |
Nick Terrell | a686d30 | 2020-08-10 12:42:03 -0700 | [diff] [blame] | 216 | ZSTD_customFree(ctx->threads, ctx->customMem); |
Yann Collet | 4567c57 | 2018-06-19 16:03:12 -0700 | [diff] [blame] | 217 | ctx->threads = threadPool; |
Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 218 | /* Initialize additional threads */ |
| 219 | { size_t threadId; |
| 220 | for (threadId = ctx->threadCapacity; threadId < numThreads; ++threadId) { |
| 221 | if (ZSTD_pthread_create(&threadPool[threadId], NULL, &POOL_thread, ctx)) { |
Yann Collet | 62469c9 | 2018-06-19 20:14:03 -0700 | [diff] [blame] | 222 | ctx->threadCapacity = threadId; |
Yann Collet | fbd5dfc | 2018-06-22 12:14:59 -0700 | [diff] [blame] | 223 | return 1; |
Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 224 | } } |
Yann Collet | 62469c9 | 2018-06-19 20:14:03 -0700 | [diff] [blame] | 225 | } } |
Yann Collet | 4567c57 | 2018-06-19 16:03:12 -0700 | [diff] [blame] | 226 | /* successfully expanded */ |
Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 227 | ctx->threadCapacity = numThreads; |
| 228 | ctx->threadLimit = numThreads; |
Yann Collet | fbd5dfc | 2018-06-22 12:14:59 -0700 | [diff] [blame] | 229 | return 0; |
Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Yann Collet | fbd5dfc | 2018-06-22 12:14:59 -0700 | [diff] [blame] | 232 | /* @return : 0 on success, 1 on error */ |
| 233 | int POOL_resize(POOL_ctx* ctx, size_t numThreads) |
Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 234 | { |
Yann Collet | fbd5dfc | 2018-06-22 12:14:59 -0700 | [diff] [blame] | 235 | int result; |
| 236 | if (ctx==NULL) return 1; |
Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 237 | ZSTD_pthread_mutex_lock(&ctx->queueMutex); |
Yann Collet | fbd5dfc | 2018-06-22 12:14:59 -0700 | [diff] [blame] | 238 | result = POOL_resize_internal(ctx, numThreads); |
Yann Collet | 243cd9d | 2018-06-21 18:04:58 -0700 | [diff] [blame] | 239 | ZSTD_pthread_cond_broadcast(&ctx->queuePopCond); |
Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 240 | ZSTD_pthread_mutex_unlock(&ctx->queueMutex); |
Yann Collet | fbd5dfc | 2018-06-22 12:14:59 -0700 | [diff] [blame] | 241 | return result; |
Yann Collet | c4a5a21 | 2017-06-01 17:56:14 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Stella Lau | 73ba589 | 2017-08-01 20:12:06 -0700 | [diff] [blame] | 244 | /** |
| 245 | * Returns 1 if the queue is full and 0 otherwise. |
| 246 | * |
Yann Collet | 1c714fd | 2018-06-18 20:46:39 -0700 | [diff] [blame] | 247 | * When queueSize is 1 (pool was created with an intended queueSize of 0), |
| 248 | * then a queue is empty if there is a thread free _and_ no job is waiting. |
Stella Lau | 73ba589 | 2017-08-01 20:12:06 -0700 | [diff] [blame] | 249 | */ |
| 250 | static int isQueueFull(POOL_ctx const* ctx) { |
| 251 | if (ctx->queueSize > 1) { |
| 252 | return ctx->queueHead == ((ctx->queueTail + 1) % ctx->queueSize); |
| 253 | } else { |
Yann Collet | 6b48eb1 | 2018-06-20 14:35:39 -0700 | [diff] [blame] | 254 | return (ctx->numThreadsBusy == ctx->threadLimit) || |
Stella Lau | 73ba589 | 2017-08-01 20:12:06 -0700 | [diff] [blame] | 255 | !ctx->queueEmpty; |
| 256 | } |
| 257 | } |
| 258 | |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 259 | |
Yann Collet | 997e4d0 | 2018-01-18 14:39:51 -0800 | [diff] [blame] | 260 | static void POOL_add_internal(POOL_ctx* ctx, POOL_function function, void *opaque) |
| 261 | { |
| 262 | POOL_job const job = {function, opaque}; |
| 263 | assert(ctx != NULL); |
| 264 | if (ctx->shutdown) return; |
Stella Lau | 1d76da1 | 2017-08-01 12:24:55 -0700 | [diff] [blame] | 265 | |
Yann Collet | 997e4d0 | 2018-01-18 14:39:51 -0800 | [diff] [blame] | 266 | ctx->queueEmpty = 0; |
| 267 | ctx->queue[ctx->queueTail] = job; |
| 268 | ctx->queueTail = (ctx->queueTail + 1) % ctx->queueSize; |
Nick Terrell | 6c41adf | 2017-09-27 11:16:24 -0700 | [diff] [blame] | 269 | ZSTD_pthread_cond_signal(&ctx->queuePopCond); |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Yann Collet | 997e4d0 | 2018-01-18 14:39:51 -0800 | [diff] [blame] | 272 | void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque) |
| 273 | { |
| 274 | assert(ctx != NULL); |
| 275 | ZSTD_pthread_mutex_lock(&ctx->queueMutex); |
| 276 | /* Wait until there is space in the queue for the new job */ |
| 277 | while (isQueueFull(ctx) && (!ctx->shutdown)) { |
| 278 | ZSTD_pthread_cond_wait(&ctx->queuePushCond, &ctx->queueMutex); |
| 279 | } |
| 280 | POOL_add_internal(ctx, function, opaque); |
| 281 | ZSTD_pthread_mutex_unlock(&ctx->queueMutex); |
| 282 | } |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 283 | |
Yann Collet | 997e4d0 | 2018-01-18 14:39:51 -0800 | [diff] [blame] | 284 | |
| 285 | int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque) |
| 286 | { |
| 287 | assert(ctx != NULL); |
| 288 | ZSTD_pthread_mutex_lock(&ctx->queueMutex); |
| 289 | if (isQueueFull(ctx)) { |
| 290 | ZSTD_pthread_mutex_unlock(&ctx->queueMutex); |
| 291 | return 0; |
| 292 | } |
| 293 | POOL_add_internal(ctx, function, opaque); |
| 294 | ZSTD_pthread_mutex_unlock(&ctx->queueMutex); |
| 295 | return 1; |
| 296 | } |
| 297 | |
| 298 | |
| 299 | #else /* ZSTD_MULTITHREAD not defined */ |
| 300 | |
| 301 | /* ========================== */ |
| 302 | /* No multi-threading support */ |
| 303 | /* ========================== */ |
| 304 | |
| 305 | |
| 306 | /* We don't need any data, but if it is empty, malloc() might return NULL. */ |
Nick Terrell | 02033be | 2017-08-28 17:19:01 -0700 | [diff] [blame] | 307 | struct POOL_ctx_s { |
| 308 | int dummy; |
| 309 | }; |
yoshihitoh | c6548ea | 2020-06-29 10:51:50 +0900 | [diff] [blame] | 310 | static POOL_ctx g_poolCtx; |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 311 | |
Yann Collet | 16261e6 | 2017-07-11 14:14:07 -0700 | [diff] [blame] | 312 | POOL_ctx* POOL_create(size_t numThreads, size_t queueSize) { |
Nick Terrell | 26dc040 | 2017-08-24 17:01:41 -0700 | [diff] [blame] | 313 | return POOL_create_advanced(numThreads, queueSize, ZSTD_defaultCMem); |
| 314 | } |
| 315 | |
| 316 | POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize, ZSTD_customMem customMem) { |
Nick Terrell | 89dc856 | 2017-08-24 16:48:32 -0700 | [diff] [blame] | 317 | (void)numThreads; |
| 318 | (void)queueSize; |
Nick Terrell | 26dc040 | 2017-08-24 17:01:41 -0700 | [diff] [blame] | 319 | (void)customMem; |
yoshihitoh | c6548ea | 2020-06-29 10:51:50 +0900 | [diff] [blame] | 320 | return &g_poolCtx; |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 321 | } |
| 322 | |
Yann Collet | 16261e6 | 2017-07-11 14:14:07 -0700 | [diff] [blame] | 323 | void POOL_free(POOL_ctx* ctx) { |
yoshihitoh | c6548ea | 2020-06-29 10:51:50 +0900 | [diff] [blame] | 324 | assert(!ctx || ctx == &g_poolCtx); |
Nick Terrell | 26dc040 | 2017-08-24 17:01:41 -0700 | [diff] [blame] | 325 | (void)ctx; |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Yann Collet | fbd5dfc | 2018-06-22 12:14:59 -0700 | [diff] [blame] | 328 | int POOL_resize(POOL_ctx* ctx, size_t numThreads) { |
| 329 | (void)ctx; (void)numThreads; |
| 330 | return 0; |
Yann Collet | 4567c57 | 2018-06-19 16:03:12 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Yann Collet | 997e4d0 | 2018-01-18 14:39:51 -0800 | [diff] [blame] | 333 | void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque) { |
Nick Terrell | 89dc856 | 2017-08-24 16:48:32 -0700 | [diff] [blame] | 334 | (void)ctx; |
| 335 | function(opaque); |
Nick Terrell | e777a5b | 2016-12-29 23:39:44 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Yann Collet | 997e4d0 | 2018-01-18 14:39:51 -0800 | [diff] [blame] | 338 | int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque) { |
| 339 | (void)ctx; |
| 340 | function(opaque); |
| 341 | return 1; |
| 342 | } |
| 343 | |
Yann Collet | 16261e6 | 2017-07-11 14:14:07 -0700 | [diff] [blame] | 344 | size_t POOL_sizeof(POOL_ctx* ctx) { |
Yann Collet | 6056e4c | 2017-06-02 11:36:47 -0700 | [diff] [blame] | 345 | if (ctx==NULL) return 0; /* supports sizeof NULL */ |
yoshihitoh | c6548ea | 2020-06-29 10:51:50 +0900 | [diff] [blame] | 346 | assert(ctx == &g_poolCtx); |
Yann Collet | 6056e4c | 2017-06-02 11:36:47 -0700 | [diff] [blame] | 347 | return sizeof(*ctx); |
| 348 | } |
| 349 | |
Yann Collet | 0f984d9 | 2017-01-19 14:05:07 -0800 | [diff] [blame] | 350 | #endif /* ZSTD_MULTITHREAD */ |