Coverage Report

Created: 2026-10-15 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/thrdpool.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
#ifdef USE_THREADS
27
28
#include "llist.h"
29
#include "curl_threads.h"
30
#include "curlx/timeval.h"
31
#include "curlx/strparse.h"
32
#include "thrdpool.h"
33
#ifdef CURLVERBOSE
34
#include "curl_trc.h"
35
#include "urldata.h"
36
#endif
37
38
39
struct thrdslot {
40
  struct Curl_llist_node node;
41
  struct curl_thrdpool *tpool;
42
  curl_thread_t thread;
43
  curl_cond_t await;
44
  struct curltime starttime;
45
  const char *work_description;
46
  timediff_t work_timeout_ms;
47
  uint32_t id;
48
  BIT(running);
49
  BIT(idle);
50
};
51
52
struct curl_thrdpool {
53
  const char *name;
54
  uint64_t refcount;
55
  curl_mutex_t lock;
56
  curl_cond_t await;
57
  struct Curl_llist slots;
58
  struct Curl_llist zombies;
59
  Curl_thrdpool_take_item_cb *fn_take;
60
  Curl_thrdpool_process_item_cb *fn_process;
61
  Curl_thrdpool_return_item_cb *fn_return;
62
  void *fn_user_data;
63
  CURLcode fatal_err;
64
  uint32_t min_threads;
65
  uint32_t max_threads;
66
  uint32_t idle_time_ms;
67
  uint32_t next_id;
68
#ifdef DEBUGBUILD
69
  int dbg_fail_starts; /* fail this many thread starts */
70
#endif
71
  BIT(aborted);
72
  BIT(detached);
73
};
74
75
static void thrdpool_join_zombies(struct curl_thrdpool *tpool);
76
static bool thrdpool_unlink(struct curl_thrdpool *tpool, bool locked);
77
78
static void thrdslot_destroy(struct thrdslot *tslot)
79
137
{
80
137
  DEBUGASSERT(tslot->thread == curl_thread_t_null);
81
137
  DEBUGASSERT(!tslot->running);
82
137
  Curl_cond_destroy(&tslot->await);
83
137
  curlx_free(tslot);
84
137
}
85
86
static void thrdslot_done(struct thrdslot *tslot)
87
132
{
88
132
  struct curl_thrdpool *tpool = tslot->tpool;
89
90
132
  DEBUGASSERT(Curl_node_llist(&tslot->node) == &tpool->slots);
91
132
  Curl_node_remove(&tslot->node);
92
132
  tslot->running = FALSE;
93
132
  Curl_llist_append(&tpool->zombies, tslot, &tslot->node);
94
132
  Curl_cond_signal(&tpool->await);
95
132
}
96
97
static CURL_THREAD_RETURN_T CURL_STDCALL thrdslot_run(void *arg)
98
132
{
99
132
  struct thrdslot *tslot = arg;
100
132
  struct curl_thrdpool *tpool = tslot->tpool;
101
132
  void *item;
102
103
132
  Curl_mutex_acquire(&tpool->lock);
104
132
  DEBUGASSERT(Curl_node_llist(&tslot->node) == &tpool->slots);
105
251
  for(;;) {
106
377
    while(!tpool->aborted) {
107
245
      tslot->work_description = NULL;
108
245
      tslot->work_timeout_ms = 0;
109
245
      item = tpool->fn_take(tpool->fn_user_data, &tslot->work_description,
110
245
                            &tslot->work_timeout_ms);
111
245
      if(!item)
112
119
        break;
113
126
      tslot->starttime = curlx_now();
114
126
      tslot->idle = FALSE;
115
126
      Curl_mutex_release(&tpool->lock);
116
117
126
      tpool->fn_process(item);
118
119
126
      Curl_mutex_acquire(&tpool->lock);
120
126
      tslot->work_description = NULL;
121
126
      tpool->fn_return(item, tpool->aborted ? NULL : tpool->fn_user_data);
122
126
    }
123
124
251
    if(tpool->aborted ||
125
119
       (Curl_llist_count(&tpool->slots) > tpool->max_threads))
126
132
      goto out;
127
128
119
    tslot->idle = TRUE;
129
119
    tslot->starttime = curlx_now();
130
119
    thrdpool_join_zombies(tpool);
131
119
    Curl_cond_signal(&tpool->await);
132
    /* Only wait with idle timeout when we are above the minimum
133
     * number of threads. Otherwise short idle timeouts will keep
134
     * on activating threads that have no means to shut down. */
135
119
    if((tpool->idle_time_ms > 0) &&
136
119
       (Curl_llist_count(&tpool->slots) > tpool->min_threads)) {
137
119
      CURLcode result = Curl_cond_timedwait(&tslot->await, &tpool->lock,
138
119
                                            tpool->idle_time_ms);
139
119
      if((result == CURLE_OPERATION_TIMEDOUT) &&
140
0
         (Curl_llist_count(&tpool->slots) > tpool->min_threads)) {
141
0
        goto out;
142
0
      }
143
119
    }
144
0
    else {
145
0
      Curl_cond_wait(&tslot->await, &tpool->lock);
146
0
    }
147
119
  }
148
149
132
out:
150
132
  thrdslot_done(tslot);
151
132
  if(!thrdpool_unlink(tslot->tpool, TRUE)) {
152
    /* tpool not destroyed */
153
132
    Curl_mutex_release(&tpool->lock);
154
132
  }
155
132
  return 0;
156
132
}
157
158
static CURLcode thrdslot_start(struct curl_thrdpool *tpool)
159
138
{
160
138
  struct thrdslot *tslot;
161
138
  CURLcode result = CURLE_OUT_OF_MEMORY;
162
163
138
  tslot = curlx_calloc(1, sizeof(*tslot));
164
138
  if(!tslot)
165
1
    goto out;
166
137
  tslot->id = tpool->next_id++;
167
137
  tslot->tpool = tpool;
168
137
  tslot->thread = curl_thread_t_null;
169
137
  Curl_cond_init(&tslot->await);
170
171
137
  tpool->refcount++;
172
137
  tslot->running = TRUE;
173
137
#ifdef DEBUGBUILD
174
137
  if(tpool->dbg_fail_starts > 0) {
175
0
    --tpool->dbg_fail_starts;
176
0
    tslot->thread = curl_thread_t_null;
177
0
  }
178
137
  else
179
137
#endif
180
137
    tslot->thread = Curl_thread_create(thrdslot_run, tslot);
181
137
  if(tslot->thread == curl_thread_t_null) { /* never started */
182
5
    tslot->running = FALSE;
183
5
    thrdpool_unlink(tpool, TRUE);
184
5
    result = CURLE_FAILED_INIT;
185
5
    goto out;
186
5
  }
187
188
132
  Curl_llist_append(&tpool->slots, tslot, &tslot->node);
189
132
  tslot = NULL;
190
132
  result = CURLE_OK;
191
192
138
out:
193
138
  if(tslot)
194
5
    thrdslot_destroy(tslot);
195
138
  return result;
196
132
}
197
198
static void thrdpool_wake_all(struct curl_thrdpool *tpool)
199
97
{
200
97
  struct Curl_llist_node *e;
201
258
  for(e = Curl_llist_head(&tpool->slots); e; e = Curl_node_next(e)) {
202
161
    struct thrdslot *tslot = Curl_node_elem(e);
203
161
    Curl_cond_signal(&tslot->await);
204
161
  }
205
97
}
206
207
static void thrdpool_join_zombies(struct curl_thrdpool *tpool)
208
38.8k
{
209
38.8k
  struct Curl_llist_node *e;
210
211
39.0k
  for(e = Curl_llist_head(&tpool->zombies); e;
212
38.8k
      e = Curl_llist_head(&tpool->zombies)) {
213
132
    struct thrdslot *tslot = Curl_node_elem(e);
214
215
132
    Curl_node_remove(&tslot->node);
216
132
    if(tslot->thread != curl_thread_t_null) {
217
132
      Curl_mutex_release(&tpool->lock);
218
132
      Curl_thread_join(&tslot->thread);
219
132
      Curl_mutex_acquire(&tpool->lock);
220
132
      tslot->thread = curl_thread_t_null;
221
132
    }
222
132
    thrdslot_destroy(tslot);
223
132
  }
224
38.8k
}
225
226
static bool thrdpool_unlink(struct curl_thrdpool *tpool, bool locked)
227
19.3k
{
228
19.3k
  DEBUGASSERT(tpool->refcount);
229
19.3k
  if(tpool->refcount)
230
19.3k
    tpool->refcount--;
231
19.3k
  if(tpool->refcount)
232
137
    return FALSE;
233
234
  /* no more references, free */
235
19.2k
  DEBUGASSERT(tpool->aborted);
236
19.2k
  thrdpool_join_zombies(tpool);
237
19.2k
  if(locked)
238
19.2k
    Curl_mutex_release(&tpool->lock);
239
19.2k
  Curl_cond_destroy(&tpool->await);
240
19.2k
  Curl_mutex_destroy(&tpool->lock);
241
19.2k
  curlx_free(tpool);
242
19.2k
  return TRUE;
243
19.2k
}
244
245
static CURLcode thrdpool_signal(struct curl_thrdpool *tpool,
246
                                uint32_t nthreads)
