| 7081 | } |
| 7082 | |
| 7083 | POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize, |
| 7084 | ZSTD_customMem customMem) { |
| 7085 | POOL_ctx* ctx; |
| 7086 | /* Check parameters */ |
| 7087 | if (!numThreads) { return NULL; } |
| 7088 | /* Allocate the context and zero initialize */ |
| 7089 | ctx = (POOL_ctx*)ZSTD_calloc(sizeof(POOL_ctx), customMem); |
| 7090 | if (!ctx) { return NULL; } |
| 7091 | /* Initialize the job queue. |
| 7092 | * It needs one extra space since one space is wasted to differentiate |
| 7093 | * empty and full queues. |
| 7094 | */ |
| 7095 | ctx->queueSize = queueSize + 1; |
| 7096 | ctx->queue = (POOL_job*)ZSTD_malloc(ctx->queueSize * sizeof(POOL_job), customMem); |
| 7097 | ctx->queueHead = 0; |
| 7098 | ctx->queueTail = 0; |
| 7099 | ctx->numThreadsBusy = 0; |
| 7100 | ctx->queueEmpty = 1; |
| 7101 | { |
| 7102 | int error = 0; |
| 7103 | error |= ZSTD_pthread_mutex_init(&ctx->queueMutex, NULL); |
| 7104 | error |= ZSTD_pthread_cond_init(&ctx->queuePushCond, NULL); |
| 7105 | error |= ZSTD_pthread_cond_init(&ctx->queuePopCond, NULL); |
| 7106 | if (error) { POOL_free(ctx); return NULL; } |
| 7107 | } |
| 7108 | ctx->shutdown = 0; |
| 7109 | /* Allocate space for the thread handles */ |
| 7110 | ctx->threads = (ZSTD_pthread_t*)ZSTD_malloc(numThreads * sizeof(ZSTD_pthread_t), customMem); |
| 7111 | ctx->threadCapacity = 0; |
| 7112 | ctx->customMem = customMem; |
| 7113 | /* Check for errors */ |
| 7114 | if (!ctx->threads || !ctx->queue) { POOL_free(ctx); return NULL; } |
| 7115 | /* Initialize the threads */ |
| 7116 | { size_t i; |
| 7117 | for (i = 0; i < numThreads; ++i) { |
| 7118 | if (ZSTD_pthread_create(&ctx->threads[i], NULL, &POOL_thread, ctx)) { |
| 7119 | ctx->threadCapacity = i; |
| 7120 | POOL_free(ctx); |
| 7121 | return NULL; |
| 7122 | } } |
| 7123 | ctx->threadCapacity = numThreads; |
| 7124 | ctx->threadLimit = numThreads; |
| 7125 | } |
| 7126 | return ctx; |
| 7127 | } |
| 7128 | |
| 7129 | /*! POOL_join() : |
| 7130 | Shutdown the queue, wake any sleeping threads, and join all of the threads. |
no test coverage detected