lib3026.c 5.3 KB

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