247
257
{
248
257
  struct Curl_llist_node *e, *n;
249
257
  CURLcode result = CURLE_OK;
250
251
257
  DEBUGASSERT(!tpool->aborted);
252
257
  thrdpool_join_zombies(tpool);
253
254
566
  for(e = Curl_llist_head(&tpool->slots); e && nthreads; e = n) {
255
309
    struct thrdslot *tslot = Curl_node_elem(e);
256
309
    n = Curl_node_next(e);
257
309
    if(tslot->idle) {
258
0
      Curl_cond_signal(&tslot->await);
259
0
      --nthreads;
260
0
    }
261
309
    else if(!tslot->starttime.tv_sec && !tslot->starttime.tv_usec) {
262
      /* starting thread, queries for work soon. */
263
304
      --nthreads;
264
304
    }
265
309
  }
266
267
389
  while(nthreads && !result &&
268
138
        Curl_llist_count(&tpool->slots) < tpool->max_threads) {
269
138
    result = thrdslot_start(tpool);
270
138
    if(result)
271
6
      break;
272
132
    --nthreads;
273
132
  }
274
275
257
  return result;
276
257
}
277
278
CURLcode Curl_thrdpool_set_props(struct curl_thrdpool *tpool,
279
                                 uint32_t min_threads,
280
                                 uint32_t max_threads,
281
                                 uint32_t idle_time_ms)
