Coverage Report

Created: 2026-06-13 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mpv/misc/thread_pool.c
Line
Count
Source
1
/* Copyright (C) 2018 the mpv developers
2
 *
3
 * Permission to use, copy, modify, and/or distribute this software for any
4
 * purpose with or without fee is hereby granted, provided that the above
5
 * copyright notice and this permission notice appear in all copies.
6
 *
7
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
 */
15
16
#include "common/common.h"
17
#include "osdep/threads.h"
18
#include "osdep/timer.h"
19
20
#include "thread_pool.h"
21
22
// Threads destroy themselves after this many seconds, if there's no new work
23
// and the thread count is above the configured minimum.
24
#define DESTROY_TIMEOUT 10
25
26
struct work {
27
    void (*fn)(void *ctx);
28
    void *fn_ctx;
29
};
30
31
struct mp_thread_pool {
32
    int min_threads, max_threads;
33
34
    mp_mutex lock;
35
    mp_cond wakeup;
36
37
    // --- the following fields are protected by lock
38
39
    mp_thread *threads;
40
    int num_threads;
41
42
    // Number of threads which have taken up work and are still processing it.
43
    int busy_threads;
44
45
    bool terminate;
46
47
    struct work *work;
48
    int num_work;
49
};
50
51
static MP_THREAD_VOID worker_thread(void *arg)
52
81.1k
{
53
81.1k
    struct mp_thread_pool *pool = arg;
54
55
81.1k
    mp_thread_set_name("worker");
56
57
81.1k
    mp_mutex_lock(&pool->lock);
58
59
81.1k
    int64_t destroy_deadline = 0;
60
81.1k
    bool got_timeout = false;
61
243k
    while (1) {
62
243k
        struct work work = {0};
63
243k
        if (pool->num_work > 0) {
64
81.3k
            work = pool->work[pool->num_work - 1];
65
81.3k
            pool->num_work -= 1;
66
81.3k
        }
67
68
243k
        if (!work.fn) {
69
162k
            if (got_timeout || pool->terminate)
70
81.1k
                break;
71
72
81.3k
            if (pool->num_threads > pool->min_threads) {
73
0
                if (!destroy_deadline)
74
0
                    destroy_deadline = mp_time_ns() + MP_TIME_S_TO_NS(DESTROY_TIMEOUT);
75
0
                if (mp_cond_timedwait_until(&pool->wakeup, &pool->lock, destroy_deadline))
76
0
                    got_timeout = pool->num_threads > pool->min_threads;
77
81.3k
            } else {
78
81.3k
                mp_cond_wait(&pool->wakeup, &pool->lock);
79
81.3k
            }
80
81.3k
            continue;
81
162k
        }
82
83
81.3k
        pool->busy_threads += 1;
84
81.3k
        mp_mutex_unlock(&pool->lock);
85
86
81.3k
        work.fn(work.fn_ctx);
87
88
81.3k
        mp_mutex_lock(&pool->lock);
89
81.3k
        pool->busy_threads -= 1;
90
91
81.3k
        destroy_deadline = 0;
92
81.3k
        got_timeout = false;
93
81.3k
    }
94
95
    // If no termination signal was given, it must mean we died because of a
96
    // timeout, and nobody is waiting for us. We have to remove ourselves.
97
81.1k
    if (!pool->terminate) {
98
0
        for (int n = 0; n < pool->num_threads; n++) {
99
0
            if (mp_thread_id_equal(mp_thread_get_id(pool->threads[n]),
100
0
                                   mp_thread_current_id()))
101
0
            {
102
0
                mp_thread_detach(pool->threads[n]);
103
0
                MP_TARRAY_REMOVE_AT(pool->threads, pool->num_threads, n);
104
0
                mp_mutex_unlock(&pool->lock);
105
0
                MP_THREAD_RETURN();
106
0
            }
107
0
        }
108
0
        MP_ASSERT_UNREACHABLE();
109
0
    }
110
111
81.1k
    mp_mutex_unlock(&pool->lock);
112
81.1k
    MP_THREAD_RETURN();
113
81.1k
}
114
115
static void thread_pool_dtor(void *ctx)
116
143k
{
117
143k
    struct mp_thread_pool *pool = ctx;
118
119
120
143k
    mp_mutex_lock(&pool->lock);
121
122
143k
    pool->terminate = true;
123
143k
    mp_cond_broadcast(&pool->wakeup);
124
125
143k
    mp_thread *threads = pool->threads;
126
143k
    int num_threads = pool->num_threads;
127
128
143k
    pool->threads = NULL;
129
143k
    pool->num_threads = 0;
130
131
143k
    mp_mutex_unlock(&pool->lock);
132
133
224k
    for (int n = 0; n < num_threads; n++)
134
81.1k
        mp_thread_join(threads[n]);
135
136
143k
    mp_assert(pool->num_work == 0);
137
143k
    mp_assert(pool->num_threads == 0);
138
143k
    mp_cond_destroy(&pool->wakeup);
139
143k
    mp_mutex_destroy(&pool->lock);
140
143k
}
141
142
static bool add_thread(struct mp_thread_pool *pool)
143
81.1k
{
144
81.1k
    mp_thread thread;
145
146
81.1k
    if (mp_thread_create(&thread, worker_thread, pool) != 0)
147
0
        return false;
148
149
81.1k
    MP_TARRAY_APPEND(pool, pool->threads, pool->num_threads, thread);
150
81.1k
    return true;
151
81.1k
}
152
153
struct mp_thread_pool *mp_thread_pool_create(void *ta_parent, int init_threads,
154
                                             int min_threads, int max_threads)
