lib3026.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 "test.h"
  25. #include "testutil.h"
  26. #include "warnless.h"
  27. #define NUM_THREADS 100
  28. #ifdef _WIN32
  29. #if defined(_WIN32_WCE) || defined(CURL_WINDOWS_APP)
  30. static DWORD WINAPI run_thread(LPVOID ptr)
  31. #else
  32. #include <process.h>
  33. static unsigned int WINAPI run_thread(void *ptr)
  34. #endif
  35. {
  36. CURLcode *result = ptr;
  37. *result = curl_global_init(CURL_GLOBAL_ALL);
  38. if(*result == CURLE_OK)
  39. curl_global_cleanup();
  40. return 0;
  41. }
  42. CURLcode test(char *URL)
  43. {
  44. #if defined(_WIN32_WCE) || defined(CURL_WINDOWS_APP)
  45. typedef HANDLE curl_win_thread_handle_t;
  46. #else
  47. typedef uintptr_t curl_win_thread_handle_t;
  48. #endif
  49. CURLcode results[NUM_THREADS];
  50. curl_win_thread_handle_t ths[NUM_THREADS];
  51. unsigned tid_count = NUM_THREADS, i;
  52. int test_failure = 0;
  53. curl_version_info_data *ver;
  54. (void) URL;
  55. ver = curl_version_info(CURLVERSION_NOW);
  56. if((ver->features & CURL_VERSION_THREADSAFE) == 0) {
  57. fprintf(stderr, "%s:%d On Windows but the "
  58. "CURL_VERSION_THREADSAFE feature flag is not set\n",
  59. __FILE__, __LINE__);
  60. return (CURLcode)-1;
  61. }
  62. /* On Windows libcurl global init/cleanup calls LoadLibrary/FreeLibrary for
  63. secur32.dll and iphlpapi.dll. Here we load them beforehand so that when
  64. libcurl calls LoadLibrary/FreeLibrary it only increases/decreases the
  65. library's refcount rather than actually loading/unloading the library,
  66. which would affect the test runtime. */
  67. (void)win32_load_system_library(TEXT("secur32.dll"));
  68. (void)win32_load_system_library(TEXT("iphlpapi.dll"));
  69. for(i = 0; i < tid_count; i++) {
  70. curl_win_thread_handle_t th;
  71. results[i] = CURL_LAST; /* initialize with invalid value */
  72. #if defined(_WIN32_WCE) || defined(CURL_WINDOWS_APP)
  73. th = CreateThread(NULL, 0, run_thread, &results[i], 0, NULL);
  74. #else
  75. th = _beginthreadex(NULL, 0, run_thread, &results[i], 0, NULL);
  76. #endif
  77. if(!th) {
  78. fprintf(stderr, "%s:%d Couldn't create thread, errno %lu\n",
  79. __FILE__, __LINE__, GetLastError());
  80. tid_count = i;
  81. test_failure = -1;
  82. goto cleanup;
  83. }
  84. ths[i] = th;
  85. }
  86. cleanup:
  87. for(i = 0; i < tid_count; i++) {
  88. WaitForSingleObject((HANDLE)ths[i], INFINITE);
  89. CloseHandle((HANDLE)ths[i]);
  90. if(results[i] != CURLE_OK) {
  91. fprintf(stderr, "%s:%d thread[%u]: curl_global_init() failed,"
  92. "with code %d (%s)\n", __FILE__, __LINE__,
  93. i, (int) results[i], curl_easy_strerror(results[i]));
  94. test_failure = -1;
  95. }
  96. }
  97. return (CURLcode)test_failure;
  98. }
  99. #elif defined(HAVE_PTHREAD_H)
  100. #include <pthread.h>
  101. #include <unistd.h>
  102. static void *run_thread(void *ptr)
  103. {
  104. CURLcode *result = ptr;
  105. *result = curl_global_init(CURL_GLOBAL_ALL);
  106. if(*result == CURLE_OK)
  107. curl_global_cleanup();
  108. return NULL;
  109. }
  110. CURLcode test(char *URL)
  111. {
  112. CURLcode results[NUM_THREADS];
  113. pthread_t tids[NUM_THREADS];
  114. unsigned tid_count = NUM_THREADS, i;
  115. CURLcode test_failure = CURLE_OK;
  116. curl_version_info_data *ver;
  117. (void) URL;
  118. ver = curl_version_info(CURLVERSION_NOW);
  119. if((ver->features & CURL_VERSION_THREADSAFE) == 0) {
  120. fprintf(stderr, "%s:%d Have pthread but the "
  121. "CURL_VERSION_THREADSAFE feature flag is not set\n",
  122. __FILE__, __LINE__);
  123. return (CURLcode)-1;
  124. }
  125. for(i = 0; i < tid_count; i++) {
  126. int res;
  127. results[i] = CURL_LAST; /* initialize with invalid value */
  128. res = pthread_create(&tids[i], NULL, run_thread, &results[i]);
  129. if(res) {
  130. fprintf(stderr, "%s:%d Couldn't create thread, errno %d\n",
  131. __FILE__, __LINE__, res);
  132. tid_count = i;
  133. test_failure = (CURLcode)-1;
  134. goto cleanup;
  135. }
  136. }
  137. cleanup:
  138. for(i = 0; i < tid_count; i++) {
  139. pthread_join(tids[i], NULL);
  140. if(results[i] != CURLE_OK) {
  141. fprintf(stderr, "%s:%d thread[%u]: curl_global_init() failed,"
  142. "with code %d (%s)\n", __FILE__, __LINE__,
  143. i, (int) results[i], curl_easy_strerror(results[i]));
  144. test_failure = (CURLcode)-1;
  145. }
  146. }
  147. return test_failure;
  148. }
  149. #else /* without pthread or Windows, this test doesn't work */
  150. CURLcode test(char *URL)
  151. {
  152. curl_version_info_data *ver;
  153. (void)URL;
  154. ver = curl_version_info(CURLVERSION_NOW);
  155. if((ver->features & CURL_VERSION_THREADSAFE) != 0) {
  156. fprintf(stderr, "%s:%d No pthread but the "
  157. "CURL_VERSION_THREADSAFE feature flag is set\n",
  158. __FILE__, __LINE__);
  159. return (CURLcode)-1;
  160. }
  161. return CURLE_OK;
  162. }
  163. #endif