2
0

curl_threads.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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 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. #if defined(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. #ifdef BUILDING_LIBCURL
  35. #include "curl_memory.h"
  36. #endif
  37. /* The last #include file should be: */
  38. #include "memdebug.h"
  39. #if defined(USE_THREADS_POSIX)
  40. struct Curl_actual_call {
  41. unsigned int (*func)(void *);
  42. void *arg;
  43. };
  44. static void *curl_thread_create_thunk(void *arg)
  45. {
  46. struct Curl_actual_call *ac = arg;
  47. unsigned int (*func)(void *) = ac->func;
  48. void *real_arg = ac->arg;
  49. free(ac);
  50. (*func)(real_arg);
  51. return 0;
  52. }
  53. curl_thread_t Curl_thread_create(unsigned int (*func) (void *), void *arg)
  54. {
  55. curl_thread_t t = malloc(sizeof(pthread_t));
  56. struct Curl_actual_call *ac = malloc(sizeof(struct Curl_actual_call));
  57. if(!(ac && t))
  58. goto err;
  59. ac->func = func;
  60. ac->arg = arg;
  61. if(pthread_create(t, NULL, curl_thread_create_thunk, ac) != 0)
  62. goto err;
  63. return t;
  64. err:
  65. free(t);
  66. free(ac);
  67. return curl_thread_t_null;
  68. }
  69. void Curl_thread_destroy(curl_thread_t hnd)
  70. {
  71. if(hnd != curl_thread_t_null) {
  72. pthread_detach(*hnd);
  73. free(hnd);
  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. #elif defined(USE_THREADS_WIN32)
  84. curl_thread_t Curl_thread_create(
  85. #if defined(_WIN32_WCE) || defined(CURL_WINDOWS_UWP)
  86. DWORD
  87. #else
  88. unsigned int
  89. #endif
  90. (CURL_STDCALL *func) (void *),
  91. void *arg)
  92. {
  93. #if defined(_WIN32_WCE) || defined(CURL_WINDOWS_UWP)
  94. typedef HANDLE curl_win_thread_handle_t;
  95. #else
  96. typedef uintptr_t curl_win_thread_handle_t;
  97. #endif
  98. curl_thread_t t;
  99. curl_win_thread_handle_t thread_handle;
  100. #if defined(_WIN32_WCE) || defined(CURL_WINDOWS_UWP)
  101. thread_handle = CreateThread(NULL, 0, func, arg, 0, NULL);
  102. #else
  103. thread_handle = _beginthreadex(NULL, 0, func, arg, 0, NULL);
  104. #endif
  105. t = (curl_thread_t)thread_handle;
  106. if((t == 0) || (t == LongToHandle(-1L))) {
  107. #ifdef _WIN32_WCE
  108. DWORD gle = GetLastError();
  109. errno = ((gle == ERROR_ACCESS_DENIED ||
  110. gle == ERROR_NOT_ENOUGH_MEMORY) ?
  111. EACCES : EINVAL);
  112. #endif
  113. return curl_thread_t_null;
  114. }
  115. return t;
  116. }
  117. void Curl_thread_destroy(curl_thread_t hnd)
  118. {
  119. if(hnd != curl_thread_t_null)
  120. CloseHandle(hnd);
  121. }
  122. int Curl_thread_join(curl_thread_t *hnd)
  123. {
  124. #if !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_VISTA) || \
  125. (_WIN32_WINNT < _WIN32_WINNT_VISTA)
  126. int ret = (WaitForSingleObject(*hnd, INFINITE) == WAIT_OBJECT_0);
  127. #else
  128. int ret = (WaitForSingleObjectEx(*hnd, INFINITE, FALSE) == WAIT_OBJECT_0);
  129. #endif
  130. Curl_thread_destroy(*hnd);
  131. *hnd = curl_thread_t_null;
  132. return ret;
  133. }
  134. #endif /* USE_THREADS_* */