curl_threads.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
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 /* The last #include file should be: */
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 /* !checksrc! disable SPACEBEFOREPAREN 1 */
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 /* USE_THREADS_* */


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:02