• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

saitoha / libsixel / 25090517000

28 Apr 2026 04:44PM UTC coverage: 85.521% (-0.02%) from 85.545%
25090517000

push

github

saitoha
ci: use clang cl-driver for cygwin msvc abi autotools

122356 of 257257 branches covered (47.56%)

147399 of 172355 relevant lines covered (85.52%)

7608075.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.16
/src/threadpool.c
1
/*
2
 * SPDX-License-Identifier: MIT
3
 *
4
 * Copyright (c) 2025 libsixel developers. See `AUTHORS`.
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7
 * this software and associated documentation files (the "Software"), to deal in
8
 * the Software without restriction, including without limitation the rights to
9
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10
 * of the Software, and to permit persons to whom the Software is furnished to do
11
 * so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in all
14
 * copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 */
24

25
#if defined(HAVE_CONFIG_H)
26
#include "config.h"
27
#endif
28

29
#if SIXEL_ENABLE_THREADS
30

31
#include <errno.h>
32
#include <stdlib.h>
33
#include <string.h>
34

35
#include "threadpool.h"
36
#include "threading.h"
37

38
typedef struct threadpool_worker threadpool_worker_t;
39

40
struct threadpool_worker {
41
    threadpool_t *pool;
42
    sixel_thread_t thread;
43
    void *workspace;
44
    int started;
45
    int index;
46
    int pinned;
47
};
48

49
struct threadpool {
50
    int nthreads;
51
    int qsize;
52
    size_t workspace_size;
53
    tp_workspace_cleanup_fn workspace_cleanup;
54
    tp_worker_fn worker;
55
    void *userdata;
56
    tp_job_t *jobs;
57
    int head;
58
    int tail;
59
    int count;
60
    int running;
61
    int shutting_down;
62
    int joined;
63
    int error;
64
    int threads_started;
65
    int worker_capacity;
66
    int pin_threads;
67
    int hw_threads;
68
    sixel_mutex_t mutex;
69
    sixel_cond_t cond_not_empty;
70
    sixel_cond_t cond_not_full;
71
    sixel_cond_t cond_drained;
72
    int mutex_ready;
73
    int cond_not_empty_ready;
74
    int cond_not_full_ready;
75
    int cond_drained_ready;
76
    threadpool_worker_t **workers; /* owned worker slots (stable addresses) */
77
};
78

79
static void threadpool_free(threadpool_t *pool);
80
static int threadpool_worker_main(void *arg);
81
static int threadpool_spawn_worker(threadpool_t *pool,
82
                                   threadpool_worker_t *worker);
83

84
/*
85
 * Release every dynamically allocated component of the pool. Callers must
86
 * ensure that worker threads have already terminated before invoking this
87
 * helper; otherwise joining would operate on freed memory.
88
 */
89
static void
90
threadpool_free(threadpool_t *pool)
55,381✔
91
{
92
    int i;
25,908✔
93

94
    if (pool == NULL) {
55,381✔
95
        return;
96
    }
97
    if (pool->workers != NULL) {
55,381!
98
        for (i = 0; i < pool->worker_capacity; ++i) {
202,067!
99
            threadpool_worker_t *worker;
68,826✔
100
            worker = pool->workers[i];
146,686✔
101
            if (worker == NULL) {
146,686!
102
                continue;
×
103
            }
104
            if (worker->workspace != NULL) {
146,686✔
105
                if (pool->workspace_cleanup != NULL) {
125,056✔
106
                    pool->workspace_cleanup(worker->workspace);
21,064✔
107
                }
2,402✔
108
                free(worker->workspace);
125,056✔
109
            }
50,924✔
110
            free(worker);
146,686✔
111
        }
54,034!
112
        free(pool->workers);
55,381✔
113
    }
19,923✔
114
    if (pool->jobs != NULL) {
55,381!
115
        free(pool->jobs);
55,381✔
116
    }
19,923✔
117
    if (pool->cond_drained_ready) {
55,381!
118
        sixel_cond_destroy(&pool->cond_drained);
55,381✔
119
    }
19,923✔
120
    if (pool->cond_not_full_ready) {
55,381!
121
        sixel_cond_destroy(&pool->cond_not_full);
55,381✔
122
    }
19,923✔
123
    if (pool->cond_not_empty_ready) {
55,381!
124
        sixel_cond_destroy(&pool->cond_not_empty);
55,381✔
125
    }
19,923✔
126
    if (pool->mutex_ready) {
55,381!
127
        sixel_mutex_destroy(&pool->mutex);
55,381✔
128
    }
19,923✔
129
    free(pool);
55,381✔
130
}
19,923✔
131