155
143k
{
156
143k
    mp_assert(min_threads >= 0);
157
143k
    mp_assert(init_threads <= min_threads);
158
143k
    mp_assert(max_threads > 0 && max_threads >= min_threads);
159
160
143k
    struct mp_thread_pool *pool = talloc_zero(ta_parent, struct mp_thread_pool);
161
143k
    talloc_set_destructor(pool, thread_pool_dtor);
162
163
143k
    mp_mutex_init(&pool->lock);
164
143k
    mp_cond_init(&pool->wakeup);
165
166
143k
    pool->min_threads = min_threads;
167
143k
    pool->max_threads = max_threads;
168
169
143k
    mp_mutex_lock(&pool->lock);
170
143k
    for (int n = 0; n < init_threads; n++)
171
0
        add_thread(pool);
172
143k
    bool ok = pool->num_threads >= init_threads;
173
143k
    mp_mutex_unlock(&pool->lock);
174
175
143k
    if (!ok)
176
0
        TA_FREEP(&pool);
177
178
143k
    return pool;
179
143k
}
180
181
static bool thread_pool_add(struct mp_thread_pool *pool, void (*fn)(void *ctx),
182
                            void *fn_ctx, bool allow_queue)
183
81.3k
{
184
81.3k
    bool ok = true;
185
186
81.3k
    mp_assert(fn);
187
188
81.3k
    mp_mutex_lock(&pool->lock);
189
81.3k
    struct work work = {fn, fn_ctx};
190
191
    // If there are not enough threads to process all at once, but we can
192
    // create a new thread, then do so. If work is queued quickly, it can
193
    // happen that not all available threads have picked up work yet (up to
194
    // num_threads - busy_threads threads), which has to be accounted for.
195
81.3k
    if (pool->busy_threads + pool->num_work + 1 > pool->num_threads &&
196
81.1k
        pool->num_threads < pool->max_threads)
197
81.1k
    {
198
81.1k
        if (!add_thread(pool)) {
199
            // If we can queue it, it'll get done as long as there is 1 thread.
200
0
            ok = allow_queue && pool->num_threads > 0;
201
0
        }
202
81.1k
    }
203
204
81.3k
    if (ok) {
205
81.3k
        MP_TARRAY_INSERT_AT(pool, pool->work, pool->num_work, 0, work);
206
81.3k
        mp_cond_signal(&pool->wakeup);
207
81.3k
    }
208
209
81.3k
    mp_mutex_unlock(&pool->lock);
210
81.3k
    return ok;
211
81.3k
}
212
213
bool mp_thread_pool_queue(struct mp_thread_pool *pool, void (*fn)(void *ctx),
214
                          void *fn_ctx)
215
81.3k
{
216
81.3k
    return thread_pool_add(pool, fn, fn_ctx, true);
217
81.3k
}
218
219
bool mp_thread_pool_run(struct mp_thread_pool *pool, void (*fn)(void *ctx),
220
                        void *fn_ctx)
221
0
{
222
    return thread_pool_add(pool, fn, fn_ctx, false);
223
0
}