pool.c 11 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 "debug.h" /* assert */
  13. #include "zstd_internal.h" /* ZSTD_malloc, ZSTD_free */
  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. if (ctx->queueSize == 1) {
  85. ZSTD_pthread_cond_signal(&ctx->queuePushCond);
  86. }
  87. ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
  88. }
  89. } /* for (;;) */
  90. assert(0); /* Unreachable */
  91. }
  92. POOL_ctx* POOL_create(size_t numThreads, size_t queueSize) {
  93. return POOL_create_advanced(numThreads, queueSize, ZSTD_defaultCMem);
  94. }
  95. POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize,
  96. ZSTD_customMem customMem) {
  97. POOL_ctx* ctx;
  98. /* Check parameters */
  99. if (!numThreads) { return NULL; }
  100. /* Allocate the context and zero initialize */
  101. ctx = (POOL_ctx*)ZSTD_calloc(sizeof(POOL_ctx), customMem);
  102. if (!ctx) { return NULL; }
  103. /* Initialize the job queue.
  104. * It needs one extra space since one space is wasted to differentiate
  105. * empty and full queues.
  106. */
  107. ctx->queueSize = queueSize + 1;
  108. ctx->queue = (POOL_job*)ZSTD_malloc(ctx->queueSize * sizeof(POOL_job), customMem);
  109. ctx->queueHead = 0;
  110. ctx->queueTail = 0;
  111. ctx->numThreadsBusy = 0;
  112. ctx->queueEmpty = 1;
  113. {
  114. int error = 0;
  115. error |= ZSTD_pthread_mutex_init(&ctx->queueMutex, NULL);
  116. error |= ZSTD_pthread_cond_init(&ctx->queuePushCond, NULL);
  117. error |= ZSTD_pthread_cond_init(&ctx->queuePopCond, NULL);
  118. if (error) { POOL_free(ctx); return NULL; }
  119. }
  120. ctx->shutdown = 0;
  121. /* Allocate space for the thread handles */
  122. ctx->threads = (ZSTD_pthread_t*)ZSTD_malloc(numThreads * sizeof(ZSTD_pthread_t), customMem);
  123. ctx->threadCapacity = 0;
  124. ctx->customMem = customMem;
  125. /* Check for errors */
  126. if (!ctx->threads || !ctx->queue) { POOL_free(ctx); return NULL; }
  127. /* Initialize the threads */
  128. { size_t i;
  129. for (i = 0; i < numThreads; ++i) {
  130. if (ZSTD_pthread_create(&ctx->threads[i], NULL, &POOL_thread, ctx)) {
  131. ctx->threadCapacity = i;
  132. POOL_free(ctx);
  133. return NULL;
  134. } }
  135. ctx->threadCapacity = numThreads;
  136. ctx->threadLimit = numThreads;
  137. }
  138. return ctx;
  139. }
  140. /*! POOL_join() :
  141. Shutdown the queue, wake any sleeping threads, and join all of the threads.
  142. */
  143. static void POOL_join(POOL_ctx* ctx) {
  144. /* Shut down the queue */
  145. ZSTD_pthread_mutex_lock(&ctx->queueMutex);
  146. ctx->shutdown = 1;
  147. ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
  148. /* Wake up sleeping threads */
  149. ZSTD_pthread_cond_broadcast(&ctx->queuePushCond);
  150. ZSTD_pthread_cond_broadcast(&ctx->queuePopCond);
  151. /* Join all of the threads */
  152. { size_t i;
  153. for (i = 0; i < ctx->threadCapacity; ++i) {
  154. ZSTD_pthread_join(ctx->threads[i], NULL); /* note : could fail */
  155. } }
  156. }
  157. void POOL_free(POOL_ctx *ctx) {
  158. if (!ctx) { return; }
  159. POOL_join(ctx);
  160. ZSTD_pthread_mutex_destroy(&ctx->queueMutex);
  161. ZSTD_pthread_cond_destroy(&ctx->queuePushCond);
  162. ZSTD_pthread_cond_destroy(&ctx->queuePopCond);
  163. ZSTD_free(ctx->queue, ctx->customMem);
  164. ZSTD_free(ctx->threads, ctx->customMem);
  165. ZSTD_free(ctx, ctx->customMem);
  166. }
  167. size_t POOL_sizeof(POOL_ctx *ctx) {
  168. if (ctx==NULL) return 0; /* supports sizeof NULL */
  169. return sizeof(*ctx)
  170. + ctx->queueSize * sizeof(POOL_job)
  171. + ctx->threadCapacity * sizeof(ZSTD_pthread_t);
  172. }
  173. /* @return : 0 on success, 1 on error */
  174. static int POOL_resize_internal(POOL_ctx* ctx, size_t numThreads)
  175. {
  176. if (numThreads <= ctx->threadCapacity) {
  177. if (!numThreads) return 1;
  178. ctx->threadLimit = numThreads;
  179. return 0;
  180. }
  181. /* numThreads > threadCapacity */
  182. { ZSTD_pthread_t* const threadPool = (ZSTD_pthread_t*)ZSTD_malloc(numThreads * sizeof(ZSTD_pthread_t), ctx->customMem);
  183. if (!threadPool) return 1;
  184. /* replace existing thread pool */
  185. memcpy(threadPool, ctx->threads, ctx->threadCapacity * sizeof(*threadPool));
  186. ZSTD_free(ctx->threads, ctx->customMem);
  187. ctx->threads = threadPool;
  188. /* Initialize additional threads */
  189. { size_t threadId;
  190. for (threadId = ctx->threadCapacity; threadId < numThreads; ++threadId) {
  191. if (ZSTD_pthread_create(&threadPool[threadId], NULL, &POOL_thread, ctx)) {
  192. ctx->threadCapacity = threadId;
  193. return 1;
  194. } }
  195. } }
  196. /* successfully expanded */
  197. ctx->threadCapacity = numThreads;
  198. ctx->threadLimit = numThreads;
  199. return 0;
  200. }
  201. /* @return : 0 on success, 1 on error */
  202. int POOL_resize(POOL_ctx* ctx, size_t numThreads)
  203. {
  204. int result;
  205. if (ctx==NULL) return 1;
  206. ZSTD_pthread_mutex_lock(&ctx->queueMutex);
  207. result = POOL_resize_internal(ctx, numThreads);
  208. ZSTD_pthread_cond_broadcast(&ctx->queuePopCond);
  209. ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
  210. return result;
  211. }
  212. /**
  213. * Returns 1 if the queue is full and 0 otherwise.
  214. *
  215. * When queueSize is 1 (pool was created with an intended queueSize of 0),
  216. * then a queue is empty if there is a thread free _and_ no job is waiting.
  217. */
  218. static int isQueueFull(POOL_ctx const* ctx) {
  219. if (ctx->queueSize > 1) {
  220. return ctx->queueHead == ((ctx->queueTail + 1) % ctx->queueSize);
  221. } else {
  222. return (ctx->numThreadsBusy == ctx->threadLimit) ||
  223. !ctx->queueEmpty;
  224. }
  225. }
  226. static void POOL_add_internal(POOL_ctx* ctx, POOL_function function, void *opaque)
  227. {
  228. POOL_job const job = {function, opaque};
  229. assert(ctx != NULL);
  230. if (ctx->shutdown) return;
  231. ctx->queueEmpty = 0;
  232. ctx->queue[ctx->queueTail] = job;
  233. ctx->queueTail = (ctx->queueTail + 1) % ctx->queueSize;
  234. ZSTD_pthread_cond_signal(&ctx->queuePopCond);
  235. }
  236. void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque)
  237. {
  238. assert(ctx != NULL);
  239. ZSTD_pthread_mutex_lock(&ctx->queueMutex);
  240. /* Wait until there is space in the queue for the new job */
  241. while (isQueueFull(ctx) && (!ctx->shutdown)) {
  242. ZSTD_pthread_cond_wait(&ctx->queuePushCond, &ctx->queueMutex);
  243. }
  244. POOL_add_internal(ctx, function, opaque);
  245. ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
  246. }
  247. int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque)
  248. {
  249. assert(ctx != NULL);
  250. ZSTD_pthread_mutex_lock(&ctx->queueMutex);
  251. if (isQueueFull(ctx)) {
  252. ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
  253. return 0;
  254. }
  255. POOL_add_internal(ctx, function, opaque);
  256. ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
  257. return 1;
  258. }
  259. #else /* ZSTD_MULTITHREAD not defined */
  260. /* ========================== */
  261. /* No multi-threading support */
  262. /* ========================== */
  263. /* We don't need any data, but if it is empty, malloc() might return NULL. */
  264. struct POOL_ctx_s {
  265. int dummy;
  266. };
  267. static POOL_ctx g_ctx;
  268. POOL_ctx* POOL_create(size_t numThreads, size_t queueSize) {
  269. return POOL_create_advanced(numThreads, queueSize, ZSTD_defaultCMem);
  270. }
  271. POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize, ZSTD_customMem customMem) {
  272. (void)numThreads;
  273. (void)queueSize;
  274. (void)customMem;
  275. return &g_ctx;
  276. }
  277. void POOL_free(POOL_ctx* ctx) {
  278. assert(!ctx || ctx == &g_ctx);
  279. (void)ctx;
  280. }
  281. int POOL_resize(POOL_ctx* ctx, size_t numThreads) {
  282. (void)ctx; (void)numThreads;
  283. return 0;
  284. }
  285. void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque) {
  286. (void)ctx;
  287. function(opaque);
  288. }
  289. int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque) {
  290. (void)ctx;
  291. function(opaque);
  292. return 1;
  293. }
  294. size_t POOL_sizeof(POOL_ctx* ctx) {
  295. if (ctx==NULL) return 0; /* supports sizeof NULL */
  296. assert(ctx == &g_ctx);
  297. return sizeof(*ctx);
  298. }
  299. #endif /* ZSTD_MULTITHREAD */