You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pool.c 11KB

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