xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame^] | 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 | |
| 26 | #include "testutil.h" |
| 27 | #include "warnless.h" |
| 28 | |
| 29 | #define NUM_THREADS 100 |
| 30 | |
| 31 | #ifdef WIN32 |
| 32 | #ifdef _WIN32_WCE |
| 33 | static DWORD WINAPI run_thread(LPVOID ptr) |
| 34 | #else |
| 35 | #include <process.h> |
| 36 | static unsigned int WINAPI run_thread(void *ptr) |
| 37 | #endif |
| 38 | { |
| 39 | CURLcode *result = ptr; |
| 40 | |
| 41 | *result = curl_global_init(CURL_GLOBAL_ALL); |
| 42 | if(*result == CURLE_OK) |
| 43 | curl_global_cleanup(); |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | int test(char *URL) |
| 49 | { |
| 50 | #ifdef _WIN32_WCE |
| 51 | typedef HANDLE curl_win_thread_handle_t; |
| 52 | #elif defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) |
| 53 | typedef unsigned long curl_win_thread_handle_t; |
| 54 | #else |
| 55 | typedef uintptr_t curl_win_thread_handle_t; |
| 56 | #endif |
| 57 | CURLcode results[NUM_THREADS]; |
| 58 | curl_win_thread_handle_t ths[NUM_THREADS]; |
| 59 | unsigned tid_count = NUM_THREADS, i; |
| 60 | int test_failure = 0; |
| 61 | curl_version_info_data *ver; |
| 62 | (void) URL; |
| 63 | |
| 64 | ver = curl_version_info(CURLVERSION_NOW); |
| 65 | if((ver->features & CURL_VERSION_THREADSAFE) == 0) { |
| 66 | fprintf(stderr, "%s:%d On Windows but the " |
| 67 | "CURL_VERSION_THREADSAFE feature flag is not set\n", |
| 68 | __FILE__, __LINE__); |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | for(i = 0; i < tid_count; i++) { |
| 73 | curl_win_thread_handle_t th; |
| 74 | results[i] = CURL_LAST; /* initialize with invalid value */ |
| 75 | #ifdef _WIN32_WCE |
| 76 | th = CreateThread(NULL, 0, run_thread, &results[i], 0, NULL); |
| 77 | #else |
| 78 | th = _beginthreadex(NULL, 0, run_thread, &results[i], 0, NULL); |
| 79 | #endif |
| 80 | if(!th) { |
| 81 | fprintf(stderr, "%s:%d Couldn't create thread, errno %d\n", |
| 82 | __FILE__, __LINE__, GetLastError()); |
| 83 | tid_count = i; |
| 84 | test_failure = -1; |
| 85 | goto cleanup; |
| 86 | } |
| 87 | ths[i] = th; |
| 88 | } |
| 89 | |
| 90 | cleanup: |
| 91 | for(i = 0; i < tid_count; i++) { |
| 92 | WaitForSingleObject((HANDLE)ths[i], INFINITE); |
| 93 | CloseHandle((HANDLE)ths[i]); |
| 94 | if(results[i] != CURLE_OK) { |
| 95 | fprintf(stderr, "%s:%d thread[%u]: curl_global_init() failed," |
| 96 | "with code %d (%s)\n", __FILE__, __LINE__, |
| 97 | i, (int) results[i], curl_easy_strerror(results[i])); |
| 98 | test_failure = -1; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return test_failure; |
| 103 | } |
| 104 | |
| 105 | #elif defined(HAVE_PTHREAD_H) |
| 106 | #include <pthread.h> |
| 107 | #include <unistd.h> |
| 108 | |
| 109 | static void *run_thread(void *ptr) |
| 110 | { |
| 111 | CURLcode *result = ptr; |
| 112 | |
| 113 | *result = curl_global_init(CURL_GLOBAL_ALL); |
| 114 | if(*result == CURLE_OK) |
| 115 | curl_global_cleanup(); |
| 116 | |
| 117 | return NULL; |
| 118 | } |
| 119 | |
| 120 | int test(char *URL) |
| 121 | { |
| 122 | CURLcode results[NUM_THREADS]; |
| 123 | pthread_t tids[NUM_THREADS]; |
| 124 | unsigned tid_count = NUM_THREADS, i; |
| 125 | int test_failure = 0; |
| 126 | curl_version_info_data *ver; |
| 127 | (void) URL; |
| 128 | |
| 129 | ver = curl_version_info(CURLVERSION_NOW); |
| 130 | if((ver->features & CURL_VERSION_THREADSAFE) == 0) { |
| 131 | fprintf(stderr, "%s:%d Have pthread but the " |
| 132 | "CURL_VERSION_THREADSAFE feature flag is not set\n", |
| 133 | __FILE__, __LINE__); |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | for(i = 0; i < tid_count; i++) { |
| 138 | int res; |
| 139 | results[i] = CURL_LAST; /* initialize with invalid value */ |
| 140 | res = pthread_create(&tids[i], NULL, run_thread, &results[i]); |
| 141 | if(res) { |
| 142 | fprintf(stderr, "%s:%d Couldn't create thread, errno %d\n", |
| 143 | __FILE__, __LINE__, res); |
| 144 | tid_count = i; |
| 145 | test_failure = -1; |
| 146 | goto cleanup; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | cleanup: |
| 151 | for(i = 0; i < tid_count; i++) { |
| 152 | pthread_join(tids[i], NULL); |
| 153 | if(results[i] != CURLE_OK) { |
| 154 | fprintf(stderr, "%s:%d thread[%u]: curl_global_init() failed," |
| 155 | "with code %d (%s)\n", __FILE__, __LINE__, |
| 156 | i, (int) results[i], curl_easy_strerror(results[i])); |
| 157 | test_failure = -1; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return test_failure; |
| 162 | } |
| 163 | |
| 164 | #else /* without pthread or Windows, this test doesn't work */ |
| 165 | int test(char *URL) |
| 166 | { |
| 167 | curl_version_info_data *ver; |
| 168 | (void)URL; |
| 169 | |
| 170 | ver = curl_version_info(CURLVERSION_NOW); |
| 171 | if((ver->features & CURL_VERSION_THREADSAFE) != 0) { |
| 172 | fprintf(stderr, "%s:%d No pthread but the " |
| 173 | "CURL_VERSION_THREADSAFE feature flag is set\n", |
| 174 | __FILE__, __LINE__); |
| 175 | return -1; |
| 176 | } |
| 177 | return 0; |
| 178 | } |
| 179 | #endif |