282
19.2k
{
283
19.2k
  CURLcode result = CURLE_OK;
284
19.2k
  size_t running;
285
286
19.2k
  if(!max_threads || (min_threads > max_threads))
287
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
288
289
19.2k
  Curl_mutex_acquire(&tpool->lock);
290
19.2k
  tpool->min_threads = min_threads;
291
19.2k
  tpool->max_threads = max_threads;
292
19.2k
  tpool->idle_time_ms = idle_time_ms;
293
19.2k
  running = Curl_llist_count(&tpool->slots);
294
19.2k
  if(tpool->min_threads > running) {
295
0
    result = thrdpool_signal(tpool, tpool->min_threads - (uint32_t)running);
296
0
  }
297
19.2k
  Curl_mutex_release(&tpool->lock);
298
299
19.2k
  return result;
300
19.2k
}
301
302
CURLcode Curl_thrdpool_create(struct curl_thrdpool **ptpool,
303
                              const char *name,
304
                              uint32_t min_threads,
305
                              uint32_t max_threads,
306
                              uint32_t idle_time_ms,
307
                              Curl_thrdpool_take_item_cb *fn_take,
308
                              Curl_thrdpool_process_item_cb *fn_process,
309
                              Curl_thrdpool_return_item_cb *fn_return,
310
                              void *user_data)
