2
0

curl_threads.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #include "curl_memory.h"
  35. /* The last #include file should be: */
  36. #include "memdebug.h"
  37. #if defined(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(unsigned int (*func) (void *), void *arg)
  52. {
  53. curl_thread_t t = malloc(sizeof(pthread_t));
  54. struct Curl_actual_call *ac = malloc(sizeof(struct Curl_actual_call));
  55. if(!(ac && t))
  56. goto err;
  57. ac->func = func;
  58. ac->arg = arg;
  59. if(pthread_create(t, NULL, curl_thread_create_thunk, ac) != 0)
  60. goto err;
  61. return t;
  62. err:
  63. free(t);
  64. free(ac);
  65. return curl_thread_t_null;
  66. }
  67. void Curl_thread_destroy(curl_thread_t hnd)
  68. {
  69. if(hnd != curl_thread_t_null) {
  70. pthread_detach(*hnd);
  71. free(hnd);
  72. }
  73. }
  74. int Curl_thread_join(curl_thread_t *hnd)
  75. {
  76. int ret = (pthread_join(**hnd, NULL) == 0);
  77. free(*hnd);
  78. *hnd = curl_thread_t_null;
  79. return ret;
  80. }
  81. #elif defined(USE_THREADS_WIN32)
  82. curl_thread_t Curl_thread_create(
  83. #if defined(_WIN32_WCE) || defined(CURL_WINDOWS_APP)
  84. DWORD
  85. #else
  86. unsigned int
  87. #endif
  88. (CURL_STDCALL *func) (void *),
  89. void *arg)
  90. {
  91. #if defined(_WIN32_WCE) || defined(CURL_WINDOWS_APP)
  92. typedef HANDLE curl_win_thread_handle_t;
  93. #else
  94. typedef uintptr_t curl_win_thread_handle_t;
  95. #endif
  96. curl_thread_t t;
  97. curl_win_thread_handle_t thread_handle;
  98. #if defined(_WIN32_WCE) || defined(CURL_WINDOWS_APP)
  99. thread_handle = CreateThread(NULL, 0, func, arg, 0, NULL);
  100. #else
  101. thread_handle = _beginthreadex(NULL, 0, func, arg, 0, NULL);
  102. #endif
  103. t = (curl_thread_t)thread_handle;
  104. if((t == 0) || (t == LongToHandle(-1L))) {
  105. #ifdef _WIN32_WCE
  106. DWORD gle = GetLastError();
  107. errno = ((gle == ERROR_ACCESS_DENIED ||
  108. gle == ERROR_NOT_ENOUGH_MEMORY) ?
  109. EACCES : EINVAL);
  110. #endif
  111. return curl_thread_t_null;
  112. }
  113. return t;
  114. }
  115. void Curl_thread_destroy(curl_thread_t hnd)
  116. {
  117. if(hnd != curl_thread_t_null)
  118. CloseHandle(hnd);
  119. }
  120. int Curl_thread_join(curl_thread_t *hnd)
  121. {
  122. #if !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_VISTA) || \
  123. (_WIN32_WINNT < _WIN32_WINNT_VISTA)
  124. int ret = (WaitForSingleObject(*hnd, INFINITE) == WAIT_OBJECT_0);
  125. #else
  126. int ret = (WaitForSingleObjectEx(*hnd, INFINITE, FALSE) == WAIT_OBJECT_0);
  127. #endif
  128. Curl_thread_destroy(*hnd);
  129. *hnd = curl_thread_t_null;
  130. return ret;
  131. }
  132. #endif /* USE_THREADS_* */