curl_threads.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, 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 https://curl.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include <curl/curl.h>
  26. #ifdef USE_THREADS_POSIX
  27. # ifdef HAVE_PTHREAD_H
  28. # include <pthread.h>
  29. # endif
  30. #elif defined(USE_THREADS_WIN32)
  31. # include <process.h>
  32. #endif
  33. #include "curl_threads.h"
  34. #include "curl_memory.h"
  35. /* The last #include FILE should be: */
  36. #include "memdebug.h"
  37. #ifdef USE_THREADS_POSIX
  38. struct Curl_actual_call {
  39. unsigned int (*func)(void *);
  40. void *arg;
  41. };
  42. static void *curl_thread_create_thunk(void *arg)
  43. {
  44. struct Curl_actual_call *ac = arg;
  45. unsigned int (*func)(void *) = ac->func;
  46. void *real_arg = ac->arg;
  47. free(ac);
  48. (*func)(real_arg);
  49. return 0;
  50. }
  51. curl_thread_t Curl_thread_create(CURL_THREAD_RETURN_T
  52. (CURL_STDCALL *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. free(t);
  65. free(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. *hnd = curl_thread_t_null;
  74. }
  75. }
  76. int Curl_thread_join(curl_thread_t *hnd)
  77. {
  78. int ret = (pthread_join(**hnd, NULL) == 0);
  79. free(*hnd);
  80. *hnd = curl_thread_t_null;
  81. return ret;
  82. }
  83. /* do not use pthread_cancel if:
  84. * - pthread_cancel seems to be absent
  85. * - on FreeBSD, as we see hangers in CI testing
  86. * - this is a -fsanitize=thread build
  87. * (clang sanitizer reports false positive when functions to not return)
  88. */
  89. #if defined(PTHREAD_CANCEL_ENABLE) && !defined(__FreeBSD__)
  90. #if defined(__has_feature)
  91. # if !__has_feature(thread_sanitizer)
  92. #define USE_PTHREAD_CANCEL
  93. # endif
  94. #else /* __has_feature */
  95. #define USE_PTHREAD_CANCEL
  96. #endif /* !__has_feature */
  97. #endif /* PTHREAD_CANCEL_ENABLE && !__FreeBSD__ */
  98. int Curl_thread_cancel(curl_thread_t *hnd)
  99. {
  100. (void)hnd;
  101. if(*hnd != curl_thread_t_null)
  102. #ifdef USE_PTHREAD_CANCEL
  103. return pthread_cancel(**hnd);
  104. #else
  105. return 1; /* not supported */
  106. #endif
  107. return 0;
  108. }
  109. #elif defined(USE_THREADS_WIN32)
  110. curl_thread_t Curl_thread_create(CURL_THREAD_RETURN_T
  111. (CURL_STDCALL *func) (void *), void *arg)
  112. {
  113. #if defined(CURL_WINDOWS_UWP) || defined(UNDER_CE)
  114. typedef HANDLE curl_win_thread_handle_t;
  115. #else
  116. typedef uintptr_t curl_win_thread_handle_t;
  117. #endif
  118. curl_thread_t t;
  119. curl_win_thread_handle_t thread_handle;
  120. #if defined(CURL_WINDOWS_UWP) || defined(UNDER_CE)
  121. thread_handle = CreateThread(NULL, 0, func, arg, 0, NULL);
  122. #else
  123. thread_handle = _beginthreadex(NULL, 0, func, arg, 0, NULL);
  124. #endif
  125. t = (curl_thread_t)thread_handle;
  126. if((t == 0) || (t == LongToHandle(-1L))) {
  127. #ifdef UNDER_CE
  128. DWORD gle = GetLastError();
  129. /* !checksrc! disable ERRNOVAR 1 */
  130. int err = (gle == ERROR_ACCESS_DENIED ||
  131. gle == ERROR_NOT_ENOUGH_MEMORY) ?
  132. EACCES : EINVAL;
  133. CURL_SETERRNO(err);
  134. #endif
  135. return curl_thread_t_null;
  136. }
  137. return t;
  138. }
  139. void Curl_thread_destroy(curl_thread_t *hnd)
  140. {
  141. if(*hnd != curl_thread_t_null) {
  142. CloseHandle(*hnd);
  143. *hnd = curl_thread_t_null;
  144. }
  145. }
  146. int Curl_thread_join(curl_thread_t *hnd)
  147. {
  148. #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < _WIN32_WINNT_VISTA)
  149. int ret = (WaitForSingleObject(*hnd, INFINITE) == WAIT_OBJECT_0);
  150. #else
  151. int ret = (WaitForSingleObjectEx(*hnd, INFINITE, FALSE) == WAIT_OBJECT_0);
  152. #endif
  153. Curl_thread_destroy(hnd);
  154. return ret;
  155. }
  156. int Curl_thread_cancel(curl_thread_t *hnd)
  157. {
  158. if(*hnd != curl_thread_t_null) {
  159. return 1; /* not supported */
  160. }
  161. return 0;
  162. }
  163. #endif /* USE_THREADS_* */