311
19.2k
{
312
19.2k
  struct curl_thrdpool *tpool;
313
19.2k
  CURLcode result = CURLE_OUT_OF_MEMORY;
314
19.2k
  DEBUGASSERT(name);
315
316
19.2k
  tpool = curlx_calloc(1, sizeof(*tpool));
317
19.2k
  if(!tpool)
318
4
    goto out;
319
19.2k
  tpool->refcount = 1;
320
321
19.2k
  Curl_mutex_init(&tpool->lock);
322
19.2k
  Curl_cond_init(&tpool->await);
323
19.2k
  Curl_llist_init(&tpool->slots, NULL);
324
19.2k
  Curl_llist_init(&tpool->zombies, NULL);
325
19.2k
  tpool->fn_take = fn_take;
326
19.2k
  tpool->fn_process = fn_process;
327
19.2k
  tpool->fn_return = fn_return;
328
19.2k
  tpool->fn_user_data = user_data;
329
330
  /* a const string that remains */
331
19.2k
  tpool->name = name;
332
333
19.2k
#ifdef DEBUGBUILD
334
19.2k
  {
335
19.2k
    const char *p = getenv("CURL_DBG_THRDPOOL_FAIL_STARTS");
336
19.2k
    if(p) {
337
0
      curl_off_t l;
338
0
      if(!curlx_str_number(&p, &l, INT_MAX))
339
0
        tpool->dbg_fail_starts = (int)l;
340
0
    }
341
19.2k
  }
342
19.2k
#endif
343
344
19.2k
  result = Curl_thrdpool_set_props(tpool, min_threads, max_threads,
345
19.2k
                                   idle_time_ms);
346
347
19.2k
out:
348
19.2k
  if(result && tpool) {
349
0
    tpool->aborted = TRUE;
350
0
    thrdpool_unlink(tpool, FALSE);
351
0
    tpool = NULL;
352
0
  }
353
19.2k
  *ptpool = tpool;
354
19.2k
  return result;
355
19.2k
}
356
357
void Curl_thrdpool_destroy(struct curl_thrdpool *tpool, bool join)
358
19.2k
{
359
19.2k
  Curl_mutex_acquire(&tpool->lock);
360
361
19.2k
  tpool->aborted = TRUE;
362
363
19.3k
  while(join && Curl_llist_count(&tpool->slots)) {
364
97
    thrdpool_wake_all(tpool);
365
97
    Curl_cond_wait(&tpool->await, &tpool->lock);
366
97
  }
367
368
19.2k
  thrdpool_join_zombies(tpool);
369
370
  /* detach all still running threads */
371
19.2k
  if(Curl_llist_count(&tpool->slots)) {
372
0
    struct Curl_llist_node *e;
373
0
    for(e = Curl_llist_head(&tpool->slots); e; e = Curl_node_next(e)) {
374
0
      struct thrdslot *tslot = Curl_node_elem(e);
375
0
      if(tslot->thread != curl_thread_t_null)
376
0
        Curl_thread_destroy(&tslot->thread);
377
0
    }
378
0
    tpool->detached = TRUE;
379
0
  }
380
381
19.2k
  if(!thrdpool_unlink(tpool, TRUE)) {
382
    /* tpool not destroyed */
383
0
    Curl_mutex_release(&tpool->lock);
384
0
  }
385
19.2k
}
386
387
CURLcode Curl_thrdpool_signal(struct curl_thrdpool *tpool, uint32_t nthreads)
388
257
{
389
257
  CURLcode result;
390
391
257
  Curl_mutex_acquire(&tpool->lock);
392
257
  result = thrdpool_signal(tpool, nthreads);
393
257
  Curl_mutex_release(&tpool->lock);
394
257
  return result;
395
257
}
396
397
static bool thrdpool_all_idle(struct curl_thrdpool *tpool)
398
0
{
399
0
  struct Curl_llist_node *e;
400
0
  for(e = Curl_llist_head(&tpool->slots); e; e = Curl_node_next(e)) {
401
0
    struct thrdslot *tslot = Curl_node_elem(e);
402
0
    if(!tslot->idle)
403
0
      return FALSE;
404
0
  }
405
0
  return TRUE;
406
0
}
407
408
CURLcode Curl_thrdpool_await_idle(struct curl_thrdpool *tpool,
409
                                  uint32_t timeout_ms)