132
/*
133
 * Worker threads pull jobs from the ring buffer, execute the supplied callback
134
 * outside the critical section, and record the first failure code. All
135
 * synchronization is delegated to the mutex/condition helpers provided by the
136
 * threading abstraction.
137
 */
138
static int
139
threadpool_worker_main(void *arg)
146,686✔
140
{
141
    threadpool_worker_t *worker;
68,826✔
142
    threadpool_t *pool;
68,826✔
143
    tp_job_t job;
68,826✔
144
    int rc;
68,826✔
145

146
    worker = (threadpool_worker_t *)arg;
146,686✔
147
    pool = worker->pool;
146,686✔
148
    for (;;) {
461,914✔
149
        sixel_mutex_lock(&pool->mutex);
533,829✔
150
        while (pool->count == 0 && !pool->shutting_down) {
730,835!
151
            sixel_cond_wait(&pool->cond_not_empty, &pool->mutex);
197,007✔
152
        }
153
        if (pool->count == 0 && pool->shutting_down) {
533,836!
154
            sixel_mutex_unlock(&pool->mutex);
146,686✔
155
            break;
146,684✔
156
        }
157
        job = pool->jobs[pool->head];
384,138✔
158
        pool->head = (pool->head + 1) % pool->qsize;
384,138✔
159
        pool->count -= 1;
384,138✔
160
        pool->running += 1;
384,138✔
161
        sixel_cond_signal(&pool->cond_not_full);
384,138✔
162
        sixel_mutex_unlock(&pool->mutex);
384,141✔
163

164
        if (pool->pin_threads && !worker->pinned && pool->hw_threads > 0) {
384,142!
165
            int cpu_index;
44,419✔
166

167
            cpu_index = worker->index % pool->hw_threads;
98,215✔
168
            (void)sixel_thread_pin_self(cpu_index);
98,215✔
169
            worker->pinned = 1;
98,214✔
170
        }
30,618✔
171

172
        rc = pool->worker(job, pool->userdata, worker->workspace);
377,192✔
173

174
        sixel_mutex_lock(&pool->mutex);
377,094✔
175
        pool->running -= 1;
377,193✔
176
        if (rc != SIXEL_OK && pool->error == SIXEL_OK) {
377,193!
177
            pool->error = rc;
×
178
        }
179
        if (pool->count == 0 && pool->running == 0) {
378,132!
180
            sixel_cond_broadcast(&pool->cond_drained);
119,932✔
181
        }
49,994✔
182
        sixel_mutex_unlock(&pool->mutex);
387,154✔
183
    }
184
    return SIXEL_OK;
146,684✔
185
}
26,505✔
186

187
SIXELAPI threadpool_t *
188
threadpool_create(int nthreads,
55,381✔
189
                  int qsize,
190
                  size_t workspace_size,
191
                  tp_worker_fn worker,
192
                  void *userdata,
193
                  tp_workspace_cleanup_fn workspace_cleanup)
