Coverage Report

Created: 2026-05-30 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zstd/lib/common/pool.c
Line
Count
Source
1
/*
2
 * Copyright (c) Meta Platforms, Inc. and affiliates.
3
 * All rights reserved.
4
 *
5
 * This source code is licensed under both the BSD-style license (found in the
6
 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7
 * in the COPYING file in the root directory of this source tree).
8
 * You may select, at your option, one of the above-listed licenses.
9
 */
10
11
12
/* ======   Dependencies   ======= */
13
#include "../common/allocations.h"  /* ZSTD_customCalloc, ZSTD_customFree */
14
#include "zstd_deps.h" /* size_t */
15
#include "debug.h"     /* assert */
16
#include "pool.h"
17
18
/* ======   Compiler specifics   ====== */
19
#if defined(_MSC_VER)
20
#  pragma warning(disable : 4204)        /* disable: C4204: non-constant aggregate initializer */
21
#endif
22
23
24
#ifdef ZSTD_MULTITHREAD
25
26
#include "threading.h"   /* pthread adaptation */
27
28
/* A job is a function and an opaque argument */
29
typedef struct POOL_job_s {
30
    POOL_function function;
31
    void *opaque;
32
} POOL_job;
33
34
struct POOL_ctx_s {
35
    ZSTD_customMem customMem;
36
    /* Keep track of the threads */
37
    ZSTD_pthread_t* threads;
38
    size_t threadCapacity;
39
    size_t threadLimit;
40
41
    /* The queue is a circular buffer */
42
    POOL_job *queue;
43
    size_t queueHead;
44
    size_t queueTail;
45
    size_t queueSize;
46
47
    /* The number of threads working on jobs */
48
    size_t numThreadsBusy;
49
    /* Indicates if the queue is empty */
50
    int queueEmpty;
51
52
    /* The mutex protects the queue */
53
    ZSTD_pthread_mutex_t queueMutex;
54
    /* Condition variable for pushers to wait on when the queue is full */
55
    ZSTD_pthread_cond_t queuePushCond;
56
    /* Condition variables for poppers to wait on when the queue is empty */
57
    ZSTD_pthread_cond_t queuePopCond;
58
    /* Indicates if the queue is shutting down */
59
    int shutdown;
60
};
61
62
/* POOL_thread() :
63
 * Work thread for the thread pool.
64
 * Waits for jobs and executes them.
65
 * @returns : NULL on failure else non-null.
66
 */
67
42.4k
static void* POOL_thread(void* opaque) {
68
42.4k
    POOL_ctx* const ctx = (POOL_ctx*)opaque;
69
42.4k
    if (!ctx) { return NULL; }
70
2.80M
    for (;;) {
71
        /* Lock the mutex and wait for a non-empty queue or until shutdown */
72
2.80M
        ZSTD_pthread_mutex_lock(&ctx->queueMutex);
73
74
5.59M
        while ( ctx->queueEmpty
75
2.83M
            || (ctx->numThreadsBusy >= ctx->threadLimit) ) {
76
2.83M
            if (ctx->shutdown) {
77
                /* even if !queueEmpty, (possible if numThreadsBusy >= threadLimit),
78
                 * a few threads will be shutdown while !queueEmpty,
79
                 * but enough threads will remain active to finish the queue */
80
42.4k
                ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
81
42.4k
                return opaque;
82
42.4k
            }
83
2.79M
            ZSTD_pthread_cond_wait(&ctx->queuePopCond, &ctx->queueMutex);
84
2.79M
        }
85
        /* Pop a job off the queue */
86
2.76M
        {   POOL_job const job = ctx->queue[ctx->queueHead];
87
2.76M
            ctx->queueHead = (ctx->queueHead + 1) % ctx->queueSize;
88
2.76M
            ctx->numThreadsBusy++;
89
2.76M
            ctx->queueEmpty = (ctx->queueHead == ctx->queueTail);
90
            /* Unlock the mutex, signal a pusher, and run the job */
91
2.76M
            ZSTD_pthread_cond_signal(&ctx->queuePushCond);
92
2.76M
            ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
93
94
2.76M
            job.function(job.opaque);
95
96
            /* If the intended queue size was 0, signal after finishing job */
97
2.76M
            ZSTD_pthread_mutex_lock(&ctx->queueMutex);
98
2.76M
            ctx->numThreadsBusy--;
99
2.76M
            ZSTD_pthread_cond_signal(&ctx->queuePushCond);
100
2.76M
            ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
101
2.76M
        }
102
2.76M
    }  /* for (;;) */
103
42.4k
    assert(0);  /* Unreachable */
104
0
}
105
106
/* ZSTD_createThreadPool() : public access point */
107
0
POOL_ctx* ZSTD_createThreadPool(size_t numThreads) {
108
0
    return POOL_create (numThreads, 0);
109
0
}
110
111
0
POOL_ctx* POOL_create(size_t numThreads, size_t queueSize) {
112
0
    return POOL_create_advanced(numThreads, queueSize, ZSTD_defaultCMem);
113
0
}
114
115
POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize,
116
                               ZSTD_customMem customMem)
