Coverage Report

Created: 2026-09-01 06:58

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