Coverage Report

Created: 2026-12-01 06:27

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
869
{
80
869
  DEBUGASSERT(tslot->thread == curl_thread_t_null);
81
869
  DEBUGASSERT(!tslot->running);
82
869
  Curl_cond_destroy(&tslot->await);
83
869
  curlx_free(tslot);
84
869
}
85
86
static void thrdslot_done(struct thrdslot *tslot)
87
860
{
88
860
  struct curl_thrdpool *tpool = tslot->tpool;
89
90
860
  DEBUGASSERT(Curl_node_llist(&tslot->node) == &tpool->slots);
91
860
  Curl_node_remove(&tslot->node);
92
860
  tslot->running = FALSE;
93
860
  Curl_llist_append(&tpool->zombies, tslot, &tslot->node);
94
860
  Curl_cond_signal(&tpool->await);
95
860
}
96
97
static CURL_THREAD_RETURN_T CURL_STDCALL thrdslot_run(void *arg)
98
860
{
99
860
  struct thrdslot *tslot = arg;
100
860
  struct curl_thrdpool *tpool = tslot->tpool;
101
860
  void *item;
102
103
860
  Curl_mutex_acquire(&tpool->lock);
104
860
  DEBUGASSERT(Curl_node_llist(&tslot->node) == &tpool->slots);
105
984
  for(;;) {
106
1.22k
    while(!tpool->aborted) {
107
368
      tslot->work_description = NULL;
108
368
      tslot->work_timeout_ms = 0;
109
368
      item = tpool->fn_take(tpool->fn_user_data, &tslot->work_description,
110
368
                            &tslot->work_timeout_ms);
111
368
      if(!item)
112
124
        break;
113
244
      tslot->starttime = curlx_now();
114
244
      tslot->idle = FALSE;
115
244
      Curl_mutex_release(&tpool->lock);
116
117
244
      tpool->fn_process(item);
118
119
244
      Curl_mutex_acquire(&tpool->lock);
120
244
      tslot->work_description = NULL;
121
244
      tpool->fn_return(item, tpool->aborted ? NULL : tpool->fn_user_data);
122
244
    }
123
124
984
    if(tpool->aborted ||
125
124
       (Curl_llist_count(&tpool->slots) > tpool->max_threads))
126
860
      goto out;
127
128
124
    tslot->idle = TRUE;
129
124
    tslot->starttime = curlx_now();
130
124
    thrdpool_join_zombies(tpool);
131
124
    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
124
    if((tpool->idle_time_ms > 0) &&
136
124
       (Curl_llist_count(&tpool->slots) > tpool->min_threads)) {
137
124
      CURLcode result = Curl_cond_timedwait(&tslot->await, &tpool->lock,
138
124
                                            tpool->idle_time_ms);
139
124
      if((result == CURLE_OPERATION_TIMEDOUT) &&
140
0
         (Curl_llist_count(&tpool->slots) > tpool->min_threads)) {
141
0
        goto out;
142
0
      }
143
124
    }
144
0
    else {
145
0
      Curl_cond_wait(&tslot->await, &tpool->lock);
146
0
    }
147
124
  }
148
149
860
out:
150
860
  thrdslot_done(tslot);
151
860
  if(!thrdpool_unlink(tslot->tpool, TRUE)) {
152
    /* tpool not destroyed */
153
860
    Curl_mutex_release(&tpool->lock);
154
860
  }
155
860
  return 0;
156
860
}
157
158
static CURLcode thrdslot_start(struct curl_thrdpool *tpool)
159
879
{
160
879
  struct thrdslot *tslot;
161
879
  CURLcode result = CURLE_OUT_OF_MEMORY;
162
163
879
  tslot = curlx_calloc(1, sizeof(*tslot));
164
879
  if(!tslot)
165
10
    goto out;
166
869
  tslot->id = tpool->next_id++;
167
869
  tslot->tpool = tpool;
168
869
  tslot->thread = curl_thread_t_null;
169
869
  Curl_cond_init(&tslot->await);
170
171
869
  tpool->refcount++;
172
869
  tslot->running = TRUE;
173
869
#ifdef DEBUGBUILD
174
869
  if(tpool->dbg_fail_starts > 0) {
175
0
    --tpool->dbg_fail_starts;
176
0
    tslot->thread = curl_thread_t_null;
177
0
  }
178
869
  else
179
869
#endif
180
869
    tslot->thread = Curl_thread_create(thrdslot_run, tslot);
181
869
  if(tslot->thread == curl_thread_t_null) { /* never started */
182
9
    tslot->running = FALSE;
183
9
    thrdpool_unlink(tpool, TRUE);
184
9
    result = CURLE_FAILED_INIT;
185
9
    goto out;
186
9
  }
187
188
860
  Curl_llist_append(&tpool->slots, tslot, &tslot->node);
189
860
  tslot = NULL;
190
860
  result = CURLE_OK;
191
192
879
out:
193
879
  if(tslot)
194
9
    thrdslot_destroy(tslot);
195
879
  return result;
196
860
}
197
198
static void thrdpool_wake_all(struct curl_thrdpool *tpool)
199
697
{
200
697
  struct Curl_llist_node *e;
201
1.90k
  for(e = Curl_llist_head(&tpool->slots); e; e = Curl_node_next(e)) {
202
1.20k
    struct thrdslot *tslot = Curl_node_elem(e);
203
1.20k
    Curl_cond_signal(&tslot->await);
204
1.20k
  }
205
697
}
206
207
static void thrdpool_join_zombies(struct curl_thrdpool *tpool)
208
62.6k
{
209
62.6k
  struct Curl_llist_node *e;
210
211
63.4k
  for(e = Curl_llist_head(&tpool->zombies); e;
212
62.6k
      e = Curl_llist_head(&tpool->zombies)) {
213
860
    struct thrdslot *tslot = Curl_node_elem(e);
214
215
860
    Curl_node_remove(&tslot->node);
216
860
    if(tslot->thread != curl_thread_t_null) {
217
860
      Curl_mutex_release(&tpool->lock);
218
860
      Curl_thread_join(&tslot->thread);
219
860
      Curl_mutex_acquire(&tpool->lock);
220
860
      tslot->thread = curl_thread_t_null;
221
860
    }
222
860
    thrdslot_destroy(tslot);
223
860
  }
224
62.6k
}
225
226
static bool thrdpool_unlink(struct curl_thrdpool *tpool, bool locked)
227
30.3k
{
228
30.3k
  DEBUGASSERT(tpool->refcount);
229
30.3k
  if(tpool->refcount)
230
30.3k
    tpool->refcount--;
231
30.3k
  if(tpool->refcount)
232
869
    return FALSE;
233
234
  /* no more references, free */
235
29.5k
  DEBUGASSERT(tpool->aborted);
236
29.5k
  thrdpool_join_zombies(tpool);
237
29.5k
  if(locked)
238
29.5k
    Curl_mutex_release(&tpool->lock);
239
29.5k
  Curl_cond_destroy(&tpool->await);
240
29.5k
  Curl_mutex_destroy(&tpool->lock);
241
29.5k
  curlx_free(tpool);
242
29.5k
  return TRUE;
243
29.5k
}
244
245
static CURLcode thrdpool_signal(struct curl_thrdpool *tpool,
246
                                uint32_t nthreads)
