1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #include <curl/curl.h>
27 #if defined(USE_THREADS_POSIX)
28 # ifdef HAVE_PTHREAD_H
31 #elif defined(USE_THREADS_WIN32)
32 # ifdef HAVE_PROCESS_H
37 #include "curl_threads.h"
38 #include "curl_memory.h"
39 /* The last #include file should be: */
42 #if defined(USE_THREADS_POSIX)
44 struct curl_actual_call {
45 unsigned int (*func)(void *);
49 static void *curl_thread_create_thunk(void *arg)
51 struct curl_actual_call * ac = arg;
52 unsigned int (*func)(void *) = ac->func;
53 void *real_arg = ac->arg;
62 curl_thread_t Curl_thread_create(unsigned int (*func) (void*), void *arg)
64 curl_thread_t t = malloc(sizeof(pthread_t));
65 struct curl_actual_call *ac = malloc(sizeof(struct curl_actual_call));
72 if(pthread_create(t, NULL, curl_thread_create_thunk, ac) != 0)
80 return curl_thread_t_null;
83 void Curl_thread_destroy(curl_thread_t hnd)
85 if(hnd != curl_thread_t_null) {
91 int Curl_thread_join(curl_thread_t *hnd)
93 int ret = (pthread_join(**hnd, NULL) == 0);
96 *hnd = curl_thread_t_null;
101 #elif defined(USE_THREADS_WIN32)
103 curl_thread_t Curl_thread_create(unsigned int (CURL_STDCALL *func) (void*),
107 return CreateThread(NULL, 0, func, arg, 0, NULL);
110 t = (curl_thread_t)_beginthreadex(NULL, 0, func, arg, 0, NULL);
111 if((t == 0) || (t == (curl_thread_t)-1L))
112 return curl_thread_t_null;
117 void Curl_thread_destroy(curl_thread_t hnd)
122 int Curl_thread_join(curl_thread_t *hnd)
124 #if !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_VISTA) || \
125 (_WIN32_WINNT < _WIN32_WINNT_VISTA)
126 int ret = (WaitForSingleObject(*hnd, INFINITE) == WAIT_OBJECT_0);
128 int ret = (WaitForSingleObjectEx(*hnd, INFINITE, FALSE) == WAIT_OBJECT_0);
131 Curl_thread_destroy(*hnd);
133 *hnd = curl_thread_t_null;
138 #endif /* USE_THREADS_* */