117
30.5k
{
118
30.5k
    POOL_ctx* ctx;
119
    /* Check parameters */
120
30.5k
    if (!numThreads) { return NULL; }
121
    /* Allocate the context and zero initialize */
122
30.5k
    ctx = (POOL_ctx*)ZSTD_customCalloc(sizeof(POOL_ctx), customMem);
123
30.5k
    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
30.5k
    ctx->queueSize = queueSize + 1;
129
30.5k
    ctx->queue = (POOL_job*)ZSTD_customCalloc(ctx->queueSize * sizeof(POOL_job), customMem);
130
30.5k
    ctx->queueHead = 0;
131
30.5k
    ctx->queueTail = 0;
132
30.5k
    ctx->numThreadsBusy = 0;
133
30.5k
    ctx->queueEmpty = 1;
134
30.5k
    {
135
30.5k
        int error = 0;
136
30.5k
        error |= ZSTD_pthread_mutex_init(&ctx->queueMutex, NULL);
137
30.5k
        error |= ZSTD_pthread_cond_init(&ctx->queuePushCond, NULL);
138
30.5k
        error |= ZSTD_pthread_cond_init(&ctx->queuePopCond, NULL);
139
30.5k
        if (error) { POOL_free(ctx); return NULL; }
140
30.5k
    }
141
30.5k
    ctx->shutdown = 0;
142
    /* Allocate space for the thread handles */
143
30.5k
    ctx->threads = (ZSTD_pthread_t*)ZSTD_customCalloc(numThreads * sizeof(ZSTD_pthread_t), customMem);
144
30.5k
    ctx->threadCapacity = 0;
145
30.5k
    ctx->threadLimit = numThreads;
146
30.5k
    ctx->customMem = customMem;
147
    /* Check for errors */
148
30.5k
    if (!ctx->threads || !ctx->queue) { POOL_free(ctx); return NULL; }
149
    /* Initialize the threads */
150
72.6k
    while (ctx->threadCapacity < numThreads) {
151
42.1k
        if (ZSTD_pthread_create(&ctx->threads[ctx->threadCapacity++], NULL, &POOL_thread, ctx)) {
152
0
            --ctx->threadCapacity;
153
0
            POOL_free(ctx);
154
0
            return NULL;
155
0
        }
156
42.1k
    }
157
30.5k
    return ctx;
158
30.5k
}
159
160
/*! POOL_join() :
161
    Shutdown the queue, wake any sleeping threads, and join all of the threads.
162
*/
163
30.5k
static void POOL_join(POOL_ctx* ctx) {
164
    /* Shut down the queue */
165
30.5k
    ZSTD_pthread_mutex_lock(&ctx->queueMutex);
166
30.5k
    ctx->shutdown = 1;
167
30.5k
    ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
168
    /* Wake up sleeping threads */
169
30.5k
    ZSTD_pthread_cond_broadcast(&ctx->queuePushCond);
170
30.5k
    ZSTD_pthread_cond_broadcast(&ctx->queuePopCond);
171
    /* Join all of the threads */
172
30.5k
    {   size_t i;
173
72.9k
        for (i = 0; i < ctx->threadCapacity; ++i) {
174
42.4k
            ZSTD_pthread_join(ctx->threads[i]);  /* note : could fail */
175
42.4k
    }   }
176
30.5k
}
177
178
30.5k
void POOL_free(POOL_ctx *ctx) {
179
30.5k
    if (!ctx) { return; }
180
30.5k
    POOL_join(ctx);
181
30.5k
    ZSTD_pthread_mutex_destroy(&ctx->queueMutex);
182
30.5k
    ZSTD_pthread_cond_destroy(&ctx->queuePushCond);
183
30.5k
    ZSTD_pthread_cond_destroy(&ctx->queuePopCond);
184
30.5k
    ZSTD_customFree(ctx->queue, ctx->customMem);
185
30.5k
    ZSTD_customFree(ctx->threads, ctx->customMem);
186
30.5k
    ZSTD_customFree(ctx, ctx->customMem);
187
30.5k
}
188
189
/*! POOL_joinJobs() :
190
 *  Waits for all queued jobs to finish executing.
191
 */
