curl_threads.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include <curl/curl.h>
  24. #if defined(USE_THREADS_POSIX)
  25. # ifdef HAVE_PTHREAD_H
  26. # include <pthread.h>
  27. # endif
  28. #elif defined(USE_THREADS_WIN32)
  29. # ifdef HAVE_PROCESS_H
  30. # include <process.h>
  31. # endif
  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. #elif defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
  89. typedef unsigned long curl_win_thread_handle_t;
  90. #else
  91. typedef uintptr_t curl_win_thread_handle_t;
  92. #endif
  93. curl_thread_t t;
  94. curl_win_thread_handle_t thread_handle;
  95. #ifdef _WIN32_WCE
  96. thread_handle = CreateThread(NULL, 0, func, arg, 0, NULL);
  97. #else
  98. thread_handle = _beginthreadex(NULL, 0, func, arg, 0, NULL);
  99. #endif
  100. t = (curl_thread_t)thread_handle;
  101. if((t == 0) || (t == LongToHandle(-1L))) {
  102. #ifdef _WIN32_WCE
  103. DWORD gle = GetLastError();
  104. errno = ((gle == ERROR_ACCESS_DENIED ||
  105. gle == ERROR_NOT_ENOUGH_MEMORY) ?
  106. EACCES : EINVAL);
  107. #endif
  108. return curl_thread_t_null;
  109. }
  110. return t;
  111. }
  112. void Curl_thread_destroy(curl_thread_t hnd)
  113. {
  114. CloseHandle(hnd);
  115. }
  116. int Curl_thread_join(curl_thread_t *hnd)
  117. {
  118. #if !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_VISTA) || \
  119. (_WIN32_WINNT < _WIN32_WINNT_VISTA)
  120. int ret = (WaitForSingleObject(*hnd, INFINITE) == WAIT_OBJECT_0);
  121. #else
  122. int ret = (WaitForSingleObjectEx(*hnd, INFINITE, FALSE) == WAIT_OBJECT_0);
  123. #endif
  124. Curl_thread_destroy(*hnd);
  125. *hnd = curl_thread_t_null;
  126. return ret;
  127. }
  128. #endif /* USE_THREADS_* */