| 114 | } |
| 115 | |
| 116 | POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize, |
| 117 | ZSTD_customMem customMem) { |
| 118 | POOL_ctx* ctx; |
| 119 | /* Check parameters */ |
| 120 | if (!numThreads) { return NULL; } |
| 121 | /* Allocate the context and zero initialize */ |
| 122 | ctx = (POOL_ctx*)ZSTD_customCalloc(sizeof(POOL_ctx), customMem); |
| 123 | if (!ctx) { return NULL; } |
| 124 | /* Initialize the job queue. |
| 125 | * It needs one extra space since one space is wasted to differentiate |
| 126 | * empty and full queues. |
| 127 | */ |
| 128 | ctx->queueSize = queueSize + 1; |
| 129 | ctx->queue = (POOL_job*)ZSTD_customMalloc(ctx->queueSize * sizeof(POOL_job), customMem); |
| 130 | ctx->queueHead = 0; |
| 131 | ctx->queueTail = 0; |
| 132 | ctx->numThreadsBusy = 0; |
| 133 | ctx->queueEmpty = 1; |
| 134 | { |
| 135 | int error = 0; |
| 136 | error |= ZSTD_pthread_mutex_init(&ctx->queueMutex, NULL); |
| 137 | error |= ZSTD_pthread_cond_init(&ctx->queuePushCond, NULL); |
| 138 | error |= ZSTD_pthread_cond_init(&ctx->queuePopCond, NULL); |
| 139 | if (error) { POOL_free(ctx); return NULL; } |
| 140 | } |
| 141 | ctx->shutdown = 0; |
| 142 | /* Allocate space for the thread handles */ |
| 143 | ctx->threads = (ZSTD_pthread_t*)ZSTD_customMalloc(numThreads * sizeof(ZSTD_pthread_t), customMem); |
| 144 | ctx->threadCapacity = 0; |
| 145 | ctx->customMem = customMem; |
| 146 | /* Check for errors */ |
| 147 | if (!ctx->threads || !ctx->queue) { POOL_free(ctx); return NULL; } |
| 148 | /* Initialize the threads */ |
| 149 | { size_t i; |
| 150 | for (i = 0; i < numThreads; ++i) { |
| 151 | if (ZSTD_pthread_create(&ctx->threads[i], NULL, &POOL_thread, ctx)) { |
| 152 | ctx->threadCapacity = i; |
| 153 | POOL_free(ctx); |
| 154 | return NULL; |
| 155 | } } |
| 156 | ctx->threadCapacity = numThreads; |
| 157 | ctx->threadLimit = numThreads; |
| 158 | } |
| 159 | return ctx; |
| 160 | } |
| 161 | |
| 162 | /*! POOL_join() : |
| 163 | Shutdown the queue, wake any sleeping threads, and join all of the threads. |
no test coverage detected