Coverage Report

Created: 2026-12-01 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/httrack/src/htsthread.c
Line
Count
Source
1
/* ------------------------------------------------------------ */
2
/*
3
HTTrack Website Copier, Offline Browser for Windows and Unix
4
Copyright (C) 1998 Xavier Roche and other contributors
5
6
SPDX-License-Identifier: GPL-3.0-or-later
7
8
This program is free software: you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation, either version 3 of the License, or
11
(at your option) any later version.
12
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
GNU General Public License for more details.
17
18
You should have received a copy of the GNU General Public License
19
along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21
Ethical use: we kindly ask that you NOT use this software to harvest email
22
addresses or to collect any other private information about people. Doing so
23
would dishonor our work and waste the many hours we have spent on it.
24
25
Please visit our Website: http://www.httrack.com
26
*/
27
28
/* ------------------------------------------------------------ */
29
/* File: Threads                                                */
30
/* Author: Xavier Roche                                         */
31
/* ------------------------------------------------------------ */
32
33
#include "htsglobal.h"
34
#include "htsbase.h"
35
#include "htsthread.h"
36
#include "httrack-library.h"
37
38
#ifdef _WIN32
39
#include <process.h>
40
#endif
41
42
/* Outstanding threads, counted at spawn rather than by the child at entry, so
43
   that a caller which spawns and immediately waits still joins them (#747). */
44
static int process_chain = 0;
45
static htsmutex process_chain_mutex = HTSMUTEX_INIT;
46
47
0
static void process_chain_add(int delta) {
48
0
  hts_mutexlock(&process_chain_mutex);
49
0
  process_chain += delta;
50
0
  assertf(process_chain >= 0);
51
0
  hts_mutexrelease(&process_chain_mutex);
52
0
}
53
54
0
HTSEXT_API void htsthread_wait(void) {
55
0
  htsthread_wait_n(0);
56
0
}
57
58
0
HTSEXT_API void htsthread_wait_n(int n_wait) {
59
0
  int wait = 0;
60
61
0
  do {
62
0
    hts_mutexlock(&process_chain_mutex);
63
0
    wait = (process_chain > n_wait);
64
0
    hts_mutexrelease(&process_chain_mutex);
65
0
    if (wait)
66
0
      Sleep(100);
67
0
  } while (wait);
68
0
}
69
70
/* hts_mutexlock() builds process_chain_mutex on first use. */
71
1
void htsthread_init(void) {
72
#if (defined(_DEBUG) || defined(DEBUG))
73
  assertf(process_chain == 0);
74
#endif
75
1
}
76
77
0
void htsthread_uninit(void) {
78
0
  hts_boolean idle;
79
80
0
  hts_mutexlock(&process_chain_mutex);
81
0
  idle = process_chain == 0;
82
0
  hts_mutexrelease(&process_chain_mutex);
83
  /* a live worker, maybe the caller, still takes the lock when it ends */
84
0
  if (idle)
85
0
    hts_mutexfree(&process_chain_mutex);
86
0
}
87
88
0
hts_boolean htsthread_lock_live(void) {
89
0
  return process_chain_mutex != HTSMUTEX_INIT;
90
0
}
91
92
typedef struct hts_thread_s {
93
  void *arg;
94
  void (*fun) (void *arg);
95
  void (*tail)(void *arg);
96
  int round;
97
} hts_thread_s;
98
99
/* A body and whether it reached its end. */
100
typedef struct hts_body_s {
101
  void *arg;
102
  void (*fun)(void *arg);
103
  hts_boolean returned;
104
} hts_body_s;
105
106
0
static void hts_run_body(void *arg) {
107
0
  hts_body_s *const body = (hts_body_s *) arg;
108
109
0
  body->fun(body->arg);
110
0
  body->returned = HTS_TRUE;
111
0
}
112
113
/* Set by a worker a fault recovery cut short. A plain flag rather than a field
114
   on opt, because a worker must not touch opt at all: it outlives a timed-out
115
   resolve, and the mirror frees opt before the thread wait at exit. */
116
static volatile hts_boolean worker_faulted = HTS_FALSE;
117
/* Each mirror takes the next round, and a worker keeps the round it was spawned
118
   in, so one abandoned by an earlier mirror cannot abort this one. */