247
3.44k
{
248
3.44k
  struct Curl_llist_node *e, *n;
249
3.44k
  CURLcode result = CURLE_OK;
250
251
3.44k
  DEBUGASSERT(!tpool->aborted);
252
3.44k
  thrdpool_join_zombies(tpool);
253
254
9.11k
  for(e = Curl_llist_head(&tpool->slots); e && nthreads; e = n) {
255
5.66k
    struct thrdslot *tslot = Curl_node_elem(e);
256
5.66k
    n = Curl_node_next(e);
257
5.66k
    if(tslot->idle) {
258
1
      Curl_cond_signal(&tslot->await);
259
1
      --nthreads;
260
1
    }
261
5.66k
    else if(!tslot->starttime.tv_sec && !tslot->starttime.tv_usec) {
262
      /* starting thread, queries for work soon. */
263
4.95k
      --nthreads;
264
4.95k
    }
265
5.66k
  }
266
267
4.30k
  while(nthreads && !result &&
268
879
        Curl_llist_count(&tpool->slots) < tpool->max_threads) {
269
879
    result = thrdslot_start(tpool);
270
879
    if(result)
271
19
      break;
272
860
    --nthreads;
273
860
  }
274
275
3.44k
  return result;
276
3.44k
}
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
29.5k
{
283
29.5k
  CURLcode result = CURLE_OK;
284
29.5k
  size_t running;
285
286
29.5k
  if(!max_threads || (min_threads > max_threads))
287
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
288
289
29.5k
  Curl_mutex_acquire(&tpool->lock);
290
29.5k
  tpool->min_threads = min_threads;
291
29.5k
  tpool->max_threads = max_threads;
292
29.5k
  tpool->idle_time_ms = idle_time_ms;
293
29.5k
  running = Curl_llist_count(&tpool->slots);
294
29.5k
  if(tpool->min_threads > running) {
295
0
    result = thrdpool_signal(tpool, tpool->min_threads - (uint32_t)running);
296
0
  }
297
29.5k
  Curl_mutex_release(&tpool->lock);
298
299
29.5k
  return result;
300
29.5k
}
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
29.5k
{
312
29.5k
  struct curl_thrdpool *tpool;
313
29.5k
  CURLcode result = CURLE_OUT_OF_MEMORY;
314
29.5k
  DEBUGASSERT(name);
315
316
29.5k
  tpool = curlx_calloc(1, sizeof(*tpool));
317
29.5k
  if(!tpool)
318
24
    goto out;
319
29.5k
  tpool->refcount = 1;
320
321
29.5k
  Curl_mutex_init(&tpool->lock);
322
29.5k
  Curl_cond_init(&tpool->await);
323
29.5k
  Curl_llist_init(&tpool->slots, NULL);
324
29.5k
  Curl_llist_init(&tpool->zombies, NULL);
325
29.5k
  tpool->fn_take = fn_take;
326
29.5k
  tpool->fn_process = fn_process;
327
29.5k
  tpool->fn_return = fn_return;
328
29.5k
  tpool->fn_user_data = user_data;
329
330
  /* a const string that remains */
331
29.5k
  tpool->name = name;
332
333
29.5k
#ifdef DEBUGBUILD
334
29.5k
  {
335
29.5k
    const char *p = getenv("CURL_DBG_THRDPOOL_FAIL_STARTS");
336
29.5k
    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
29.5k
  }
342
29.5k
#endif
343
344
29.5k
  result = Curl_thrdpool_set_props(tpool, min_threads, max_threads,
345
29.5k
                                   idle_time_ms);
346
347
29.5k
out:
348
29.5k
  if(result && tpool) {
349
0
    Curl_thrdpool_destroy(tpool, TRUE);
350
0
    tpool = NULL;
351
0
  }
352
29.5k
  *ptpool = tpool;
353
29.5k
  return result;
354
29.5k
}
355
356
void Curl_thrdpool_destroy(struct curl_thrdpool *tpool, bool join)
357
29.5k
{
358
29.5k
  Curl_mutex_acquire(&tpool->lock);
359
360
29.5k
  tpool->aborted = TRUE;
361
362
30.2k
  while(join && Curl_llist_count(&tpool->slots)) {
363
697
    thrdpool_wake_all(tpool);
364
697
    Curl_cond_wait(&tpool->await, &tpool->lock);
365
697
  }
366
367
29.5k
  thrdpool_join_zombies(tpool);
368
369
  /* detach all still running threads */
370
29.5k
  if(Curl_llist_count(&tpool->slots)) {
371
0
    struct Curl_llist_node *e;
372
0
    for(e = Curl_llist_head(&tpool->slots); e; e = Curl_node_next(e)) {
373
0
      struct thrdslot *tslot = Curl_node_elem(e);
374
0
      if(tslot->thread != curl_thread_t_null)
375
0
        Curl_thread_destroy(&tslot->thread);
376
0
    }
377
0
    tpool->detached = TRUE;
378
0
  }
379
380
29.5k
  if(!thrdpool_unlink(tpool, TRUE)) {
381
    /* tpool not destroyed */
382
0
    Curl_mutex_release(&tpool->lock);
383
0
  }
384
29.5k
}
385
386
CURLcode Curl_thrdpool_signal(struct curl_thrdpool *tpool, uint32_t nthreads)
387
3.44k
{
388
3.44k
  CURLcode result;
389
390
3.44k
  Curl_mutex_acquire(&tpool->lock);
391
3.44k
  result = thrdpool_signal(tpool, nthreads);
392
3.44k
  Curl_mutex_release(&tpool->lock);
393
3.44k
  return result;
394
3.44k
}
395
396
static bool thrdpool_all_idle(struct curl_thrdpool *tpool)
397
0
{
398
0
  struct Curl_llist_node *e;
399
0
  for(e = Curl_llist_head(&tpool->slots); e; e = Curl_node_next(e)) {
400
0
    struct thrdslot *tslot = Curl_node_elem(e);
401
0
    if(!tslot->idle)
402
0
      return FALSE;
403
0
  }
404
0
  return TRUE;
405
0
}
406
407
CURLcode Curl_thrdpool_await_idle(struct curl_thrdpool *tpool,
408
                                  uint32_t timeout_ms)