194
{
195
    threadpool_t *pool;
25,908✔
196
    int i;
25,908✔
197
    int rc;
25,908✔
198

199
    if (nthreads <= 0 || qsize <= 0 || worker == NULL) {
55,381!
200
        return NULL;
201
    }
202
    pool = (threadpool_t *)calloc(1, sizeof(threadpool_t));
55,381✔
203
    if (pool == NULL) {
55,381!
204
        return NULL;
205
    }
206
    pool->nthreads = nthreads;
55,381✔
207
    pool->qsize = qsize;
55,381✔
208
    pool->workspace_size = workspace_size;
55,381✔
209
    pool->worker = worker;
55,381✔
210
    pool->userdata = userdata;
55,381✔
211
    pool->jobs = NULL;
55,381✔
212
    pool->head = 0;
55,381✔
213
    pool->tail = 0;
55,381✔
214
    pool->count = 0;
55,381✔
215
    pool->running = 0;
55,381✔
216
    pool->shutting_down = 0;
55,381✔
217
    pool->joined = 0;
55,381✔
218
    pool->error = SIXEL_OK;
55,381✔
219
    pool->threads_started = 0;
55,381✔
220
    pool->mutex_ready = 0;
55,381✔
221
    pool->cond_not_empty_ready = 0;
55,381✔
222
    pool->cond_not_full_ready = 0;
55,381✔
223
    pool->cond_drained_ready = 0;
55,381✔
224
    pool->pin_threads = 0;
55,381✔
225
    pool->hw_threads = 0;
55,381✔
226
    pool->workers = NULL;
55,381✔
227
    pool->workspace_cleanup = workspace_cleanup;
55,381✔
228

229
    rc = sixel_mutex_init(&pool->mutex);
55,381✔
230
    if (rc != SIXEL_OK) {
55,381!
231
        errno = EINVAL;
×
232
        threadpool_free(pool);
×
233
        return NULL;
×
234
    }
235
    pool->mutex_ready = 1;
55,381✔
236

237
    rc = sixel_cond_init(&pool->cond_not_empty);
55,381✔
238
    if (rc != SIXEL_OK) {
55,381!
239
        errno = EINVAL;
×
240
        threadpool_free(pool);
×
241
        return NULL;
×
242
    }
243
    pool->cond_not_empty_ready = 1;
55,381✔
244

245
    rc = sixel_cond_init(&pool->cond_not_full);
55,381✔
246
    if (rc != SIXEL_OK) {
55,381!
247
        errno = EINVAL;
×
248
        threadpool_free(pool);
×
249
        return NULL;
×
250
    }
251
    pool->cond_not_full_ready = 1;
55,381✔
252

253
    rc = sixel_cond_init(&pool->cond_drained);
55,381✔
254
    if (rc != SIXEL_OK) {
55,381!
255
        errno = EINVAL;
×
256
        threadpool_free(pool);
×
257
        return NULL;
×
258
    }
259
    pool->cond_drained_ready = 1;
55,381✔
260

261
    pool->jobs = (tp_job_t *)malloc(sizeof(tp_job_t) * (size_t)qsize);
55,381✔
262
    if (pool->jobs == NULL) {
55,381!
263
        threadpool_free(pool);
×
264
        return NULL;
×
265
    }
266

267
    pool->worker_capacity = nthreads;
55,381✔
268
    pool->workers = (threadpool_worker_t **)calloc((size_t)nthreads,
55,381✔
269
            sizeof(threadpool_worker_t *));
270
    if (pool->workers == NULL) {
55,381!
271
        threadpool_free(pool);
×
272
        return NULL;
×
273
    }
274

275
    for (i = 0; i < nthreads; ++i) {
173,753!
276
        pool->workers[i] = (threadpool_worker_t *)
118,374✔
277
            calloc(1, sizeof(threadpool_worker_t));
118,374✔
278
        if (pool->workers[i] == NULL) {
118,374!
279
            pool->shutting_down = 1;
×
280
            sixel_cond_broadcast(&pool->cond_not_empty);
×
281
            break;
×
282
        }
283
        pool->workers[i]->pool = pool;
118,374✔
284
        pool->workers[i]->workspace = NULL;
118,374✔
285
        pool->workers[i]->started = 0;
118,374✔
286
        pool->workers[i]->index = i;
118,374✔
287
        pool->workers[i]->pinned = 0;
118,374✔
288
        if (workspace_size > 0) {
118,374✔
289
            /*
290
             * Zero-initialize the per-thread workspace so that structures like
291
             * `sixel_parallel_worker_state_t` start with predictable values.
292
             * The worker initialization logic assumes fields such as
293
             * `initialized` are cleared before the first job.
294
             */
295
            pool->workers[i]->workspace = calloc(1, workspace_size);
96,744✔
296
            if (pool->workers[i]->workspace == NULL) {
96,744!
297
                pool->shutting_down = 1;
×
298
                sixel_cond_broadcast(&pool->cond_not_empty);
×
299
                break;
×
300
            }
301
        }
39,143✔
302
        rc = threadpool_spawn_worker(pool, pool->workers[i]);
118,374✔
303
        if (rc != SIXEL_OK) {
118,372!
304
            break;
305
        }
306
    }
42,253✔
307

308
    if (pool->threads_started != nthreads) {
55,379!
309
        int started;
310

311
        started = pool->threads_started;
312
        for (i = 0; i < started; ++i) {
×
313
            sixel_cond_broadcast(&pool->cond_not_empty);
×
314
            sixel_thread_join(&pool->workers[i]->thread);
×
315
        }
316
        threadpool_free(pool);
×
317
        return NULL;
×
318
    }
319

320
    return pool;
39,193✔
321
}
19,923✔
322

323
SIXELAPI void
324
threadpool_set_affinity(threadpool_t *pool, int pin_threads)
54,831✔
325
{
326
    if (pool == NULL) {
54,831✔
327
        return;
328
    }
329

330
    sixel_mutex_lock(&pool->mutex);
54,831✔
331
    pool->pin_threads = (pin_threads != 0) ? 1 : 0;
54,831!
332
    if (pool->pin_threads != 0) {
54,831✔
333
        pool->hw_threads = sixel_get_hw_threads();
53,505✔
334
        if (pool->hw_threads < 1) {
53,505!
335
            pool->pin_threads = 0;
×
336
        }
337
    } else {
19,356✔
338
        pool->hw_threads = 0;
1,326✔
339
    }
340
    sixel_mutex_unlock(&pool->mutex);
54,831✔
341
}
19,659✔
342

