lib3026.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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 "test.h"
  25. #include "testutil.h"
  26. #include "warnless.h"
  27. #define NUM_THREADS 100
  28. #ifdef WIN32
  29. static DWORD WINAPI run_thread(LPVOID ptr)
  30. {
  31. CURLcode *result = ptr;
  32. *result = curl_global_init(CURL_GLOBAL_ALL);
  33. if(*result == CURLE_OK)
  34. curl_global_cleanup();
  35. return 0;
  36. }
  37. int test(char *URL)
  38. {
  39. CURLcode results[NUM_THREADS];
  40. HANDLE ths[NUM_THREADS];
  41. unsigned tid_count = NUM_THREADS, i;
  42. int test_failure = 0;
  43. curl_version_info_data *ver;
  44. (void) URL;
  45. ver = curl_version_info(CURLVERSION_NOW);
  46. if((ver->features & CURL_VERSION_THREADSAFE) == 0) {
  47. fprintf(stderr, "%s:%d On Windows but the "
  48. "CURL_VERSION_THREADSAFE feature flag is not set\n",
  49. __FILE__, __LINE__);
  50. return -1;
  51. }
  52. for(i = 0; i < tid_count; i++) {
  53. HANDLE th;
  54. results[i] = CURL_LAST; /* initialize with invalid value */
  55. th = CreateThread(NULL, 0, run_thread, &results[i], 0, NULL);
  56. if(!th) {
  57. fprintf(stderr, "%s:%d Couldn't create thread, errno %d\n",
  58. __FILE__, __LINE__, GetLastError());
  59. tid_count = i;
  60. test_failure = -1;
  61. goto cleanup;
  62. }
  63. ths[i] = th;
  64. }
  65. cleanup:
  66. for(i = 0; i < tid_count; i++) {
  67. WaitForSingleObject(ths[i], INFINITE);
  68. CloseHandle(ths[i]);
  69. if(results[i] != CURLE_OK) {
  70. fprintf(stderr, "%s:%d thread[%u]: curl_global_init() failed,"
  71. "with code %d (%s)\n", __FILE__, __LINE__,
  72. i, (int) results[i], curl_easy_strerror(results[i]));
  73. test_failure = -1;
  74. }
  75. }
  76. return test_failure;
  77. }
  78. #elif defined(HAVE_PTHREAD_H)
  79. #include <pthread.h>
  80. #include <unistd.h>
  81. static void *run_thread(void *ptr)
  82. {
  83. CURLcode *result = ptr;
  84. *result = curl_global_init(CURL_GLOBAL_ALL);
  85. if(*result == CURLE_OK)
  86. curl_global_cleanup();
  87. return NULL;
  88. }
  89. int test(char *URL)
  90. {
  91. CURLcode results[NUM_THREADS];
  92. pthread_t tids[NUM_THREADS];
  93. unsigned tid_count = NUM_THREADS, i;
  94. int test_failure = 0;
  95. curl_version_info_data *ver;
  96. (void) URL;
  97. ver = curl_version_info(CURLVERSION_NOW);
  98. if((ver->features & CURL_VERSION_THREADSAFE) == 0) {
  99. fprintf(stderr, "%s:%d Have pthread but the "
  100. "CURL_VERSION_THREADSAFE feature flag is not set\n",
  101. __FILE__, __LINE__);
  102. return -1;
  103. }
  104. for(i = 0; i < tid_count; i++) {
  105. int res;
  106. results[i] = CURL_LAST; /* initialize with invalid value */
  107. res = pthread_create(&tids[i], NULL, run_thread, &results[i]);
  108. if(res) {
  109. fprintf(stderr, "%s:%d Couldn't create thread, errno %d\n",
  110. __FILE__, __LINE__, res);
  111. tid_count = i;
  112. test_failure = -1;
  113. goto cleanup;
  114. }
  115. }
  116. cleanup:
  117. for(i = 0; i < tid_count; i++) {
  118. pthread_join(tids[i], NULL);
  119. if(results[i] != CURLE_OK) {
  120. fprintf(stderr, "%s:%d thread[%u]: curl_global_init() failed,"
  121. "with code %d (%s)\n", __FILE__, __LINE__,
  122. i, (int) results[i], curl_easy_strerror(results[i]));
  123. test_failure = -1;
  124. }
  125. }
  126. return test_failure;
  127. }
  128. #else /* without pthread or Windows, this test doesn't work */
  129. int test(char *URL)
  130. {
  131. curl_version_info_data *ver;
  132. (void)URL;
  133. ver = curl_version_info(CURLVERSION_NOW);
  134. if((ver->features & CURL_VERSION_THREADSAFE) != 0) {
  135. fprintf(stderr, "%s:%d No pthread but the "
  136. "CURL_VERSION_THREADSAFE feature flag is set\n",
  137. __FILE__, __LINE__);
  138. return -1;
  139. }
  140. return 0;
  141. }
  142. #endif