409
0
{
410
0
  CURLcode result = CURLE_OK;
411
0
  struct curltime end = { 0 };
412
413
0
  Curl_mutex_acquire(&tpool->lock);
414
0
  DEBUGASSERT(!tpool->aborted);
415
0
  if(tpool->aborted) {
416
0
    result = CURLE_FAILED_INIT;
417
0
    goto out;
418
0
  }
419
420
0
  while(!thrdpool_all_idle(tpool)) {
421
0
    if(timeout_ms) {
422
0
      timediff_t remain_ms;
423
0
      CURLcode r;
424
425
0
      if(!end.tv_sec && !end.tv_usec) {
426
0
        end = curlx_now();
427
0
        end.tv_sec += (time_t)(timeout_ms / 1000);
428
0
        end.tv_usec += (int)(timeout_ms % 1000) * 1000;
429
0
        if(end.tv_usec >= 1000000) {
430
0
          end.tv_sec++;
431
0
          end.tv_usec -= 1000000;
432
0
        }
433
0
      }
434
0
      remain_ms = curlx_timediff_ms(curlx_now(), end);
435
0
      if(remain_ms <= 0)
436
0
        r = CURLE_OPERATION_TIMEDOUT;
437
0
      else
438
0
        r = Curl_cond_timedwait(&tpool->await, &tpool->lock,
439
0
                                (uint32_t)remain_ms);
440
0
      if(r == CURLE_OPERATION_TIMEDOUT) {
441
0
        result = r;
442
0
        break;
443
0
      }
444
0
    }
445
0
    else {
446
0
      Curl_cond_wait(&tpool->await, &tpool->lock);
447
0
    }
448
0
  }
449
450
0
out:
451
0
  thrdpool_join_zombies(tpool);
452
0
  Curl_mutex_release(&tpool->lock);
453
0
  return result;
454
0
}
455
456
#ifdef CURLVERBOSE
457
void Curl_thrdpool_trace(struct curl_thrdpool *tpool,
458
                         struct Curl_easy *data)
