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
364
{
80
364
  DEBUGASSERT(tslot->thread == curl_thread_t_null);
81
364
  DEBUGASSERT(!tslot->running);
82
364
  Curl_cond_destroy(&tslot->await);
83
364
  curlx_free(tslot);
84
364
}
85
86
static void thrdslot_done(struct thrdslot *tslot)
87
333
{
88
333
  struct curl_thrdpool *tpool = tslot->tpool;
89
90
333
  DEBUGASSERT(Curl_node_llist(&tslot->node) == &tpool->slots);
91
333
  Curl_node_remove(&tslot->node);
92
333
  tslot->running = FALSE;
93
333
  Curl_llist_append(&tpool->zombies, tslot, &tslot->node);
94
333
  Curl_cond_signal(&tpool->await);
95
333
}
96
97
static CURL_THREAD_RETURN_T CURL_STDCALL thrdslot_run(void *arg)
98
333
{
99
333
  struct thrdslot *tslot = arg;
100
333
  struct curl_thrdpool *tpool = tslot->tpool;
101
333
  void *item;
102
103
333
  Curl_mutex_acquire(&tpool->lock);
104
333
  DEBUGASSERT(Curl_node_llist(&tslot->node) == &tpool->slots);
105
373
  for(;;) {
106
475
    while(!tpool->aborted) {
107
142
      tslot->work_description = NULL;
108
142
      tslot->work_timeout_ms = 0;
109
142
      item = tpool->fn_take(tpool->fn_user_data, &tslot->work_description,
110
142
                            &tslot->work_timeout_ms);
111
142
      if(!item)
112
40
        break;
113
102
      tslot->starttime = curlx_now();
114
102
      tslot->idle = FALSE;
115
102
      Curl_mutex_release(&tpool->lock);
116
117
102
      tpool->fn_process(item);
118
119
102
      Curl_mutex_acquire(&tpool->lock);
120
102
      tslot->work_description = NULL;
121
102
      tpool->fn_return(item, tpool->aborted ? NULL : tpool->fn_user_data);
122
102
    }
123
124
373
    if(tpool->aborted ||
125
40
       (Curl_llist_count(&tpool->slots) > tpool->max_threads))
126
333
      goto out;
127
128
40
    tslot->idle = TRUE;
129
40
    tslot->starttime = curlx_now();
130
40
    thrdpool_join_zombies(tpool);
131
40
    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
40
    if((tpool->idle_time_ms > 0) &&
136
40
       (Curl_llist_count(&tpool->slots) > tpool->min_threads)) {
137
40
      CURLcode result = Curl_cond_timedwait(&tslot->await, &tpool->lock,
138
40
                                            tpool->idle_time_ms);
139
40
      if((result == CURLE_OPERATION_TIMEDOUT) &&
140
0
         (Curl_llist_count(&tpool->slots) > tpool->min_threads)) {
141
0
        goto out;
142
0
      }
143
40
    }
144
0
    else {
145
0
      Curl_cond_wait(&tslot->await, &tpool->lock);
146
0
    }
147
40
  }
148
149
333
out:
150
333
  thrdslot_done(tslot);
151
333
  if(!thrdpool_unlink(tslot->tpool, TRUE)) {
152
    /* tpool not destroyed */
153
333
    Curl_mutex_release(&tpool->lock);
154
333
  }
155
333
  return 0;
156
333
}
157
158
static CURLcode thrdslot_start(struct curl_thrdpool *tpool)
159
374
{
160
374
  struct thrdslot *tslot;
161
374
  CURLcode result = CURLE_OUT_OF_MEMORY;
162
163
374
  tslot = curlx_calloc(1, sizeof(*tslot));
164
374
  if(!tslot)
165
10
    goto out;
166
364
  tslot->id = tpool->next_id++;
167
364
  tslot->tpool = tpool;
168
364
  tslot->thread = curl_thread_t_null;
169
364
  Curl_cond_init(&tslot->await);
170
171
364
  tpool->refcount++;
172
364
  tslot->running = TRUE;
173
364
#ifdef DEBUGBUILD
174
364
  if(tpool->dbg_fail_starts > 0) {
175
0
    --tpool->dbg_fail_starts;
176
0
    tslot->thread = curl_thread_t_null;
177
0
  }
178
364
  else
179
364
#endif
180
364
    tslot->thread = Curl_thread_create(thrdslot_run, tslot);
181
364
  if(tslot->thread == curl_thread_t_null) { /* never started */
182
31
    tslot->running = FALSE;
183
31
    thrdpool_unlink(tpool, TRUE);
184
31
    result = CURLE_FAILED_INIT;
185
31
    goto out;
186
31
  }
187
188
333
  Curl_llist_append(&tpool->slots, tslot, &tslot->node);
189
333
  tslot = NULL;
190
333
  result = CURLE_OK;
191
192
374
out:
193
374
  if(tslot)
194
31
    thrdslot_destroy(tslot);
195
374
  return result;
196
333
}
197
198
static void thrdpool_wake_all(struct curl_thrdpool *tpool)
199
284
{
200
284
  struct Curl_llist_node *e;
201
753
  for(e = Curl_llist_head(&tpool->slots); e; e = Curl_node_next(e)) {
202
469
    struct thrdslot *tslot = Curl_node_elem(e);
203
469
    Curl_cond_signal(&tslot->await);
204
469
  }
205
284
}
206
207
static void thrdpool_join_zombies(struct curl_thrdpool *tpool)
208
19.2k
{
209
19.2k
  struct Curl_llist_node *e;
210
211
19.6k
  for(e = Curl_llist_head(&tpool->zombies); e;
212
19.2k
      e = Curl_llist_head(&tpool->zombies)) {
213
333
    struct thrdslot *tslot = Curl_node_elem(e);
214
215
333
    Curl_node_remove(&tslot->node);
216
333
    if(tslot->thread != curl_thread_t_null) {
217
333
      Curl_mutex_release(&tpool->lock);
218
333
      Curl_thread_join(&tslot->thread);
219
333
      Curl_mutex_acquire(&tpool->lock);
220
333
      tslot->thread = curl_thread_t_null;
221
333
    }
222
333
    thrdslot_destroy(tslot);
223
333
  }
224
19.2k
}
225
226
static bool thrdpool_unlink(struct curl_thrdpool *tpool, bool locked)
227
9.32k
{
228
9.32k
  DEBUGASSERT(tpool->refcount);
229
9.32k
  if(tpool->refcount)
230
9.32k
    tpool->refcount--;
231
9.32k
  if(tpool->refcount)
232
364
    return FALSE;
233
234
  /* no more references, free */
235
8.95k
  DEBUGASSERT(tpool->aborted);
236
8.95k
  thrdpool_join_zombies(tpool);
237
8.95k
  if(locked)
238
8.95k
    Curl_mutex_release(&tpool->lock);
239
8.95k
  Curl_cond_destroy(&tpool->await);
240
8.95k
  Curl_mutex_destroy(&tpool->lock);
241
8.95k
  curlx_free(tpool);
242
8.95k
  return TRUE;
243
8.95k
}
244
245
static CURLcode thrdpool_signal(struct curl_thrdpool *tpool,
246
                                uint32_t nthreads)
