curl_threads.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
  27. #include <pthread.h>
  28. #endif
  29. #include "curl_threads.h"
  30. #include "curl_memory.h"
  31. /* The last #include FILE should be: */
  32. #include "memdebug.h"
  33. #ifdef USE_THREADS_POSIX
  34. struct Curl_actual_call {
  35. unsigned int (*func)(void *);
  36. void *arg;
  37. };
  38. static void *curl_thread_create_thunk(void *arg)
  39. {
  40. struct Curl_actual_call *ac = arg;
  41. unsigned int (*func)(void *) = ac->func;
  42. void *real_arg = ac->arg;
  43. free(ac);
  44. (*func)(real_arg);
  45. return 0;
  46. }
  47. curl_thread_t Curl_thread_create(CURL_THREAD_RETURN_T
  48. (CURL_STDCALL *func) (void *), void *arg)
  49. {
  50. curl_thread_t t = malloc(sizeof(pthread_t));
  51. struct Curl_actual_call *ac = malloc(sizeof(struct Curl_actual_call));
  52. int rc;
  53. if(!(ac && t))
  54. goto err;
  55. ac->func = func;
  56. ac->arg = arg;
  57. rc = pthread_create(t, NULL, curl_thread_create_thunk, ac);
  58. if(rc) {
  59. CURL_SETERRNO(rc);
  60. goto err;
  61. }
  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. #elif defined(USE_THREADS_WIN32)
  84. curl_thread_t Curl_thread_create(CURL_THREAD_RETURN_T
  85. (CURL_STDCALL *func) (void *), void *arg)
  86. {
  87. curl_thread_t t = CreateThread(NULL, 0, func, arg, 0, NULL);
  88. if(!t) {
  89. DWORD gle = GetLastError();
  90. /* !checksrc! disable ERRNOVAR 1 */
  91. int err = (gle == ERROR_ACCESS_DENIED ||
  92. gle == ERROR_NOT_ENOUGH_MEMORY) ?
  93. EACCES : EINVAL;
  94. CURL_SETERRNO(err);
  95. return curl_thread_t_null;
  96. }
  97. return t;
  98. }
  99. void Curl_thread_destroy(curl_thread_t *hnd)
  100. {
  101. if(*hnd != curl_thread_t_null) {
  102. CloseHandle(*hnd);
  103. *hnd = curl_thread_t_null;
  104. }
  105. }
  106. int Curl_thread_join(curl_thread_t *hnd)
  107. {
  108. #ifdef UNDER_CE
  109. int ret = (WaitForSingleObject(*hnd, INFINITE) == WAIT_OBJECT_0);
  110. #else
  111. int ret = (WaitForSingleObjectEx(*hnd, INFINITE, FALSE) == WAIT_OBJECT_0);
  112. #endif
  113. Curl_thread_destroy(hnd);
  114. return ret;
  115. }
  116. #endif /* USE_THREADS_* */