Coverage Report

Created: 2025-08-26 07:08

/src/PROJ/curl/lib/curl_threads.c
Line
Count
Source (jump to first uncovered line)
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
25
#include "curl_setup.h"
26
27
#include <curl/curl.h>
28
29
#ifdef USE_THREADS_POSIX
30
#  ifdef HAVE_PTHREAD_H
31
#    include <pthread.h>
32
#  endif
33
#elif defined(USE_THREADS_WIN32)
34
#  include <process.h>
35
#endif
36
37
#include "curl_threads.h"
38
#include "curl_memory.h"
39
/* The last #include FILE should be: */
40
#include "memdebug.h"
41
42
#ifdef USE_THREADS_POSIX
43
44
struct Curl_actual_call {
45
  unsigned int (*func)(void *);
46
  void *arg;
47
};
48
49
static void *curl_thread_create_thunk(void *arg)
50
0
{
51
0
  struct Curl_actual_call *ac = arg;
52
0
  unsigned int (*func)(void *) = ac->func;
53
0
  void *real_arg = ac->arg;
54
55
0
  free(ac);
56
57
0
  (*func)(real_arg);
58
59
0
  return 0;
60
0
}
61
62
curl_thread_t Curl_thread_create(CURL_THREAD_RETURN_T
63
                                 (CURL_STDCALL *func) (void *), void *arg)
64
0
{
65
0
  curl_thread_t t = malloc(sizeof(pthread_t));
66
0
  struct Curl_actual_call *ac = malloc(sizeof(struct Curl_actual_call));
67
0
  if(!(ac && t))
68
0
    goto err;
69
70
0
  ac->func = func;
71
0
  ac->arg = arg;
72
73
0
  if(pthread_create(t, NULL, curl_thread_create_thunk, ac) != 0)
74
0
    goto err;
75
76
0
  return t;
77
78
0
err:
79
0
  free(t);
80
0
  free(ac);
81
0
  return curl_thread_t_null;
82
0
}
83
84
void Curl_thread_destroy(curl_thread_t *hnd)
85
0
{
86
0
  if(*hnd != curl_thread_t_null) {
87
0
    pthread_detach(**hnd);
88
0
    free(*hnd);
89
0
    *hnd = curl_thread_t_null;
90
0
  }
91
0
}
92
93
int Curl_thread_join(curl_thread_t *hnd)
94
0
{
95
0
  int ret = (pthread_join(**hnd, NULL) == 0);
96
97
0
  free(*hnd);
98
0
  *hnd = curl_thread_t_null;
99
100
0
  return ret;
101
0
}
102
103
/* do not use pthread_cancel if:
104
 * - pthread_cancel seems to be absent
105
 * - on FreeBSD, as we see hangers in CI testing
106
 * - this is a -fsanitize=thread build
107
 *   (clang sanitizer reports false positive when functions to not return)
108
 */
109
#if defined(PTHREAD_CANCEL_ENABLE) && !defined(__FreeBSD__)
110
#if defined(__has_feature)
111
#  if !__has_feature(thread_sanitizer)
112
#define USE_PTHREAD_CANCEL
113
#  endif
114
#else /* __has_feature */
115
#define USE_PTHREAD_CANCEL
116
#endif /* !__has_feature */
117
#endif /* PTHREAD_CANCEL_ENABLE && !__FreeBSD__ */
118
119
int Curl_thread_cancel(curl_thread_t *hnd)
120
0
{
121
0
  (void)hnd;
122
0
  if(*hnd != curl_thread_t_null)
123
0
#ifdef USE_PTHREAD_CANCEL
124
0
    return pthread_cancel(**hnd);
125
#else
126
    return 1; /* not supported */
127
#endif
128
0
  return 0;
129
0
}
130
131
#elif defined(USE_THREADS_WIN32)
132
133
curl_thread_t Curl_thread_create(CURL_THREAD_RETURN_T
134
                                 (CURL_STDCALL *func) (void *), void *arg)
135
{
136
#if defined(CURL_WINDOWS_UWP) || defined(UNDER_CE)
137
  typedef HANDLE curl_win_thread_handle_t;
138
#else
139
  typedef uintptr_t curl_win_thread_handle_t;
140
#endif
141
  curl_thread_t t;
142
  curl_win_thread_handle_t thread_handle;
143
#if defined(CURL_WINDOWS_UWP) || defined(UNDER_CE)
144
  thread_handle = CreateThread(NULL, 0, func, arg, 0, NULL);
145
#else
146
  thread_handle = _beginthreadex(NULL, 0, func, arg, 0, NULL);
147
#endif
148
  t = (curl_thread_t)thread_handle;
149
  if((t == 0) || (t == LongToHandle(-1L))) {
150
#ifdef UNDER_CE
151
    DWORD gle = GetLastError();
152
    /* !checksrc! disable ERRNOVAR 1 */
153
    int err = (gle == ERROR_ACCESS_DENIED ||
154
               gle == ERROR_NOT_ENOUGH_MEMORY) ?
155
               EACCES : EINVAL;
156
    CURL_SETERRNO(err);
157
#endif
158
    return curl_thread_t_null;
159
  }
160
  return t;
161
}
162
163
void Curl_thread_destroy(curl_thread_t *hnd)
164
{
165
  if(*hnd != curl_thread_t_null) {
166
    CloseHandle(*hnd);
167
    *hnd = curl_thread_t_null;
168
  }
169
}
170
171
int Curl_thread_join(curl_thread_t *hnd)
172
{
173
#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < _WIN32_WINNT_VISTA)
174
  int ret = (WaitForSingleObject(*hnd, INFINITE) == WAIT_OBJECT_0);
175
#else
176
  int ret = (WaitForSingleObjectEx(*hnd, INFINITE, FALSE) == WAIT_OBJECT_0);
177
#endif
178
179
  Curl_thread_destroy(hnd);
180
181
182
  return ret;
183
}
184
185
int Curl_thread_cancel(curl_thread_t *hnd)
186
{
187
  if(*hnd != curl_thread_t_null) {
188
    return 1; /* not supported */
189
  }
190
  return 0;
191
}
192
193
#endif /* USE_THREADS_* */