247
1.33k
{
248
1.33k
  struct Curl_llist_node *e, *n;
249
1.33k
  CURLcode result = CURLE_OK;
250
251
1.33k
  DEBUGASSERT(!tpool->aborted);
252
1.33k
  thrdpool_join_zombies(tpool);
253
254
3.48k
  for(e = Curl_llist_head(&tpool->slots); e && nthreads; e = n) {
255
2.15k
    struct thrdslot *tslot = Curl_node_elem(e);
256
2.15k
    n = Curl_node_next(e);
257
2.15k
    if(tslot->idle) {
258
1
      Curl_cond_signal(&tslot->await);
259
1
      --nthreads;
260
1
    }
261
2.15k
    else if(!tslot->starttime.tv_sec && !tslot->starttime.tv_usec) {
262
      /* starting thread, queries for work soon. */
263
1.89k
      --nthreads;
264
1.89k
    }
265
2.15k
  }
266
267
1.67k
  while(nthreads && !result &&
268
374
        Curl_llist_count(&tpool->slots) < tpool->max_threads) {
269
374
    result = thrdslot_start(tpool);
270
374
    if(result)
271
41
      break;
272
333
    --nthreads;
273
333
  }
274
275
1.33k
  return result;
276
1.33k
}
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
8.95k
{
283
8.95k
  CURLcode result = CURLE_OK;
284
8.95k
  size_t running;
285
286
8.95k
  if(!max_threads || (min_threads > max_threads))
287
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
288
289
8.95k
  Curl_mutex_acquire(&tpool->lock);
290
8.95k
  tpool->min_threads = min_threads;
291
8.95k
  tpool->max_threads = max_threads;
292
8.95k
  tpool->idle_time_ms = idle_time_ms;
293
8.95k
  running = Curl_llist_count(&tpool->slots);
294
8.95k
  if(tpool->min_threads > running) {
295
0
    result = thrdpool_signal(tpool, tpool->min_threads - (uint32_t)running);
296
0
  }
297
8.95k
  Curl_mutex_release(&tpool->lock);
298
299
8.95k
  return result;
300
8.95k
}
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
8.96k
{
312
8.96k
  struct curl_thrdpool *tpool;
313
8.96k
  CURLcode result = CURLE_OUT_OF_MEMORY;
314
8.96k
  DEBUGASSERT(name);
315
316
8.96k
  tpool = curlx_calloc(1, sizeof(*tpool));
317
8.96k
  if(!tpool)
318
5
    goto out;
319
8.95k
  tpool->refcount = 1;
320
321
8.95k
  Curl_mutex_init(&tpool->lock);
322
8.95k
  Curl_cond_init(&tpool->await);
323
8.95k
  Curl_llist_init(&tpool->slots, NULL);
324
8.95k
  Curl_llist_init(&tpool->zombies, NULL);
325
8.95k
  tpool->fn_take = fn_take;
326
8.95k
  tpool->fn_process = fn_process;
327
8.95k
  tpool->fn_return = fn_return;
328
8.95k
  tpool->fn_user_data = user_data;
329
330
  /* a const string that remains */
331
8.95k
  tpool->name = name;
332
333
8.95k
#ifdef DEBUGBUILD
334
8.95k
  {
335
8.95k
    const char *p = getenv("CURL_DBG_THRDPOOL_FAIL_STARTS");
336
8.95k
    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
8.95k
  }
342
8.95k
#endif
343
344
8.95k
  result = Curl_thrdpool_set_props(tpool, min_threads, max_threads,
345
8.95k
                                   idle_time_ms);
346
347
8.96k
out:
348
8.96k
  if(result && tpool) {
349
0
    Curl_thrdpool_destroy(tpool, TRUE);
350
0
    tpool = NULL;
351
0
  }
352
8.96k
  *ptpool = tpool;
353
8.96k
  return result;
354
8.95k
}
355
356
void Curl_thrdpool_destroy(struct curl_thrdpool *tpool, bool join)
357
8.95k
{
358
8.95k
  Curl_mutex_acquire(&tpool->lock);
359
360
8.95k
  tpool->aborted = TRUE;
361
362
9.24k
  while(join && Curl_llist_count(&tpool->slots)) {
363
284
    thrdpool_wake_all(tpool);
364
284
    Curl_cond_wait(&tpool->await, &tpool->lock);
365
284
  }
366
367
8.95k
  thrdpool_join_zombies(tpool);
368
369
  /* detach all still running threads */
370
8.95k
  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
8.95k
  if(!thrdpool_unlink(tpool, TRUE)) {
381
    /* tpool not destroyed */
382
0
    Curl_mutex_release(&tpool->lock);
383
0
  }
384
8.95k
}
385
386
CURLcode Curl_thrdpool_signal(struct curl_thrdpool *tpool, uint32_t nthreads)
387
1.33k
{
388
1.33k
  CURLcode result;
389
390
1.33k
  Curl_mutex_acquire(&tpool->lock);
391
1.33k
  result = thrdpool_signal(tpool, nthreads);
392
1.33k
  Curl_mutex_release(&tpool->lock);
393
1.33k
  return result;
394
1.33k
}
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
51
{
460
51
  struct curl_trc_feat *feat = &Curl_trc_feat_threads;
461
51
  if(Curl_trc_ft_is_verbose(data, feat)) {
462
51
    struct Curl_llist_node *e;
463
51
    struct curltime now = curlx_now();
464
465
51
    Curl_mutex_acquire(&tpool->lock);
466
51
    if(!Curl_llist_count(&tpool->slots)) {
467
2
      Curl_trc_feat_infof(data, feat, "[TPOOL-%s] no threads running",
468
2
                          tpool->name);
469
2
    }
470
141
    for(e = Curl_llist_head(&tpool->slots); e; e = Curl_node_next(e)) {
471
90
      struct thrdslot *tslot = Curl_node_elem(e);
472
90
      timediff_t elapsed_ms = curlx_ptimediff_ms(&now, &tslot->starttime);
473
90
      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
90
      else if(!tslot->starttime.tv_sec && !tslot->starttime.tv_usec) {
478
90
        Curl_trc_feat_infof(data, feat, "[TPOOL-%s] [%u]: starting...",
479
90
                            tpool->name, tslot->id);
480
90
      }
481
0
      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
0
      else {
487
0
        timediff_t remain_ms = tslot->work_timeout_ms ?
488
0
          (tslot->work_timeout_ms - elapsed_ms) : 0;
489
0
        Curl_trc_feat_infof(data, feat, "[TPOOL-%s] [%u]: busy %"
490
0
                            FMT_TIMEDIFF_T "ms, timeout in %" FMT_TIMEDIFF_T
491
0
                            "ms: %s",
492
0
                            tpool->name, tslot->id, elapsed_ms, remain_ms,
493
0
                            tslot->work_description);
494
0
      }
495
90
    }
496
51
    Curl_mutex_release(&tpool->lock);
497
51
  }
498
51
}
499
#endif
500
501
#endif /* USE_THREADS */