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()
118 POOL_ctx* ctx; in POOL_create_advanced() local
122 ctx = (POOL_ctx*)ZSTD_customCalloc(sizeof(POOL_ctx), customMem); in POOL_create_advanced()
123 if (!ctx) { return NULL; } in POOL_create_advanced()
128 ctx->queueSize = queueSize + 1; in POOL_create_advanced()
129 ctx->queue = (POOL_job*)ZSTD_customMalloc(ctx->queueSize * sizeof(POOL_job), customMem); in POOL_create_advanced()
130 ctx->queueHead = 0; in POOL_create_advanced()
131 ctx->queueTail = 0; in POOL_create_advanced()
132 ctx->numThreadsBusy = 0; in POOL_create_advanced()
133 ctx->queueEmpty = 1; in POOL_create_advanced()
136 error |= ZSTD_pthread_mutex_init(&ctx->queueMutex, NULL); in POOL_create_advanced()
137 error |= ZSTD_pthread_cond_init(&ctx->queuePushCond, NULL); in POOL_create_advanced()
138 error |= ZSTD_pthread_cond_init(&ctx->queuePopCond, NULL); in POOL_create_advanced()
139 if (error) { POOL_free(ctx); return NULL; } in POOL_create_advanced()
141 ctx->shutdown = 0; in POOL_create_advanced()
143 … ctx->threads = (ZSTD_pthread_t*)ZSTD_customMalloc(numThreads * sizeof(ZSTD_pthread_t), customMem); in POOL_create_advanced()
144 ctx->threadCapacity = 0; in POOL_create_advanced()
145 ctx->customMem = customMem; in POOL_create_advanced()
147 if (!ctx->threads || !ctx->queue) { POOL_free(ctx); return NULL; } in POOL_create_advanced()
151 if (ZSTD_pthread_create(&ctx->threads[i], NULL, &POOL_thread, ctx)) { in POOL_create_advanced()
152 ctx->threadCapacity = i; in POOL_create_advanced()
153 POOL_free(ctx); in POOL_create_advanced()
156 ctx->threadCapacity = numThreads; in POOL_create_advanced()
157 ctx->threadLimit = numThreads; in POOL_create_advanced()
159 return ctx; in POOL_create_advanced()
165 static void POOL_join(POOL_ctx* ctx) { in POOL_join() argument
167 ZSTD_pthread_mutex_lock(&ctx->queueMutex); in POOL_join()
168 ctx->shutdown = 1; in POOL_join()
169 ZSTD_pthread_mutex_unlock(&ctx->queueMutex); in POOL_join()
171 ZSTD_pthread_cond_broadcast(&ctx->queuePushCond); in POOL_join()
172 ZSTD_pthread_cond_broadcast(&ctx->queuePopCond); in POOL_join()
175 for (i = 0; i < ctx->threadCapacity; ++i) { in POOL_join()
176 ZSTD_pthread_join(ctx->threads[i], NULL); /* note : could fail */ in POOL_join()
180 void POOL_free(POOL_ctx *ctx) { in POOL_free() argument
181 if (!ctx) { return; } in POOL_free()
182 POOL_join(ctx); in POOL_free()
183 ZSTD_pthread_mutex_destroy(&ctx->queueMutex); in POOL_free()
184 ZSTD_pthread_cond_destroy(&ctx->queuePushCond); in POOL_free()
185 ZSTD_pthread_cond_destroy(&ctx->queuePopCond); in POOL_free()
186 ZSTD_customFree(ctx->queue, ctx->customMem); in POOL_free()
187 ZSTD_customFree(ctx->threads, ctx->customMem); in POOL_free()
188 ZSTD_customFree(ctx, ctx->customMem); in POOL_free()
195 size_t POOL_sizeof(POOL_ctx *ctx) { in POOL_sizeof() argument
196 if (ctx==NULL) return 0; /* supports sizeof NULL */ in POOL_sizeof()
197 return sizeof(*ctx) in POOL_sizeof()
198 + ctx->queueSize * sizeof(POOL_job) in POOL_sizeof()
199 + ctx->threadCapacity * sizeof(ZSTD_pthread_t); in POOL_sizeof()
204 static int POOL_resize_internal(POOL_ctx* ctx, size_t numThreads) in POOL_resize_internal() argument
206 if (numThreads <= ctx->threadCapacity) { in POOL_resize_internal()
208 ctx->threadLimit = numThreads; in POOL_resize_internal()
212 …readPool = (ZSTD_pthread_t*)ZSTD_customMalloc(numThreads * sizeof(ZSTD_pthread_t), ctx->customMem); in POOL_resize_internal()
215 ZSTD_memcpy(threadPool, ctx->threads, ctx->threadCapacity * sizeof(*threadPool)); in POOL_resize_internal()
216 ZSTD_customFree(ctx->threads, ctx->customMem); in POOL_resize_internal()
217 ctx->threads = threadPool; in POOL_resize_internal()
220 for (threadId = ctx->threadCapacity; threadId < numThreads; ++threadId) { in POOL_resize_internal()
221 if (ZSTD_pthread_create(&threadPool[threadId], NULL, &POOL_thread, ctx)) { in POOL_resize_internal()
222 ctx->threadCapacity = threadId; in POOL_resize_internal()
227 ctx->threadCapacity = numThreads; in POOL_resize_internal()
228 ctx->threadLimit = numThreads; in POOL_resize_internal()
233 int POOL_resize(POOL_ctx* ctx, size_t numThreads) in POOL_resize() argument
236 if (ctx==NULL) return 1; in POOL_resize()
237 ZSTD_pthread_mutex_lock(&ctx->queueMutex); in POOL_resize()
238 result = POOL_resize_internal(ctx, numThreads); in POOL_resize()
239 ZSTD_pthread_cond_broadcast(&ctx->queuePopCond); in POOL_resize()
240 ZSTD_pthread_mutex_unlock(&ctx->queueMutex); in POOL_resize()
250 static int isQueueFull(POOL_ctx const* ctx) { in isQueueFull() argument
251 if (ctx->queueSize > 1) { in isQueueFull()
252 return ctx->queueHead == ((ctx->queueTail + 1) % ctx->queueSize); in isQueueFull()
254 return (ctx->numThreadsBusy == ctx->threadLimit) || in isQueueFull()
255 !ctx->queueEmpty; in isQueueFull()
260 static void POOL_add_internal(POOL_ctx* ctx, POOL_function function, void *opaque) in POOL_add_internal() argument
263 assert(ctx != NULL); in POOL_add_internal()
264 if (ctx->shutdown) return; in POOL_add_internal()
266 ctx->queueEmpty = 0; in POOL_add_internal()
267 ctx->queue[ctx->queueTail] = job; in POOL_add_internal()
268 ctx->queueTail = (ctx->queueTail + 1) % ctx->queueSize; in POOL_add_internal()
269 ZSTD_pthread_cond_signal(&ctx->queuePopCond); in POOL_add_internal()
272 void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque) in POOL_add() argument
274 assert(ctx != NULL); in POOL_add()
275 ZSTD_pthread_mutex_lock(&ctx->queueMutex); in POOL_add()
277 while (isQueueFull(ctx) && (!ctx->shutdown)) { in POOL_add()
278 ZSTD_pthread_cond_wait(&ctx->queuePushCond, &ctx->queueMutex); in POOL_add()
280 POOL_add_internal(ctx, function, opaque); in POOL_add()
281 ZSTD_pthread_mutex_unlock(&ctx->queueMutex); in POOL_add()
285 int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque) in POOL_tryAdd() argument
287 assert(ctx != NULL); in POOL_tryAdd()
288 ZSTD_pthread_mutex_lock(&ctx->queueMutex); in POOL_tryAdd()
289 if (isQueueFull(ctx)) { in POOL_tryAdd()
290 ZSTD_pthread_mutex_unlock(&ctx->queueMutex); in POOL_tryAdd()
293 POOL_add_internal(ctx, function, opaque); in POOL_tryAdd()
294 ZSTD_pthread_mutex_unlock(&ctx->queueMutex); in POOL_tryAdd()
323 void POOL_free(POOL_ctx* ctx) { in POOL_free() argument
324 assert(!ctx || ctx == &g_poolCtx); in POOL_free()
325 (void)ctx; in POOL_free()
328 int POOL_resize(POOL_ctx* ctx, size_t numThreads) { in POOL_resize() argument
329 (void)ctx; (void)numThreads; in POOL_resize()
333 void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque) { in POOL_add() argument
334 (void)ctx; in POOL_add()
338 int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque) { in POOL_tryAdd() argument
339 (void)ctx; in POOL_tryAdd()
344 size_t POOL_sizeof(POOL_ctx* ctx) { in POOL_sizeof() argument
345 if (ctx==NULL) return 0; /* supports sizeof NULL */ in POOL_sizeof()
346 assert(ctx == &g_poolCtx); in POOL_sizeof()
347 return sizeof(*ctx); in POOL_sizeof()