Lines Matching refs:ctx
68 POOL_ctx* const ctx = (POOL_ctx*)opaque; in POOL_thread() local
69 if (!ctx) { return NULL; } in POOL_thread()
72 ZSTD_pthread_mutex_lock(&ctx->queueMutex); in POOL_thread()
74 while ( ctx->queueEmpty in POOL_thread()
75 || (ctx->numThreadsBusy >= ctx->threadLimit) ) { in POOL_thread()
76 if (ctx->shutdown) { in POOL_thread()
80 ZSTD_pthread_mutex_unlock(&ctx->queueMutex); in POOL_thread()
83 ZSTD_pthread_cond_wait(&ctx->queuePopCond, &ctx->queueMutex); in POOL_thread()
86 { POOL_job const job = ctx->queue[ctx->queueHead]; in POOL_thread()
87 ctx->queueHead = (ctx->queueHead + 1) % ctx->queueSize; in POOL_thread()
88 ctx->numThreadsBusy++; in POOL_thread()
89 ctx->queueEmpty = (ctx->queueHead == ctx->queueTail); in POOL_thread()
91 ZSTD_pthread_cond_signal(&ctx->queuePushCond); in POOL_thread()
92 ZSTD_pthread_mutex_unlock(&ctx->queueMutex); in POOL_thread()
97 ZSTD_pthread_mutex_lock(&ctx->queueMutex); in POOL_thread()
98 ctx->numThreadsBusy--; in POOL_thread()
99 if (ctx->queueSize == 1) { in POOL_thread()
100 ZSTD_pthread_cond_signal(&ctx->queuePushCond); in POOL_thread()
102 ZSTD_pthread_mutex_unlock(&ctx->queueMutex); in POOL_thread()
120 POOL_ctx* ctx; in POOL_create_advanced() local
124 ctx = (POOL_ctx*)ZSTD_customCalloc(sizeof(POOL_ctx), customMem); in POOL_create_advanced()
125 if (!ctx) { return NULL; } in POOL_create_advanced()
130 ctx->queueSize = queueSize + 1; in POOL_create_advanced()
131 ctx->queue = (POOL_job*)ZSTD_customMalloc(ctx->queueSize * sizeof(POOL_job), customMem); in POOL_create_advanced()
132 ctx->queueHead = 0; in POOL_create_advanced()
133 ctx->queueTail = 0; in POOL_create_advanced()
134 ctx->numThreadsBusy = 0; in POOL_create_advanced()
135 ctx->queueEmpty = 1; in POOL_create_advanced()
138 error |= ZSTD_pthread_mutex_init(&ctx->queueMutex, NULL); in POOL_create_advanced()
139 error |= ZSTD_pthread_cond_init(&ctx->queuePushCond, NULL); in POOL_create_advanced()
140 error |= ZSTD_pthread_cond_init(&ctx->queuePopCond, NULL); in POOL_create_advanced()
141 if (error) { POOL_free(ctx); return NULL; } in POOL_create_advanced()
143 ctx->shutdown = 0; in POOL_create_advanced()
145 … ctx->threads = (ZSTD_pthread_t*)ZSTD_customMalloc(numThreads * sizeof(ZSTD_pthread_t), customMem); in POOL_create_advanced()
146 ctx->threadCapacity = 0; in POOL_create_advanced()
147 ctx->customMem = customMem; in POOL_create_advanced()
149 if (!ctx->threads || !ctx->queue) { POOL_free(ctx); return NULL; } in POOL_create_advanced()
153 if (ZSTD_pthread_create(&ctx->threads[i], NULL, &POOL_thread, ctx)) { in POOL_create_advanced()
154 ctx->threadCapacity = i; in POOL_create_advanced()
155 POOL_free(ctx); in POOL_create_advanced()
158 ctx->threadCapacity = numThreads; in POOL_create_advanced()
159 ctx->threadLimit = numThreads; in POOL_create_advanced()
161 return ctx; in POOL_create_advanced()
167 static void POOL_join(POOL_ctx* ctx) { in POOL_join() argument
169 ZSTD_pthread_mutex_lock(&ctx->queueMutex); in POOL_join()
170 ctx->shutdown = 1; in POOL_join()
171 ZSTD_pthread_mutex_unlock(&ctx->queueMutex); in POOL_join()
173 ZSTD_pthread_cond_broadcast(&ctx->queuePushCond); in POOL_join()
174 ZSTD_pthread_cond_broadcast(&ctx->queuePopCond); in POOL_join()
177 for (i = 0; i < ctx->threadCapacity; ++i) { in POOL_join()
178 ZSTD_pthread_join(ctx->threads[i], NULL); /* note : could fail */ in POOL_join()
182 void POOL_free(POOL_ctx *ctx) { in POOL_free() argument
183 if (!ctx) { return; } in POOL_free()
184 POOL_join(ctx); in POOL_free()
185 ZSTD_pthread_mutex_destroy(&ctx->queueMutex); in POOL_free()
186 ZSTD_pthread_cond_destroy(&ctx->queuePushCond); in POOL_free()
187 ZSTD_pthread_cond_destroy(&ctx->queuePopCond); in POOL_free()
188 ZSTD_customFree(ctx->queue, ctx->customMem); in POOL_free()
189 ZSTD_customFree(ctx->threads, ctx->customMem); in POOL_free()
190 ZSTD_customFree(ctx, ctx->customMem); in POOL_free()
197 size_t POOL_sizeof(const POOL_ctx* ctx) { in POOL_sizeof() argument
198 if (ctx==NULL) return 0; /* supports sizeof NULL */ in POOL_sizeof()
199 return sizeof(*ctx) in POOL_sizeof()
200 + ctx->queueSize * sizeof(POOL_job) in POOL_sizeof()
201 + ctx->threadCapacity * sizeof(ZSTD_pthread_t); in POOL_sizeof()
206 static int POOL_resize_internal(POOL_ctx* ctx, size_t numThreads) in POOL_resize_internal() argument
208 if (numThreads <= ctx->threadCapacity) { in POOL_resize_internal()
210 ctx->threadLimit = numThreads; in POOL_resize_internal()
214 …readPool = (ZSTD_pthread_t*)ZSTD_customMalloc(numThreads * sizeof(ZSTD_pthread_t), ctx->customMem); in POOL_resize_internal()
217 ZSTD_memcpy(threadPool, ctx->threads, ctx->threadCapacity * sizeof(*threadPool)); in POOL_resize_internal()
218 ZSTD_customFree(ctx->threads, ctx->customMem); in POOL_resize_internal()
219 ctx->threads = threadPool; in POOL_resize_internal()
222 for (threadId = ctx->threadCapacity; threadId < numThreads; ++threadId) { in POOL_resize_internal()
223 if (ZSTD_pthread_create(&threadPool[threadId], NULL, &POOL_thread, ctx)) { in POOL_resize_internal()
224 ctx->threadCapacity = threadId; in POOL_resize_internal()
229 ctx->threadCapacity = numThreads; in POOL_resize_internal()
230 ctx->threadLimit = numThreads; in POOL_resize_internal()
235 int POOL_resize(POOL_ctx* ctx, size_t numThreads) in POOL_resize() argument
238 if (ctx==NULL) return 1; in POOL_resize()
239 ZSTD_pthread_mutex_lock(&ctx->queueMutex); in POOL_resize()
240 result = POOL_resize_internal(ctx, numThreads); in POOL_resize()
241 ZSTD_pthread_cond_broadcast(&ctx->queuePopCond); in POOL_resize()
242 ZSTD_pthread_mutex_unlock(&ctx->queueMutex); in POOL_resize()
252 static int isQueueFull(POOL_ctx const* ctx) { in isQueueFull() argument
253 if (ctx->queueSize > 1) { in isQueueFull()
254 return ctx->queueHead == ((ctx->queueTail + 1) % ctx->queueSize); in isQueueFull()
256 return (ctx->numThreadsBusy == ctx->threadLimit) || in isQueueFull()
257 !ctx->queueEmpty; in isQueueFull()
263 POOL_add_internal(POOL_ctx* ctx, POOL_function function, void *opaque) in POOL_add_internal() argument
266 assert(ctx != NULL); in POOL_add_internal()
267 if (ctx->shutdown) return; in POOL_add_internal()
269 ctx->queueEmpty = 0; in POOL_add_internal()
270 ctx->queue[ctx->queueTail] = job; in POOL_add_internal()
271 ctx->queueTail = (ctx->queueTail + 1) % ctx->queueSize; in POOL_add_internal()
272 ZSTD_pthread_cond_signal(&ctx->queuePopCond); in POOL_add_internal()
275 void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque) in POOL_add() argument
277 assert(ctx != NULL); in POOL_add()
278 ZSTD_pthread_mutex_lock(&ctx->queueMutex); in POOL_add()
280 while (isQueueFull(ctx) && (!ctx->shutdown)) { in POOL_add()
281 ZSTD_pthread_cond_wait(&ctx->queuePushCond, &ctx->queueMutex); in POOL_add()
283 POOL_add_internal(ctx, function, opaque); in POOL_add()
284 ZSTD_pthread_mutex_unlock(&ctx->queueMutex); in POOL_add()
288 int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque) in POOL_tryAdd() argument
290 assert(ctx != NULL); in POOL_tryAdd()
291 ZSTD_pthread_mutex_lock(&ctx->queueMutex); in POOL_tryAdd()
292 if (isQueueFull(ctx)) { in POOL_tryAdd()
293 ZSTD_pthread_mutex_unlock(&ctx->queueMutex); in POOL_tryAdd()
296 POOL_add_internal(ctx, function, opaque); in POOL_tryAdd()
297 ZSTD_pthread_mutex_unlock(&ctx->queueMutex); in POOL_tryAdd()
328 void POOL_free(POOL_ctx* ctx) { in POOL_free() argument
329 assert(!ctx || ctx == &g_poolCtx); in POOL_free()
330 (void)ctx; in POOL_free()
333 int POOL_resize(POOL_ctx* ctx, size_t numThreads) { in POOL_resize() argument
334 (void)ctx; (void)numThreads; in POOL_resize()
338 void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque) { in POOL_add() argument
339 (void)ctx; in POOL_add()
343 int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque) { in POOL_tryAdd() argument
344 (void)ctx; in POOL_tryAdd()
349 size_t POOL_sizeof(const POOL_ctx* ctx) { in POOL_sizeof() argument
350 if (ctx==NULL) return 0; /* supports sizeof NULL */ in POOL_sizeof()
351 assert(ctx == &g_poolCtx); in POOL_sizeof()
352 return sizeof(*ctx); in POOL_sizeof()