pool.c 11 KB

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