Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "curl_setup.h"
00024
00025 #include <curl/curl.h>
00026
00027 #if defined(USE_THREADS_POSIX)
00028 # ifdef HAVE_PTHREAD_H
00029 # include <pthread.h>
00030 # endif
00031 #elif defined(USE_THREADS_WIN32)
00032 # ifdef HAVE_PROCESS_H
00033 # include <process.h>
00034 # endif
00035 #endif
00036
00037 #include "curl_threads.h"
00038 #include "curl_memory.h"
00039
00040 #include "memdebug.h"
00041
00042 #if defined(USE_THREADS_POSIX)
00043
00044 struct curl_actual_call {
00045 unsigned int (*func)(void *);
00046 void *arg;
00047 };
00048
00049 static void *curl_thread_create_thunk(void *arg)
00050 {
00051 struct curl_actual_call * ac = arg;
00052 unsigned int (*func)(void *) = ac->func;
00053 void *real_arg = ac->arg;
00054
00055 free(ac);
00056
00057 (*func)(real_arg);
00058
00059 return 0;
00060 }
00061
00062 curl_thread_t Curl_thread_create(unsigned int (*func) (void *), void *arg)
00063 {
00064 curl_thread_t t = malloc(sizeof(pthread_t));
00065 struct curl_actual_call *ac = malloc(sizeof(struct curl_actual_call));
00066 if(!(ac && t))
00067 goto err;
00068
00069 ac->func = func;
00070 ac->arg = arg;
00071
00072 if(pthread_create(t, NULL, curl_thread_create_thunk, ac) != 0)
00073 goto err;
00074
00075 return t;
00076
00077 err:
00078 free(t);
00079 free(ac);
00080 return curl_thread_t_null;
00081 }
00082
00083 void Curl_thread_destroy(curl_thread_t hnd)
00084 {
00085 if(hnd != curl_thread_t_null) {
00086 pthread_detach(*hnd);
00087 free(hnd);
00088 }
00089 }
00090
00091 int Curl_thread_join(curl_thread_t *hnd)
00092 {
00093 int ret = (pthread_join(**hnd, NULL) == 0);
00094
00095 free(*hnd);
00096 *hnd = curl_thread_t_null;
00097
00098 return ret;
00099 }
00100
00101 #elif defined(USE_THREADS_WIN32)
00102
00103
00104 curl_thread_t Curl_thread_create(unsigned int (CURL_STDCALL *func) (void *),
00105 void *arg)
00106 {
00107 #ifdef _WIN32_WCE
00108 return CreateThread(NULL, 0, func, arg, 0, NULL);
00109 #else
00110 curl_thread_t t;
00111 t = (curl_thread_t)_beginthreadex(NULL, 0, func, arg, 0, NULL);
00112 if((t == 0) || (t == (curl_thread_t)-1L))
00113 return curl_thread_t_null;
00114 return t;
00115 #endif
00116 }
00117
00118 void Curl_thread_destroy(curl_thread_t hnd)
00119 {
00120 CloseHandle(hnd);
00121 }
00122
00123 int Curl_thread_join(curl_thread_t *hnd)
00124 {
00125 #if !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_VISTA) || \
00126 (_WIN32_WINNT < _WIN32_WINNT_VISTA)
00127 int ret = (WaitForSingleObject(*hnd, INFINITE) == WAIT_OBJECT_0);
00128 #else
00129 int ret = (WaitForSingleObjectEx(*hnd, INFINITE, FALSE) == WAIT_OBJECT_0);
00130 #endif
00131
00132 Curl_thread_destroy(*hnd);
00133
00134 *hnd = curl_thread_t_null;
00135
00136 return ret;
00137 }
00138
00139 #endif