192
0
void POOL_joinJobs(POOL_ctx* ctx) {
193
0
    ZSTD_pthread_mutex_lock(&ctx->queueMutex);
194
0
    while(!ctx->queueEmpty || ctx->numThreadsBusy > 0) {
195
0
        ZSTD_pthread_cond_wait(&ctx->queuePushCond, &ctx->queueMutex);
196
0
    }
197
0
    ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
198
0
}
199
200
0
void ZSTD_freeThreadPool (ZSTD_threadPool* pool) {
201
0
  POOL_free (pool);
202
0
}
203
204
0
size_t POOL_sizeof(const POOL_ctx* ctx) {
205
0
    if (ctx==NULL) return 0;  /* supports sizeof NULL */
206
0
    return sizeof(*ctx)
207
0
        + ctx->queueSize * sizeof(POOL_job)
208
0
        + ctx->threadCapacity * sizeof(ZSTD_pthread_t);
209
0
}
210
211
212
/* @return : 0 on success, 1 on error */
213
static int POOL_resize_internal(POOL_ctx* ctx, size_t numThreads)
214
563
{
215
563
    if (numThreads <= ctx->threadCapacity) {
216
285
        if (!numThreads) return 1;
217
285
        ctx->threadLimit = numThreads;
218
285
        return 0;
219
285
    }
220
    /* numThreads > threadCapacity */
221
278
    ctx->threadLimit = numThreads;
222
278
    {   ZSTD_pthread_t* const threadPool = (ZSTD_pthread_t*)ZSTD_customCalloc(numThreads * sizeof(ZSTD_pthread_t), ctx->customMem);
223
278
        if (!threadPool) return 1;
224
        /* extend existing thread pool */
225
278
        ZSTD_memcpy(threadPool, ctx->threads, ctx->threadCapacity * sizeof(ZSTD_pthread_t));
226
278
        ZSTD_customFree(ctx->threads, ctx->customMem);
227
278
        ctx->threads = threadPool;
228
        /* Initialize additional threads */
229
556
        while (ctx->threadCapacity < numThreads) {
230
278
            if (ZSTD_pthread_create(&threadPool[ctx->threadCapacity++], NULL, &POOL_thread, ctx)) {
231
0
                --ctx->threadCapacity;
232
0
                return 1;
233
0
            }
234
278
        }
235
278
    }
236
    /* successfully expanded */
237
278
    return 0;
238
278
}
239
240
/* @return : 0 on success, 1 on error */
241
int POOL_resize(POOL_ctx* ctx, size_t numThreads)
242
563
{
243
563
    int result;
244
563
    if (ctx==NULL) return 1;
245
563
    ZSTD_pthread_mutex_lock(&ctx->queueMutex);
246
563
    result = POOL_resize_internal(ctx, numThreads);
247
563
    ZSTD_pthread_cond_broadcast(&ctx->queuePopCond);
248
563
    ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
249
563
    return result;
250
563
}
251
252
/**
253
 * Returns 1 if the queue is full and 0 otherwise.
254
 *
255
 * When queueSize is 1 (pool was created with an intended queueSize of 0),
256
 * then a queue is empty if there is a thread free _and_ no job is waiting.
257
 */
