/src/curl/lib/curl_threads.c
Line | Count | Source (jump to first uncovered line) |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) 1998 - 2022, 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 | | # ifdef HAVE_PROCESS_H |
35 | | # include <process.h> |
36 | | # endif |
37 | | #endif |
38 | | |
39 | | #include "curl_threads.h" |
40 | | #include "curl_memory.h" |
41 | | /* The last #include file should be: */ |
42 | | #include "memdebug.h" |
43 | | |
44 | | #if defined(USE_THREADS_POSIX) |
45 | | |
46 | | struct Curl_actual_call { |
47 | | unsigned int (*func)(void *); |
48 | | void *arg; |
49 | | }; |
50 | | |
51 | | static void *curl_thread_create_thunk(void *arg) |
52 | 22 | { |
53 | 22 | struct Curl_actual_call *ac = arg; |
54 | 22 | unsigned int (*func)(void *) = ac->func; |
55 | 22 | void *real_arg = ac->arg; |
56 | | |
57 | 22 | free(ac); |
58 | | |
59 | 22 | (*func)(real_arg); |
60 | | |
61 | 22 | return 0; |
62 | 22 | } |
63 | | |
64 | | curl_thread_t Curl_thread_create(unsigned int (*func) (void *), void *arg) |
65 | 22 | { |
66 | 22 | curl_thread_t t = malloc(sizeof(pthread_t)); |
67 | 22 | struct Curl_actual_call *ac = malloc(sizeof(struct Curl_actual_call)); |
68 | 22 | if(!(ac && t)) |
69 | 0 | goto err; |
70 | | |
71 | 22 | ac->func = func; |
72 | 22 | ac->arg = arg; |
73 | | |
74 | 22 | if(pthread_create(t, NULL, curl_thread_create_thunk, ac) != 0) |
75 | 0 | goto err; |
76 | | |
77 | 22 | return t; |
78 | | |
79 | 0 | err: |
80 | 0 | free(t); |
81 | 0 | free(ac); |
82 | 0 | return curl_thread_t_null; |
83 | 22 | } |
84 | | |
85 | | void Curl_thread_destroy(curl_thread_t hnd) |
86 | 0 | { |
87 | 0 | if(hnd != curl_thread_t_null) { |
88 | 0 | pthread_detach(*hnd); |
89 | 0 | free(hnd); |
90 | 0 | } |
91 | 0 | } |
92 | | |
93 | | int Curl_thread_join(curl_thread_t *hnd) |
94 | 22 | { |
95 | 22 | int ret = (pthread_join(**hnd, NULL) == 0); |
96 | | |
97 | 22 | free(*hnd); |
98 | 22 | *hnd = curl_thread_t_null; |
99 | | |
100 | 22 | return ret; |
101 | 22 | } |
102 | | |
103 | | #elif defined(USE_THREADS_WIN32) |
104 | | |
105 | | /* !checksrc! disable SPACEBEFOREPAREN 1 */ |
106 | | curl_thread_t Curl_thread_create(unsigned int (CURL_STDCALL *func) (void *), |
107 | | void *arg) |
108 | | { |
109 | | #ifdef _WIN32_WCE |
110 | | typedef HANDLE curl_win_thread_handle_t; |
111 | | #elif defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) |
112 | | typedef unsigned long curl_win_thread_handle_t; |
113 | | #else |
114 | | typedef uintptr_t curl_win_thread_handle_t; |
115 | | #endif |
116 | | curl_thread_t t; |
117 | | curl_win_thread_handle_t thread_handle; |
118 | | #ifdef _WIN32_WCE |
119 | | thread_handle = CreateThread(NULL, 0, func, arg, 0, NULL); |
120 | | #else |
121 | | thread_handle = _beginthreadex(NULL, 0, func, arg, 0, NULL); |
122 | | #endif |
123 | | t = (curl_thread_t)thread_handle; |
124 | | if((t == 0) || (t == LongToHandle(-1L))) { |
125 | | #ifdef _WIN32_WCE |
126 | | DWORD gle = GetLastError(); |
127 | | errno = ((gle == ERROR_ACCESS_DENIED || |
128 | | gle == ERROR_NOT_ENOUGH_MEMORY) ? |
129 | | EACCES : EINVAL); |
130 | | #endif |
131 | | return curl_thread_t_null; |
132 | | } |
133 | | return t; |
134 | | } |
135 | | |
136 | | void Curl_thread_destroy(curl_thread_t hnd) |
137 | | { |
138 | | CloseHandle(hnd); |
139 | | } |
140 | | |
141 | | int Curl_thread_join(curl_thread_t *hnd) |
142 | | { |
143 | | #if !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_VISTA) || \ |
144 | | (_WIN32_WINNT < _WIN32_WINNT_VISTA) |
145 | | int ret = (WaitForSingleObject(*hnd, INFINITE) == WAIT_OBJECT_0); |
146 | | #else |
147 | | int ret = (WaitForSingleObjectEx(*hnd, INFINITE, FALSE) == WAIT_OBJECT_0); |
148 | | #endif |
149 | | |
150 | | Curl_thread_destroy(*hnd); |
151 | | |
152 | | *hnd = curl_thread_t_null; |
153 | | |
154 | | return ret; |
155 | | } |
156 | | |
157 | | #endif /* USE_THREADS_* */ |