1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, 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 http://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 #if defined(USE_THREADS_POSIX)
26 # ifdef HAVE_PTHREAD_H
29 #elif defined(USE_THREADS_WIN32)
30 # ifdef HAVE_PROCESS_H
35 #include "curl_threads.h"
37 #define _MPRINTF_REPLACE /* use our functions only */
38 #include <curl/mprintf.h>
40 #include "curl_memory.h"
41 /* The last #include file should be: */
44 #if defined(USE_THREADS_POSIX)
46 struct curl_actual_call {
47 unsigned int (*func)(void *);
51 static void *curl_thread_create_thunk(void *arg)
53 struct curl_actual_call * ac = arg;
54 unsigned int (*func)(void *) = ac->func;
55 void *real_arg = ac->arg;
64 curl_thread_t Curl_thread_create(unsigned int (*func) (void*), void *arg)
66 curl_thread_t t = malloc(sizeof(pthread_t));
67 struct curl_actual_call *ac = malloc(sizeof(struct curl_actual_call));
74 if(pthread_create(t, NULL, curl_thread_create_thunk, ac) != 0)
82 return curl_thread_t_null;
85 void Curl_thread_destroy(curl_thread_t hnd)
87 if(hnd != curl_thread_t_null) {
93 int Curl_thread_join(curl_thread_t *hnd)
95 int ret = (pthread_join(**hnd, NULL) == 0);
98 *hnd = curl_thread_t_null;
103 #elif defined(USE_THREADS_WIN32)
105 curl_thread_t Curl_thread_create(unsigned int (CURL_STDCALL *func) (void*),
109 return CreateThread(NULL, 0, func, arg, 0, NULL);
112 t = (curl_thread_t)_beginthreadex(NULL, 0, func, arg, 0, NULL);
113 if((t == 0) || (t == (curl_thread_t)-1L))
114 return curl_thread_t_null;
119 void Curl_thread_destroy(curl_thread_t hnd)
124 int Curl_thread_join(curl_thread_t *hnd)
126 #if !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_VISTA) || \
127 (_WIN32_WINNT < _WIN32_WINNT_VISTA)
128 int ret = (WaitForSingleObject(*hnd, INFINITE) == WAIT_OBJECT_0);
130 int ret = (WaitForSingleObjectEx(*hnd, INFINITE, FALSE) == WAIT_OBJECT_0);
133 Curl_thread_destroy(*hnd);
135 *hnd = curl_thread_t_null;
140 #endif /* USE_THREADS_* */