Coverage Report

Created: 2025-07-23 06:58

/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
#if defined(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
#if defined(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
#elif defined(USE_THREADS_WIN32)
104
105
curl_thread_t Curl_thread_create(CURL_THREAD_RETURN_T
106
                                 (CURL_STDCALL *func) (void *), void *arg)
107
{
108
#if defined(CURL_WINDOWS_UWP) || defined(UNDER_CE)
109
  typedef HANDLE curl_win_thread_handle_t;
110
#else
111
  typedef uintptr_t curl_win_thread_handle_t;
112
#endif
113
  curl_thread_t t;
114
  curl_win_thread_handle_t thread_handle;
115
#if defined(CURL_WINDOWS_UWP) || defined(UNDER_CE)
116
  thread_handle = CreateThread(NULL, 0, func, arg, 0, NULL);
117
#else
118
  thread_handle = _beginthreadex(NULL, 0, func, arg, 0, NULL);
119
#endif
120
  t = (curl_thread_t)thread_handle;
121
  if((t == 0) || (t == LongToHandle(-1L))) {
122
#ifdef UNDER_CE
123
    DWORD gle = GetLastError();
124
    /* !checksrc! disable ERRNOVAR 1 */
125
    int err = (gle == ERROR_ACCESS_DENIED ||
126
               gle == ERROR_NOT_ENOUGH_MEMORY) ?
127
               EACCES : EINVAL;
128
    CURL_SETERRNO(err);
129
#endif
130
    return curl_thread_t_null;
131
  }
132
  return t;
133
}
134
135
void Curl_thread_destroy(curl_thread_t *hnd)
136
{
137
  if(*hnd != curl_thread_t_null) {
138
    CloseHandle(*hnd);
139
    *hnd = curl_thread_t_null;
140
  }
141
}
142
143
int Curl_thread_join(curl_thread_t *hnd)
144
{
145
#if !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_VISTA) || \
146
    (_WIN32_WINNT < _WIN32_WINNT_VISTA)
147
  int ret = (WaitForSingleObject(*hnd, INFINITE) == WAIT_OBJECT_0);
148
#else
149
  int ret = (WaitForSingleObjectEx(*hnd, INFINITE, FALSE) == WAIT_OBJECT_0);
150
#endif
151
152
  Curl_thread_destroy(hnd);
153
154
  return ret;
155
}
156
157
#endif /* USE_THREADS_* */