curl_threads.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. /* !checksrc! disable SPACEBEFOREPAREN 1 */
  83. curl_thread_t Curl_thread_create(unsigned int (CURL_STDCALL *func) (void *),
  84. void *arg)
  85. {
  86. #ifdef _WIN32_WCE
  87. typedef HANDLE curl_win_thread_handle_t;
  88. #else
  89. typedef uintptr_t curl_win_thread_handle_t;
  90. #endif
  91. curl_thread_t t;
  92. curl_win_thread_handle_t thread_handle;
  93. #ifdef _WIN32_WCE
  94. thread_handle = CreateThread(NULL, 0, func, arg, 0, NULL);
  95. #else
  96. thread_handle = _beginthreadex(NULL, 0, func, arg, 0, NULL);
  97. #endif
  98. t = (curl_thread_t)thread_handle;
  99. if((t == 0) || (t == LongToHandle(-1L))) {
  100. #ifdef _WIN32_WCE
  101. DWORD gle = GetLastError();
  102. errno = ((gle == ERROR_ACCESS_DENIED ||
  103. gle == ERROR_NOT_ENOUGH_MEMORY) ?
  104. EACCES : EINVAL);
  105. #endif
  106. return curl_thread_t_null;
  107. }
  108. return t;
  109. }
  110. void Curl_thread_destroy(curl_thread_t hnd)
  111. {
  112. CloseHandle(hnd);
  113. }
  114. int Curl_thread_join(curl_thread_t *hnd)
  115. {
  116. #if !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_VISTA) || \
  117. (_WIN32_WINNT < _WIN32_WINNT_VISTA)
  118. int ret = (WaitForSingleObject(*hnd, INFINITE) == WAIT_OBJECT_0);
  119. #else
  120. int ret = (WaitForSingleObjectEx(*hnd, INFINITE, FALSE) == WAIT_OBJECT_0);
  121. #endif
  122. Curl_thread_destroy(*hnd);
  123. *hnd = curl_thread_t_null;
  124. return ret;
  125. }
  126. #endif /* USE_THREADS_* */