119
static volatile int mirror_round = 0;
120
121
0
hts_boolean hts_worker_faulted(void) { return worker_faulted; }
122
123
0
void hts_worker_fault_clear(void) {
124
0
  mirror_round++; /* first, so a straggler can no longer raise the flag */
125
0
  worker_faulted = HTS_FALSE;
126
0
}
127
128
/* Set once before any thread is spawned, hence unlocked. */
129
static void *(*thread_enter)(void) = NULL;
130
static void (*thread_leave)(void *cookie) = NULL;
131
static hts_thread_runner thread_runner = NULL;
132
133
HTSEXT_API void hts_set_thread_hooks(void *(*enter)(void),
134
0
                                     void (*leave)(void *cookie)) {
135
  /* Never half a pair: 'leave' must not see a cookie no 'enter' produced. */
136
0
  const int paired = enter != NULL && leave != NULL;
137
138
0
  thread_enter = paired ? enter : NULL;
139
0
  thread_leave = paired ? leave : NULL;
140
0
}
141
142
0
HTSEXT_API hts_thread_runner hts_set_thread_runner(hts_thread_runner runner) {
143
0
  const hts_thread_runner previous = thread_runner;
144
145
0
  thread_runner = runner;
146
0
  return previous;
147
0
}
148
149
#ifdef _WIN32
150
static unsigned int __stdcall hts_entry_point(void *tharg)
151
#else
152
static void *hts_entry_point(void *tharg)
153
#endif
154
0
{
155
0
  hts_thread_s *s_args = (hts_thread_s *) tharg;
156
0
  void *const arg = s_args->arg;
157
0
  void (*const tail)(void *arg) = s_args->tail;
158
0
  const int round = s_args->round;
159
0
  hts_body_s body;
160
0
  void *cookie;
161
162
0
  body.fun = s_args->fun;
163
0
  body.arg = arg;
164
0
  body.returned = HTS_FALSE;
165
0
  freet(tharg);
166
167
0
  cookie = thread_enter != NULL ? thread_enter() : NULL;
168
  /* run */
169
0
  if (thread_runner != NULL)
170
0
    thread_runner(hts_run_body, &body);
171
0
  else
172
0
    hts_run_body(&body);
173
  /* back_check_worker_fault() reads this and gives up the mirror. */
174
0
  if (!body.returned && round == mirror_round)
175
0
    worker_faulted = HTS_TRUE;
176
  /* Not at the end of the body, because a recovered fault never gets there. */
177
0
  if (tail != NULL)
178
0
    tail(arg);
179
0
  if (thread_leave != NULL)
180
0
    thread_leave(cookie);
181
182
0
  process_chain_add(-1);
183
#ifdef _WIN32
184
  return 0;
185
#else
186
0
  return NULL;
187
0
#endif
188
0
}
189
190
/* See httrack-library.h. */
191
0
HTSEXT_API int hts_newthread_checked(void (*fun)(void *arg), void *arg) {
192
0
  return hts_newthread_tail(fun, arg, NULL);
193
0
}
194
195
0
HTSEXT_API int hts_newthread(void (*fun)(void *arg), void *arg) {
196
0
  return hts_newthread_checked(fun, arg);
197
0
}
198
199
int hts_newthread_tail(void (*fun)(void *arg), void *arg,
200
0
                       void (*tail)(void *arg)) {
201
0
  hts_thread_s *s_args = malloct(sizeof(hts_thread_s));
202
203
0
  assertf(s_args != NULL);
204
0
  s_args->arg = arg;
205
0
  s_args->fun = fun;
206
0
  s_args->tail = tail;
207
0
  s_args->round = mirror_round;
208
0
  process_chain_add(1);
209
#ifdef _WIN32
210
  {
211
    unsigned int idt;
212
    HANDLE handle =
213
      (HANDLE) _beginthreadex(NULL, 0, hts_entry_point, s_args, 0, &idt);
214
    if (handle == 0) {
215
      process_chain_add(-1);
216
      freet(s_args);
217
      return -1;
218
    } else {
219
      /* detach the thread from the main process so that is can be independent */
220
      CloseHandle(handle);
221
    }
222
  }
223
#else
224
0
  {
225
0
    const size_t stackSize = 1024 * 1024 * 8;
226
0
    pthread_attr_t attr;
227
0
    pthread_t handle = 0;
228
0
    hts_boolean created;
229
230
    /* init kept apart: destroying an uninitialised attr is undefined (#772) */
231
0
    if (pthread_attr_init(&attr) == 0) {
232
0
      created = pthread_attr_setstacksize(&attr, stackSize) == 0 &&
233
0
                pthread_create(&handle, &attr, hts_entry_point, s_args) == 0;
234
0
      pthread_attr_destroy(&attr); /* create() copied what it needed */
235
0
    } else {
236
0
      created = HTS_FALSE;
237
0
    }
238
0
    if (!created) {
239
0
      process_chain_add(-1);
240
0
      freet(s_args);
241
0
      return -1;
242
0
    }
243
    /* detach the thread from the main process so that it can be independent */
244
0
    pthread_detach(handle);
245
0
  }
246
0
#endif
247
0
  return 0;
248
0
}
249
250
/* Note: new 3.41 cleaned up functions. */
251
252
9.65k
HTSEXT_API void hts_mutexinit(htsmutex * mutex) {
253
9.65k
  htsmutex_s *smutex = malloct(sizeof(htsmutex_s));
254
255
  /* void and exported, so abort: hts_mutexlock() asserts this anyway. */
256
9.65k
  assertf(smutex != NULL);
257
#ifdef _WIN32
258
  smutex->handle = CreateMutex(NULL, FALSE, NULL);
259
#else
260
0
  pthread_mutex_init(&smutex->handle, 0);
261
9.65k
#endif
262
9.65k
  *mutex = smutex;
263
9.65k
}
264
265
9.65k
HTSEXT_API void hts_mutexfree(htsmutex * mutex) {
266
9.65k
  if (mutex != NULL && *mutex != NULL) {
267
#ifdef _WIN32
268
    CloseHandle((*mutex)->handle);
269
#else
270
9.65k
    pthread_mutex_destroy(&((*mutex)->handle));
271
9.65k
#endif
272
9.65k
    freet(*mutex);
273
9.65k
    *mutex = NULL;
274
9.65k
  }
275
9.65k
}
276
277
29.8k
HTSEXT_API void hts_mutexlock(htsmutex * mutex) {
278
29.8k
  htsmutex lock;
279
280
29.8k
  assertf(mutex != NULL);
281
0
  lock = hts_load_acquire_mutex(mutex);
282
29.8k
  if (lock == HTSMUTEX_INIT) { /* must be initialized */
283
    /* Initialize exactly once, even when several threads race to lock the same
284
       mutex for the first time. Build our own object, then publish it with a
285
       single atomic compare-and-swap; the threads that lose the race free the
286
       object they built (issue #297). No static guard is needed, which keeps
287
       this safe on Windows 2000 (no statically-initializable lock there). */
288
2
    htsmutex created = HTSMUTEX_INIT;
289
2
    htsmutex published;
290
291
2
    hts_mutexinit(&created);
292
#ifdef _WIN32
293
    published = (htsmutex) InterlockedCompareExchangePointer(
294
        (PVOID volatile *) mutex, created, HTSMUTEX_INIT);
295
#else
296
2
    published = __sync_val_compare_and_swap(mutex, HTSMUTEX_INIT, created);
297
2
#endif
298
    /* Both forms are full barriers, so the loser's read of the winner's
299
       pointer is as ordered as the acquire above. */
300
2
    if (published != HTSMUTEX_INIT) {
301
0
      hts_mutexfree(&created);
302
0
      lock = published;
303
2
    } else {
304
2
      lock = created;
305
2
    }
306
2
  }
307
29.8k
  assertf(lock != NULL);
308
#ifdef _WIN32
309
  assertf(lock->handle != NULL);
310
  WaitForSingleObject(lock->handle, INFINITE);
311
#else
312
0
  pthread_mutex_lock(&lock->handle);
313
29.8k
#endif
314
29.8k
}
315
316
29.8k
HTSEXT_API void hts_mutexrelease(htsmutex * mutex) {
317
29.8k
  htsmutex lock;
318
319
29.8k
  assertf(mutex != NULL);
320
  /* Acquire here too: the compare-and-swap writes the slot whether it wins or
321
     loses, so a loser is still writing it while the winner unlocks. */
322
0
  lock = hts_load_acquire_mutex(mutex);
323
29.8k
  assertf(lock != NULL);
324
#ifdef _WIN32
325
  assertf(lock->handle != NULL);
326
  ReleaseMutex(lock->handle);
327
#else
328
0
  pthread_mutex_unlock(&lock->handle);
329
29.8k
#endif
330
29.8k
}