258
4.19M
static int isQueueFull(POOL_ctx const* ctx) {
259
4.19M
    if (ctx->queueSize > 1) {
260
0
        return ctx->queueHead == ((ctx->queueTail + 1) % ctx->queueSize);
261
4.19M
    } else {
262
4.19M
        return (ctx->numThreadsBusy == ctx->threadLimit) ||
263
2.77M
               !ctx->queueEmpty;
264
4.19M
    }
265
4.19M
}
266
267
268
static void
269
POOL_add_internal(POOL_ctx* ctx, POOL_function function, void *opaque)
270
2.76M
{
271
2.76M
    POOL_job job;
272
2.76M
    job.function = function;
273
2.76M
    job.opaque = opaque;
274
2.76M
    assert(ctx != NULL);
275
2.76M
    if (ctx->shutdown) return;
276
277
2.76M
    ctx->queueEmpty = 0;
278
2.76M
    ctx->queue[ctx->queueTail] = job;
279
2.76M
    ctx->queueTail = (ctx->queueTail + 1) % ctx->queueSize;
280
2.76M
    ZSTD_pthread_cond_signal(&ctx->queuePopCond);
281
2.76M
}
282
283
void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque)
284
0
{
285
0
    assert(ctx != NULL);
286
0
    ZSTD_pthread_mutex_lock(&ctx->queueMutex);
287
    /* Wait until there is space in the queue for the new job */
288
0
    while (isQueueFull(ctx) && (!ctx->shutdown)) {
289
0
        ZSTD_pthread_cond_wait(&ctx->queuePushCond, &ctx->queueMutex);
290
0
    }
291
0
    POOL_add_internal(ctx, function, opaque);
292
0
    ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
293
0
}
294
295
296
int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque)
297
4.19M
{
298
4.19M
    assert(ctx != NULL);
299
4.19M
    ZSTD_pthread_mutex_lock(&ctx->queueMutex);
300
4.19M
    if (isQueueFull(ctx)) {
301
1.43M
        ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
302
1.43M
        return 0;
303
1.43M
    }
304
2.76M
    POOL_add_internal(ctx, function, opaque);
305
2.76M
    ZSTD_pthread_mutex_unlock(&ctx->queueMutex);
306
2.76M
    return 1;
307
4.19M
}
308
309
310
#else  /* ZSTD_MULTITHREAD  not defined */
311
312
/* ========================== */
313
/* No multi-threading support */
314
/* ========================== */
315
316
317
/* We don't need any data, but if it is empty, malloc() might return NULL. */
318
struct POOL_ctx_s {
319
    int dummy;
320
};
321
static POOL_ctx g_poolCtx;
322
323
POOL_ctx* POOL_create(size_t numThreads, size_t queueSize) {
324
    return POOL_create_advanced(numThreads, queueSize, ZSTD_defaultCMem);
325
}
326
327
POOL_ctx*
328
POOL_create_advanced(size_t numThreads, size_t queueSize, ZSTD_customMem customMem)
329
{
330
    (void)numThreads;
331
    (void)queueSize;
332
    (void)customMem;
333
    return &g_poolCtx;
334
}
335
336
void POOL_free(POOL_ctx* ctx) {
337
    assert(!ctx || ctx == &g_poolCtx);
338
    (void)ctx;
339
}
340
341
void POOL_joinJobs(POOL_ctx* ctx){
342
    assert(!ctx || ctx == &g_poolCtx);
343
    (void)ctx;
344
}
345
346
int POOL_resize(POOL_ctx* ctx, size_t numThreads) {
347
    (void)ctx; (void)numThreads;
348
    return 0;
349
}
350
351
void POOL_add(POOL_ctx* ctx, POOL_function function, void* opaque) {
352
    (void)ctx;
353
    function(opaque);
354
}
355
356
int POOL_tryAdd(POOL_ctx* ctx, POOL_function function, void* opaque) {
357
    (void)ctx;
358
    function(opaque);
359
    return 1;
360
}
361
362
size_t POOL_sizeof(const POOL_ctx* ctx) {
363
    if (ctx==NULL) return 0;  /* supports sizeof NULL */
364
    assert(ctx == &g_poolCtx);
365
    return sizeof(*ctx);
366
}
367
368
#endif  /* ZSTD_MULTITHREAD */