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