1 /* 2 * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc. 3 * All rights reserved. 4 * 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. 9 */ 10 11 12 /* ====== Dependencies ======= */ 13 #include "zstd_deps.h" /* size_t */ 14 #include "debug.h" /* assert */ 15 #include "zstd_internal.h" /* ZSTD_customMalloc, ZSTD_customFree */ 16 #include "pool.h" 17 18 /* ====== Compiler specifics ====== */ 19 #if defined(_MSC_VER) 20 # pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */ 21 #endif 22 23 24 #ifdef ZSTD_MULTITHREAD 25 26 #include "threading.h" /* pthread adaptation */ 27 28 /* A job is a function and an opaque argument */ 29 typedef struct POOL_job_s { 30 POOL_function function; 31 void *opaque; 32 } POOL_job; 33 34 struct POOL_ctx_s { 35 ZSTD_customMem customMem; 36 /* Keep track of the threads */ 37 ZSTD_pthread_t* threads; 38 size_t threadCapacity; 39 size_t threadLimit; 40 41 /* The queue is a circular buffer */ 42 POOL_job *queue; 43 size_t queueHead; 44 size_t queueTail; 45 size_t queueSize; 46 47 /* The number of threads working on jobs */ 48 size_t numThreadsBusy; 49 /* Indicates if the queue is empty */ 50 int queueEmpty; 51 52 /* The mutex protects the queue */ 53 ZSTD_pthread_mutex_t queueMutex; 54 /* Condition variable for pushers to wait on when the queue is full */ 55 ZSTD_pthread_cond_t queuePushCond; 56 /* Condition variables for poppers to wait on when the queue is empty */ 57 ZSTD_pthread_cond_t queuePopCond; 58 /* Indicates if the queue is shutting down */ 59 int shutdown; 60 }; 61 62 /* POOL_thread() : 63 * Work thread for the thread pool. 64 * Waits for jobs and executes them. 65 * @returns : NULL on failure else non-null. 66 */ 67 static void* POOL_thread(void* opaque) { 68 POOL_ctx* const ctx = (POOL_ctx*)opaque; 69 if (!ctx) { return NULL; } 70 for (;;) { 71 /* Lock the mutex and wait for a non-empty queue or until shutdown */ 72 ZSTD_pthread_mutex_lock(&ctx->queueMutex); 73 74 while ( ctx->queueEmpty 75 || (ctx->numThreadsBusy >= ctx->threadLimit) ) { 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 } 83 ZSTD_pthread_cond_wait(&ctx->queuePopCond, &ctx->queueMutex); 84 } 85 /* Pop a job off the queue */ 86 { POOL_job const job = ctx->queue[ctx->queueHead]; 87 ctx->queueHead = (ctx->queueHead + 1) % ctx->queueSize; 88 ctx->numThreadsBusy++; 89 ctx->queueEmpty = ctx->queueHead == ctx->queueTail; 90 /* Unlock the mutex, signal a pusher, and run the job */ 91 ZSTD_pthread_cond_signal(&ctx->queuePushCond); 92 ZSTD_pthread_mutex_unlock(&ctx->queueMutex); 93 94 job.function(job.opaque); 95 96 /* If the intended queue size was 0, signal after finishing job */ 97 ZSTD_pthread_mutex_lock(&ctx->queueMutex); 98 ctx->numThreadsBusy--; 99 if (ctx->queueSize == 1) { 100 ZSTD_pthread_cond_signal(&ctx->queuePushCond); 101 } 102 ZSTD_pthread_mutex_unlock(&ctx->queueMutex); 103 } 104 } /* for (;;) */ 105 assert(0); /* Unreachable */ 106 } 107 108 POOL_ctx* ZSTD_createThreadPool(size_t numThreads) { 109 return POOL_create (numThreads, 0); 110 } 111 112 POOL_ctx* POOL_create(size_t numThreads, size_t queueSize) { 113 return POOL_create_advanced(numThreads, queueSize, ZSTD_defaultCMem); 114 } 115 116 POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize, 117 ZSTD_customMem customMem) { 118 POOL_ctx* ctx; 119 /* Check parameters */ 120 if (!numThreads) { return NULL; } 121 /* Allocate the context and zero initialize */ 122 ctx = (POOL_ctx*)ZSTD_customCalloc(sizeof(POOL_ctx), customMem); 123 if (!ctx) { return NULL; } 124 /* Initialize the job queue. 125 * It needs one extra space since one space is wasted to differentiate 126 * empty and full queues. 127 */ 128 ctx->queueSize = queueSize + 1; 129 ctx->queue = (POOL_job*)ZSTD_customMalloc(ctx->queueSize * sizeof(POOL_job), customMem); 130 ctx->queueHead = 0; 131 ctx->queueTail = 0; 132 ctx->numThreadsBusy = 0; 133 ctx->queueEmpty = 1; 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 } 141 ctx->shutdown = 0; 142 /* Allocate space for the thread handles */ 143 ctx->threads = (ZSTD_pthread_t*)ZSTD_customMalloc(numThreads * sizeof(ZSTD_pthread_t), customMem); 144 ctx->threadCapacity = 0; 145 ctx->customMem = customMem; 146 /* Check for errors */ 147 if (!ctx->threads || !ctx->queue) { POOL_free(ctx); return NULL; } 148 /* Initialize the threads */ 149 { size_t i; 150 for (i = 0; i < numThreads; ++i) { 151 if (ZSTD_pthread_create(&ctx->threads[i], NULL, &POOL_thread, ctx)) { 152 ctx->threadCapacity = i; 153 POOL_free(ctx); 154 return NULL; 155 } } 156 ctx->threadCapacity = numThreads; 157 ctx->threadLimit = numThreads; 158 } 159 return ctx; 160 } 161 162 /*! POOL_join() : 163 Shutdown the queue, wake any sleeping threads, and join all of the threads. 164 */ 165 static void POOL_join(POOL_ctx* ctx) { 166 /* Shut down the queue */ 167 ZSTD_pthread_mutex_lock(&ctx->queueMutex); 168 ctx->shutdown = 1; 169 ZSTD_pthread_mutex_unlock(&ctx->queueMutex); 170 /* Wake up sleeping threads */ 171 ZSTD_pthread_cond_broadcast(&ctx->queuePushCond); 172 ZSTD_pthread_cond_broadcast(&ctx->queuePopCond); 173 /* Join all of the threads */ 174 { size_t i; 175 for (i = 0; i < ctx->threadCapacity; ++i) { 176 ZSTD_pthread_join(ctx->threads[i], NULL); /* note : could fail */ 177 } } 178 } 179 180 void POOL_free(POOL_ctx *ctx) { 181 if (!ctx) { return; } 182 POOL_join(ctx); 183 ZSTD_pthread_mutex_destroy(&ctx->queueMutex); 184 ZSTD_pthread_cond_destroy(&ctx->queuePushCond); 185 ZSTD_pthread_cond_destroy(&ctx->queuePopCond); 186 ZSTD_customFree(ctx->queue, ctx->customMem); 187 ZSTD_customFree(ctx->threads, ctx->customMem); 188 ZSTD_customFree(ctx, ctx->customMem); 189 } 190 191 void ZSTD_freeThreadPool (ZSTD_threadPool* pool) { 192 POOL_free (pool); 193 } 194 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) 199 + ctx->threadCapacity * sizeof(ZSTD_pthread_t); 200 } 201 202 203 /* @return : 0 on success, 1 on error */ 204 static int POOL_resize_internal(POOL_ctx* ctx, size_t numThreads) 205 { 206 if (numThreads <= ctx->threadCapacity) { 207 if (!numThreads) return 1; 208 ctx->threadLimit = numThreads; 209 return 0; 210 } 211 /* numThreads > threadCapacity */ 212 { ZSTD_pthread_t* const threadPool = (ZSTD_pthread_t*)ZSTD_customMalloc(numThreads * sizeof(ZSTD_pthread_t), ctx->customMem); 213 if (!threadPool) return 1; 214 /* replace existing thread pool */ 215 ZSTD_memcpy(threadPool, ctx->threads, ctx->threadCapacity * sizeof(*threadPool)); 216 ZSTD_customFree(ctx->threads, ctx->customMem); 217 ctx->threads = threadPool; 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)) { 222 ctx->threadCapacity = threadId; 223 return 1; 224 } } 225 } } 226 /* successfully expanded */ 227 ctx->threadCapacity = numThreads; 228 ctx->threadLimit = numThreads; 229 return 0; 230 } 231 232 /* @return : 0 on success, 1 on error */ 233 int POOL_resize(POOL_ctx* ctx, size_t numThreads) 234 { 235 int result; 236 if (ctx==NULL) return 1; 237 ZSTD_pthread_mutex_lock(&ctx->queueMutex); 238 result = POOL_resize_internal(ctx, numThreads); 239 ZSTD_pthread_cond_broadcast(&ctx->queuePopCond); 240 ZSTD_pthread_mutex_unlock(&ctx->queueMutex); 241 return result; 242 } 243 244 /** 245 * Returns 1 if the queue is full and 0 otherwise. 246 * 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. 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 { 254 return (ctx->numThreadsBusy == ctx->threadLimit) || 255 !ctx->queueEmpty; 256 } 257 } 258 259 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; 265 266 ctx->queueEmpty = 0; 267 ctx->queue[ctx->queueTail] = job; 268 ctx->queueTail = (ctx->queueTail + 1) % ctx->queueSize; 269 ZSTD_pthread_cond_signal(&ctx->queuePopCond); 270 } 271 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 } 283 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. */ 307 struct POOL_ctx_s { 308 int dummy; 309 }; 310 static POOL_ctx g_poolCtx; 311 312 POOL_ctx* POOL_create(size_t numThreads, size_t queueSize) { 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) { 317 (void)numThreads; 318 (void)queueSize; 319 (void)customMem; 320 return &g_poolCtx; 321 } 322 323 void POOL_free(POOL_ctx* ctx) { 324 assert(!ctx || ctx == &g_poolCtx); 325 (void)ctx; 326 } 327 328 int POOL_resize(POOL_ctx* ctx, size_t numThreads) { 329 (void)ctx; (void)numThreads; 330 return 0; 331 } 332 333 void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque) { 334 (void)ctx; 335 function(opaque); 336 } 337 338 int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque) { 339 (void)ctx; 340 function(opaque); 341 return 1; 342 } 343 344 size_t POOL_sizeof(POOL_ctx* ctx) { 345 if (ctx==NULL) return 0; /* supports sizeof NULL */ 346 assert(ctx == &g_poolCtx); 347 return sizeof(*ctx); 348 } 349 350 #endif /* ZSTD_MULTITHREAD */ 351