343
static int
344
threadpool_spawn_worker(threadpool_t *pool, threadpool_worker_t *worker)
146,686✔
345
{
346
    int rc;
68,826✔
347

348
    if (pool == NULL || worker == NULL) {
146,686!
349
        return SIXEL_BAD_ARGUMENT;
350
    }
351
    rc = sixel_thread_create(&worker->thread,
200,720✔
352
                             threadpool_worker_main,
353
                             worker);
54,034✔
354
    if (rc != SIXEL_OK) {
146,686!
355
        sixel_mutex_lock(&pool->mutex);
2✔
356
        pool->shutting_down = 1;
×
357
        sixel_cond_broadcast(&pool->cond_not_empty);
×
358
        sixel_mutex_unlock(&pool->mutex);
×
359
        return rc;
×
360
    }
361
    worker->started = 1;
146,684✔
362
    pool->threads_started += 1;
146,684✔
363
    return SIXEL_OK;
146,684✔
364
}
54,034✔
365

366
SIXELAPI void
367
threadpool_destroy(threadpool_t *pool)
55,381✔
368
{
369
    if (pool == NULL) {
55,381✔
370
        return;
371
    }
372
    threadpool_finish(pool);
55,381✔
373
    threadpool_free(pool);
55,381✔
374
}
19,923✔
375

376
SIXELAPI void
377
threadpool_push(threadpool_t *pool, tp_job_t job)
387,145✔
378
{
379
    if (pool == NULL) {
387,145✔
380
        return;
381
    }
382
    sixel_mutex_lock(&pool->mutex);
387,145✔
383
    if (pool->shutting_down) {
387,149!
384
        sixel_mutex_unlock(&pool->mutex);
×
385
        return;
×
386
    }
387
    while (pool->count == pool->qsize && !pool->shutting_down) {
421,011!
388
        sixel_cond_wait(&pool->cond_not_full, &pool->mutex);
33,864✔
389
    }
390
    if (pool->shutting_down) {
387,147!
391
        sixel_mutex_unlock(&pool->mutex);
×
392
        return;
×
393
    }
394
    pool->jobs[pool->tail] = job;
387,147✔
395
    pool->tail = (pool->tail + 1) % pool->qsize;
387,147✔
396
    pool->count += 1;
387,147✔
397
    sixel_cond_signal(&pool->cond_not_empty);
387,147✔
398
    sixel_mutex_unlock(&pool->mutex);
387,148✔
399
}
140,745✔
400

401
SIXELAPI void
402
threadpool_finish(threadpool_t *pool)
110,762✔
403
{
404
    int i;
51,816✔
405

406
    if (pool == NULL) {
110,762✔
407
        return;
408
    }
409
    sixel_mutex_lock(&pool->mutex);
110,762✔
410
    if (pool->joined) {
110,762✔
411
        sixel_mutex_unlock(&pool->mutex);
55,381✔
412
        return;
55,381✔
413
    }
414
    pool->shutting_down = 1;
55,381✔
415
    sixel_cond_broadcast(&pool->cond_not_empty);
55,381✔
416
    sixel_cond_broadcast(&pool->cond_not_full);
55,381✔
417
    while (pool->count > 0 || pool->running > 0) {
97,182!
418
        sixel_cond_wait(&pool->cond_drained, &pool->mutex);
41,801✔
419
    }
420
    sixel_mutex_unlock(&pool->mutex);
55,381✔
421

422
    for (i = 0; i < pool->threads_started; ++i) {
218,252!
423
        if (pool->workers[i] != NULL && pool->workers[i]->started) {
146,685!
424
            sixel_thread_join(&pool->workers[i]->thread);
146,686✔
425
            pool->workers[i]->started = 0;
146,686✔
426
        }
54,034✔
427
    }
54,034✔
428

429
    sixel_mutex_lock(&pool->mutex);
55,381✔
430
    pool->joined = 1;
55,381✔
431
    sixel_mutex_unlock(&pool->mutex);
55,381✔
432
}
39,846!
433

