pool.c 10 KB

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