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()
114 POOL_ctx* ctx; in POOL_create_advanced() local
118 ctx = (POOL_ctx*)ZSTD_calloc(sizeof(POOL_ctx), customMem); in POOL_create_advanced()
119 if (!ctx) { return NULL; } in POOL_create_advanced()
124 ctx->queueSize = queueSize + 1; in POOL_create_advanced()
125 ctx->queue = (POOL_job*)ZSTD_malloc(ctx->queueSize * sizeof(POOL_job), customMem); in POOL_create_advanced()
126 ctx->queueHead = 0; in POOL_create_advanced()
127 ctx->queueTail = 0; in POOL_create_advanced()
128 ctx->numThreadsBusy = 0; in POOL_create_advanced()
129 ctx->queueEmpty = 1; in POOL_create_advanced()
132 error |= ZSTD_pthread_mutex_init(&ctx->queueMutex, NULL); in POOL_create_advanced()
133 error |= ZSTD_pthread_cond_init(&ctx->queuePushCond, NULL); in POOL_create_advanced()
134 error |= ZSTD_pthread_cond_init(&ctx->queuePopCond, NULL); in POOL_create_advanced()
135 if (error) { POOL_free(ctx); return NULL; } in POOL_create_advanced()
137 ctx->shutdown = 0; in POOL_create_advanced()
139 ctx->threads = (ZSTD_pthread_t*)ZSTD_malloc(numThreads * sizeof(ZSTD_pthread_t), customMem); in POOL_create_advanced()
140 ctx->threadCapacity = 0; in POOL_create_advanced()
141 ctx->customMem = customMem; in POOL_create_advanced()
143 if (!ctx->threads || !ctx->queue) { POOL_free(ctx); return NULL; } in POOL_create_advanced()
147 if (ZSTD_pthread_create(&ctx->threads[i], NULL, &POOL_thread, ctx)) { in POOL_create_advanced()
148 ctx->threadCapacity = i; in POOL_create_advanced()
149 POOL_free(ctx); in POOL_create_advanced()
152 ctx->threadCapacity = numThreads; in POOL_create_advanced()
153 ctx->threadLimit = numThreads; in POOL_create_advanced()
155 return ctx; in POOL_create_advanced()
161 static void POOL_join(POOL_ctx* ctx) { in POOL_join() argument
163 ZSTD_pthread_mutex_lock(&ctx->queueMutex); in POOL_join()
164 ctx->shutdown = 1; in POOL_join()
165 ZSTD_pthread_mutex_unlock(&ctx->queueMutex); in POOL_join()
167 ZSTD_pthread_cond_broadcast(&ctx->queuePushCond); in POOL_join()
168 ZSTD_pthread_cond_broadcast(&ctx->queuePopCond); in POOL_join()
171 for (i = 0; i < ctx->threadCapacity; ++i) { in POOL_join()
172 ZSTD_pthread_join(ctx->threads[i], NULL); /* note : could fail */ in POOL_join()
176 void POOL_free(POOL_ctx *ctx) { in POOL_free() argument
177 if (!ctx) { return; } in POOL_free()
178 POOL_join(ctx); in POOL_free()
179 ZSTD_pthread_mutex_destroy(&ctx->queueMutex); in POOL_free()
180 ZSTD_pthread_cond_destroy(&ctx->queuePushCond); in POOL_free()
181 ZSTD_pthread_cond_destroy(&ctx->queuePopCond); in POOL_free()
182 ZSTD_free(ctx->queue, ctx->customMem); in POOL_free()
183 ZSTD_free(ctx->threads, ctx->customMem); in POOL_free()
184 ZSTD_free(ctx, ctx->customMem); in POOL_free()
189 size_t POOL_sizeof(POOL_ctx *ctx) { in POOL_sizeof() argument
190 if (ctx==NULL) return 0; /* supports sizeof NULL */ in POOL_sizeof()
191 return sizeof(*ctx) in POOL_sizeof()
192 + ctx->queueSize * sizeof(POOL_job) in POOL_sizeof()
193 + ctx->threadCapacity * sizeof(ZSTD_pthread_t); in POOL_sizeof()
198 static int POOL_resize_internal(POOL_ctx* ctx, size_t numThreads) in POOL_resize_internal() argument
200 if (numThreads <= ctx->threadCapacity) { in POOL_resize_internal()
202 ctx->threadLimit = numThreads; in POOL_resize_internal()
206 …nst threadPool = (ZSTD_pthread_t*)ZSTD_malloc(numThreads * sizeof(ZSTD_pthread_t), ctx->customMem); in POOL_resize_internal()
209 memcpy(threadPool, ctx->threads, ctx->threadCapacity * sizeof(*threadPool)); in POOL_resize_internal()
210 ZSTD_free(ctx->threads, ctx->customMem); in POOL_resize_internal()
211 ctx->threads = threadPool; in POOL_resize_internal()
214 for (threadId = ctx->threadCapacity; threadId < numThreads; ++threadId) { in POOL_resize_internal()
215 if (ZSTD_pthread_create(&threadPool[threadId], NULL, &POOL_thread, ctx)) { in POOL_resize_internal()
216 ctx->threadCapacity = threadId; in POOL_resize_internal()
221 ctx->threadCapacity = numThreads; in POOL_resize_internal()
222 ctx->threadLimit = numThreads; in POOL_resize_internal()
227 int POOL_resize(POOL_ctx* ctx, size_t numThreads) in POOL_resize() argument
230 if (ctx==NULL) return 1; in POOL_resize()
231 ZSTD_pthread_mutex_lock(&ctx->queueMutex); in POOL_resize()
232 result = POOL_resize_internal(ctx, numThreads); in POOL_resize()
233 ZSTD_pthread_cond_broadcast(&ctx->queuePopCond); in POOL_resize()
234 ZSTD_pthread_mutex_unlock(&ctx->queueMutex); in POOL_resize()
244 static int isQueueFull(POOL_ctx const* ctx) { in isQueueFull() argument
245 if (ctx->queueSize > 1) { in isQueueFull()
246 return ctx->queueHead == ((ctx->queueTail + 1) % ctx->queueSize); in isQueueFull()
248 return (ctx->numThreadsBusy == ctx->threadLimit) || in isQueueFull()
249 !ctx->queueEmpty; in isQueueFull()
254 static void POOL_add_internal(POOL_ctx* ctx, POOL_function function, void *opaque) in POOL_add_internal() argument
257 assert(ctx != NULL); in POOL_add_internal()
258 if (ctx->shutdown) return; in POOL_add_internal()
260 ctx->queueEmpty = 0; in POOL_add_internal()
261 ctx->queue[ctx->queueTail] = job; in POOL_add_internal()
262 ctx->queueTail = (ctx->queueTail + 1) % ctx->queueSize; in POOL_add_internal()
263 ZSTD_pthread_cond_signal(&ctx->queuePopCond); in POOL_add_internal()
266 void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque) in POOL_add() argument
268 assert(ctx != NULL); in POOL_add()
269 ZSTD_pthread_mutex_lock(&ctx->queueMutex); in POOL_add()
271 while (isQueueFull(ctx) && (!ctx->shutdown)) { in POOL_add()
272 ZSTD_pthread_cond_wait(&ctx->queuePushCond, &ctx->queueMutex); in POOL_add()
274 POOL_add_internal(ctx, function, opaque); in POOL_add()
275 ZSTD_pthread_mutex_unlock(&ctx->queueMutex); in POOL_add()
279 int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque) in POOL_tryAdd() argument
281 assert(ctx != NULL); in POOL_tryAdd()
282 ZSTD_pthread_mutex_lock(&ctx->queueMutex); in POOL_tryAdd()
283 if (isQueueFull(ctx)) { in POOL_tryAdd()
284 ZSTD_pthread_mutex_unlock(&ctx->queueMutex); in POOL_tryAdd()
287 POOL_add_internal(ctx, function, opaque); in POOL_tryAdd()
288 ZSTD_pthread_mutex_unlock(&ctx->queueMutex); in POOL_tryAdd()
317 void POOL_free(POOL_ctx* ctx) { in POOL_free() argument
318 assert(!ctx || ctx == &g_ctx); in POOL_free()
319 (void)ctx; in POOL_free()
322 int POOL_resize(POOL_ctx* ctx, size_t numThreads) { in POOL_resize() argument
323 (void)ctx; (void)numThreads; in POOL_resize()
327 void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque) { in POOL_add() argument
328 (void)ctx; in POOL_add()
332 int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque) { in POOL_tryAdd() argument
333 (void)ctx; in POOL_tryAdd()
338 size_t POOL_sizeof(POOL_ctx* ctx) { in POOL_sizeof() argument
339 if (ctx==NULL) return 0; /* supports sizeof NULL */ in POOL_sizeof()
340 assert(ctx == &g_ctx); in POOL_sizeof()
341 return sizeof(*ctx); in POOL_sizeof()