434
SIXELAPI int
435
threadpool_grow(threadpool_t *pool, int additional_threads)
22,967✔
436
{
437
    threadpool_worker_t **expanded;
11,238✔
438
    int new_target;
11,238✔
439
    int started_new;
11,238✔
440
    int i;
11,238✔
441
    int rc;
11,238✔
442

443
    if (pool == NULL || additional_threads <= 0) {
22,967!
444
        return SIXEL_OK;
445
    }
446

447
    sixel_mutex_lock(&pool->mutex);
22,967✔
448
    if (pool->shutting_down) {
22,967!
449
        sixel_mutex_unlock(&pool->mutex);
×
450
        return SIXEL_RUNTIME_ERROR;
×
451
    }
452
    new_target = pool->nthreads + additional_threads;
22,967✔
453
    /*
454
     * Worker structs stay heap-allocated per slot so pointer-table growth
455
     * never invalidates addresses already held by running threads.
456
     */
457
    if (new_target > pool->worker_capacity) {
22,967!
458
        expanded = (threadpool_worker_t **)realloc(
22,967✔
459
            pool->workers,
22,967✔
460
            (size_t)new_target * sizeof(threadpool_worker_t *));
22,967✔
461
        if (expanded == NULL) {
22,967!
462
            sixel_mutex_unlock(&pool->mutex);
×
463
            return SIXEL_BAD_ALLOCATION;
×
464
        }
465
        memset(expanded + pool->worker_capacity,
24,052✔
466
               0,
467
               (size_t)(new_target - pool->worker_capacity)
12,846✔
468
                   * sizeof(threadpool_worker_t *));
8,635✔
469
        pool->workers = expanded;
22,967✔
470
        pool->worker_capacity = new_target;
22,967✔
471
    }
11,206✔
472
    sixel_mutex_unlock(&pool->mutex);
22,967✔
473

474
    started_new = 0;
22,967✔
475
    rc = SIXEL_OK;
22,967✔
476
    for (i = pool->nthreads; i < new_target; ++i) {
51,279!
477
        pool->workers[i] = (threadpool_worker_t *)
28,312✔
478
            calloc(1, sizeof(threadpool_worker_t));
28,312✔
479
        if (pool->workers[i] == NULL) {
28,312!
480
            rc = SIXEL_BAD_ALLOCATION;
481
            break;
482
        }
483
        pool->workers[i]->pool = pool;
28,312✔
484
        pool->workers[i]->workspace = NULL;
28,312✔
485
        pool->workers[i]->started = 0;
28,312✔
486
        pool->workers[i]->index = i;
28,312✔
487
        pool->workers[i]->pinned = 0;
28,312✔
488
        if (pool->workspace_size > 0) {
28,312!
489
            pool->workers[i]->workspace =
28,312✔
490
                calloc(1, pool->workspace_size);
28,312✔
491
            if (pool->workers[i]->workspace == NULL) {
28,312!
492
                rc = SIXEL_BAD_ALLOCATION;
493
                break;
494
            }
495
        }
11,781✔
496

497
        rc = threadpool_spawn_worker(pool, pool->workers[i]);
28,312✔
498
        if (rc != SIXEL_OK) {
28,312!
499
            break;
500
        }
501
        started_new += 1;
28,312✔
502
    }
11,781✔
503

504
    if (rc != SIXEL_OK) {
21,396!
505
        int j;
506

507
        for (j = i; j < new_target; ++j) {
×
508
            if (pool->workers[j] != NULL) {
×
509
                if (pool->workers[j]->workspace != NULL) {
×
510
                    free(pool->workers[j]->workspace);
×
511
                }
512
                free(pool->workers[j]);
×
513
                pool->workers[j] = NULL;
×
514
            }
515
        }
516
    }
517

518
    sixel_mutex_lock(&pool->mutex);
22,967✔
519
    pool->nthreads = pool->nthreads + started_new;
22,967✔
520
    sixel_mutex_unlock(&pool->mutex);
22,967✔
521

522
    return rc;
22,967✔
523
}
11,206✔
524

525
SIXELAPI int
526
threadpool_get_error(threadpool_t *pool)
55,381✔
527
{
528
    int error;
25,908✔
529

530
    if (pool == NULL) {
55,381✔
531
        return SIXEL_BAD_ARGUMENT;
532
    }
533
    sixel_mutex_lock(&pool->mutex);
55,381✔
534
    error = pool->error;
55,381✔
535
    sixel_mutex_unlock(&pool->mutex);
55,381✔
536
    return error;
55,381✔
537
}
19,923✔
538

539
#endif  /* SIXEL_ENABLE_THREADS */
540

541
/* emacs Local Variables:      */
542
/* emacs mode: c               */
543
/* emacs tab-width: 4          */
544
/* emacs indent-tabs-mode: nil */
545
/* emacs c-basic-offset: 4     */
546
/* emacs End:                  */
547
/* vim: set expandtab ts=4 sts=4 sw=4 : */
548
/* EOF */
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc