curl_threads.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, 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 http://curl.haxx.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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #if defined(USE_THREADS_POSIX)
  24. # ifdef HAVE_PTHREAD_H
  25. # include <pthread.h>
  26. # endif
  27. #elif defined(USE_THREADS_WIN32)
  28. # ifdef HAVE_PROCESS_H
  29. # include <process.h>
  30. # endif
  31. #endif
  32. #include "curl_threads.h"
  33. #define _MPRINTF_REPLACE /* use our functions only */
  34. #include <curl/mprintf.h>
  35. #include "curl_memory.h"
  36. /* The last #include file should be: */
  37. #include "memdebug.h"
  38. #if defined(USE_THREADS_POSIX)
  39. struct curl_actual_call {
  40. unsigned int (*func)(void *);
  41. void *arg;
  42. };
  43. static void *curl_thread_create_thunk(void *arg)
  44. {
  45. struct curl_actual_call * ac = arg;
  46. unsigned int (*func)(void *) = ac->func;
  47. void *real_arg = ac->arg;
  48. free(ac);
  49. (*func)(real_arg);
  50. return 0;
  51. }
  52. curl_thread_t Curl_thread_create(unsigned int (*func) (void*), void *arg)
  53. {
  54. curl_thread_t t = malloc(sizeof(pthread_t));
  55. struct curl_actual_call *ac = malloc(sizeof(struct curl_actual_call));
  56. if(!(ac && t))
  57. goto err;
  58. ac->func = func;
  59. ac->arg = arg;
  60. if(pthread_create(t, NULL, curl_thread_create_thunk, ac) != 0)
  61. goto err;
  62. return t;
  63. err:
  64. Curl_safefree(t);
  65. Curl_safefree(ac);
  66. return curl_thread_t_null;
  67. }
  68. void Curl_thread_destroy(curl_thread_t hnd)
  69. {
  70. if(hnd != curl_thread_t_null) {
  71. pthread_detach(*hnd);
  72. free(hnd);
  73. }
  74. }
  75. int Curl_thread_join(curl_thread_t *hnd)
  76. {
  77. int ret = (pthread_join(**hnd, NULL) == 0);
  78. free(*hnd);
  79. *hnd = curl_thread_t_null;
  80. return ret;
  81. }
  82. #elif defined(USE_THREADS_WIN32)
  83. curl_thread_t Curl_thread_create(unsigned int (CURL_STDCALL *func) (void*),
  84. void *arg)
  85. {
  86. #ifdef _WIN32_WCE
  87. return CreateThread(NULL, 0, func, arg, 0, NULL);
  88. #else
  89. curl_thread_t t;
  90. t = (curl_thread_t)_beginthreadex(NULL, 0, func, arg, 0, NULL);
  91. if((t == 0) || (t == (curl_thread_t)-1L))
  92. return curl_thread_t_null;
  93. return t;
  94. #endif
  95. }
  96. void Curl_thread_destroy(curl_thread_t hnd)
  97. {
  98. CloseHandle(hnd);
  99. }
  100. int Curl_thread_join(curl_thread_t *hnd)
  101. {
  102. #if !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_VISTA) || \
  103. (_WIN32_WINNT < _WIN32_WINNT_VISTA)
  104. int ret = (WaitForSingleObject(*hnd, INFINITE) == WAIT_OBJECT_0);
  105. #else
  106. int ret = (WaitForSingleObjectEx(*hnd, INFINITE, FALSE) == WAIT_OBJECT_0);
  107. #endif
  108. Curl_thread_destroy(*hnd);
  109. *hnd = curl_thread_t_null;
  110. return ret;
  111. }
  112. #endif /* USE_THREADS_* */