459
58
{
460
58
  struct curl_trc_feat *feat = &Curl_trc_feat_threads;
461
58
  if(Curl_trc_ft_is_verbose(data, feat)) {
462
58
    struct Curl_llist_node *e;
463
58
    struct curltime now = curlx_now();
464
465
58
    Curl_mutex_acquire(&tpool->lock);
466
58
    if(!Curl_llist_count(&tpool->slots)) {
467
41
      Curl_trc_feat_infof(data, feat, "[TPOOL-%s] no threads running",
468
41
                          tpool->name);
469
41
    }
470
90
    for(e = Curl_llist_head(&tpool->slots); e; e = Curl_node_next(e)) {
471
32
      struct thrdslot *tslot = Curl_node_elem(e);
472
32
      timediff_t elapsed_ms = curlx_ptimediff_ms(&now, &tslot->starttime);
473
32
      if(!tslot->running) {
474
0
        Curl_trc_feat_infof(data, feat, "[TPOOL-%s] [%u]: not running",
475
0
                            tpool->name, tslot->id);
476
0
      }
477
32
      else if(!tslot->starttime.tv_sec && !tslot->starttime.tv_usec) {
478
31
        Curl_trc_feat_infof(data, feat, "[TPOOL-%s] [%u]: starting...",
479
31
                            tpool->name, tslot->id);
480
31
      }
481
1
      else if(tslot->idle) {
482
0
        Curl_trc_feat_infof(data, feat, "[TPOOL-%s] [%u]: idle for %"
483
0
                            FMT_TIMEDIFF_T "ms",
484
0
                            tpool->name, tslot->id, elapsed_ms);
485
0
      }
486
1
      else {
487
1
        timediff_t remain_ms = tslot->work_timeout_ms ?
488
1
          (tslot->work_timeout_ms - elapsed_ms) : 0;
489
1
        Curl_trc_feat_infof(data, feat, "[TPOOL-%s] [%u]: busy %"
490
1
                            FMT_TIMEDIFF_T "ms, timeout in %" FMT_TIMEDIFF_T
491
1
                            "ms: %s",
492
1
                            tpool->name, tslot->id, elapsed_ms, remain_ms,
493
1
                            tslot->work_description);
494
1
      }
495
32
    }
496
58
    Curl_mutex_release(&tpool->lock);
497
58
  }
498
58
}
499
#endif
500
501
#endif /* USE_THREADS */