410
0
{
411
0
  CURLcode result = CURLE_OK;
412
0
  struct curltime end = { 0 };
413
414
0
  Curl_mutex_acquire(&tpool->lock);
415
0
  DEBUGASSERT(!tpool->aborted);
416
0
  if(tpool->aborted) {
417
0
    result = CURLE_FAILED_INIT;
418
0
    goto out;
419
0
  }
420
421
0
  while(!thrdpool_all_idle(tpool)) {
422
0
    if(timeout_ms) {
423
0
      timediff_t remain_ms;
424
0
      CURLcode r;
425
426
0
      if(!end.tv_sec && !end.tv_usec) {
427
0
        end = curlx_now();
428
0
        end.tv_sec += (time_t)(timeout_ms / 1000);
429
0
        end.tv_usec += (int)(timeout_ms % 1000) * 1000;
430
0
        if(end.tv_usec >= 1000000) {
431
0
          end.tv_sec++;
432
0
          end.tv_usec -= 1000000;
433
0
        }
434
0
      }
435
0
      remain_ms = curlx_timediff_ms(curlx_now(), end);
436
0
      if(remain_ms <= 0)
437
0
        r = CURLE_OPERATION_TIMEDOUT;
438
0
      else
439
0
        r = Curl_cond_timedwait(&tpool->await, &tpool->lock,
440
0
                                (uint32_t)remain_ms);
441
0
      if(r == CURLE_OPERATION_TIMEDOUT) {
442
0
        result = r;
443
0
        break;
444
0
      }
445
0
    }
446
0
    else {
447
0
      Curl_cond_wait(&tpool->await, &tpool->lock);
448
0
    }
449
0
  }
450
451
0
out:
452
0
  thrdpool_join_zombies(tpool);
453
0
  Curl_mutex_release(&tpool->lock);
454
0
  return result;
455
0
}
456
457
#ifdef CURLVERBOSE
458
void Curl_thrdpool_trace(struct curl_thrdpool *tpool,
459
                         struct Curl_easy *data)
460
0
{
461
0
  struct curl_trc_feat *feat = &Curl_trc_feat_threads;
462
0
  if(Curl_trc_ft_is_verbose(data, feat)) {
463
0
    struct Curl_llist_node *e;
464
0
    struct curltime now = curlx_now();
465
466
0
    Curl_mutex_acquire(&tpool->lock);
467
0
    if(!Curl_llist_count(&tpool->slots)) {
468
0
      Curl_trc_feat_infof(data, feat, "[TPOOL-%s] no threads running",
469
0
                          tpool->name);
470
0
    }
471
0
    for(e = Curl_llist_head(&tpool->slots); e; e = Curl_node_next(e)) {
472
0
      struct thrdslot *tslot = Curl_node_elem(e);
473
0
      timediff_t elapsed_ms = curlx_ptimediff_ms(&now, &tslot->starttime);
474
0
      if(!tslot->running) {
475
0
        Curl_trc_feat_infof(data, feat, "[TPOOL-%s] [%u]: not running",
476
0
                            tpool->name, tslot->id);
477
0
      }
478
0
      else if(!tslot->starttime.tv_sec && !tslot->starttime.tv_usec) {
479
0
        Curl_trc_feat_infof(data, feat, "[TPOOL-%s] [%u]: starting...",
480
0
                            tpool->name, tslot->id);
481
0
      }
482
0
      else if(tslot->idle) {
483
0
        Curl_trc_feat_infof(data, feat, "[TPOOL-%s] [%u]: idle for %"
484
0
                            FMT_TIMEDIFF_T "ms",
485
0
                            tpool->name, tslot->id, elapsed_ms);
486
0
      }
487
0
      else {
488
0
        timediff_t remain_ms = tslot->work_timeout_ms ?
489
0
          (tslot->work_timeout_ms - elapsed_ms) : 0;
490
0
        Curl_trc_feat_infof(data, feat, "[TPOOL-%s] [%u]: busy %"
491
0
                            FMT_TIMEDIFF_T "ms, timeout in %" FMT_TIMEDIFF_T
492
0
                            "ms: %s",
493
0
                            tpool->name, tslot->id, elapsed_ms, remain_ms,
494
0
                            tslot->work_description);
495
0
      }
496
0
    }
497
0
    Curl_mutex_release(&tpool->lock);
498
0
  }
499
0
}
500
#endif
501
502
#endif /* USE_THREADS */