lib3026.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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. for(i = 0; i < tid_count; i++) {
  65. curl_win_thread_handle_t th;
  66. results[i] = CURL_LAST; /* initialize with invalid value */
  67. #ifdef _WIN32_WCE
  68. th = CreateThread(NULL, 0, run_thread, &results[i], 0, NULL);
  69. #else
  70. th = _beginthreadex(NULL, 0, run_thread, &results[i], 0, NULL);
  71. #endif
  72. if(!th) {
  73. fprintf(stderr, "%s:%d Couldn't create thread, errno %d\n",
  74. __FILE__, __LINE__, GetLastError());
  75. tid_count = i;
  76. test_failure = -1;
  77. goto cleanup;
  78. }
  79. ths[i] = th;
  80. }
  81. cleanup:
  82. for(i = 0; i < tid_count; i++) {
  83. WaitForSingleObject((HANDLE)ths[i], INFINITE);
  84. CloseHandle((HANDLE)ths[i]);
  85. if(results[i] != CURLE_OK) {
  86. fprintf(stderr, "%s:%d thread[%u]: curl_global_init() failed,"
  87. "with code %d (%s)\n", __FILE__, __LINE__,
  88. i, (int) results[i], curl_easy_strerror(results[i]));
  89. test_failure = -1;
  90. }
  91. }
  92. return test_failure;
  93. }
  94. #elif defined(HAVE_PTHREAD_H)
  95. #include <pthread.h>
  96. #include <unistd.h>
  97. static void *run_thread(void *ptr)
  98. {
  99. CURLcode *result = ptr;
  100. *result = curl_global_init(CURL_GLOBAL_ALL);
  101. if(*result == CURLE_OK)
  102. curl_global_cleanup();
  103. return NULL;
  104. }
  105. int test(char *URL)
  106. {
  107. CURLcode results[NUM_THREADS];
  108. pthread_t tids[NUM_THREADS];
  109. unsigned tid_count = NUM_THREADS, i;
  110. int test_failure = 0;
  111. curl_version_info_data *ver;
  112. (void) URL;
  113. ver = curl_version_info(CURLVERSION_NOW);
  114. if((ver->features & CURL_VERSION_THREADSAFE) == 0) {
  115. fprintf(stderr, "%s:%d Have pthread but the "
  116. "CURL_VERSION_THREADSAFE feature flag is not set\n",
  117. __FILE__, __LINE__);
  118. return -1;
  119. }
  120. for(i = 0; i < tid_count; i++) {
  121. int res;
  122. results[i] = CURL_LAST; /* initialize with invalid value */
  123. res = pthread_create(&tids[i], NULL, run_thread, &results[i]);
  124. if(res) {
  125. fprintf(stderr, "%s:%d Couldn't create thread, errno %d\n",
  126. __FILE__, __LINE__, res);
  127. tid_count = i;
  128. test_failure = -1;
  129. goto cleanup;
  130. }
  131. }
  132. cleanup:
  133. for(i = 0; i < tid_count; i++) {
  134. pthread_join(tids[i], NULL);
  135. if(results[i] != CURLE_OK) {
  136. fprintf(stderr, "%s:%d thread[%u]: curl_global_init() failed,"
  137. "with code %d (%s)\n", __FILE__, __LINE__,
  138. i, (int) results[i], curl_easy_strerror(results[i]));
  139. test_failure = -1;
  140. }
  141. }
  142. return test_failure;
  143. }
  144. #else /* without pthread or Windows, this test doesn't work */
  145. int test(char *URL)
  146. {
  147. curl_version_info_data *ver;
  148. (void)URL;
  149. ver = curl_version_info(CURLVERSION_NOW);
  150. if((ver->features & CURL_VERSION_THREADSAFE) != 0) {
  151. fprintf(stderr, "%s:%d No pthread but the "
  152. "CURL_VERSION_THREADSAFE feature flag is set\n",
  153. __FILE__, __LINE__);
  154. return -1;
  155. }
  156. return 0;
  157. }
  158. #endif