113498266Sopenharmony_ci/*************************************************************************** 213498266Sopenharmony_ci * _ _ ____ _ 313498266Sopenharmony_ci * Project ___| | | | _ \| | 413498266Sopenharmony_ci * / __| | | | |_) | | 513498266Sopenharmony_ci * | (__| |_| | _ <| |___ 613498266Sopenharmony_ci * \___|\___/|_| \_\_____| 713498266Sopenharmony_ci * 813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 913498266Sopenharmony_ci * 1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which 1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms 1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html. 1313498266Sopenharmony_ci * 1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell 1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is 1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file. 1713498266Sopenharmony_ci * 1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 1913498266Sopenharmony_ci * KIND, either express or implied. 2013498266Sopenharmony_ci * 2113498266Sopenharmony_ci * SPDX-License-Identifier: curl 2213498266Sopenharmony_ci * 2313498266Sopenharmony_ci ***************************************************************************/ 2413498266Sopenharmony_ci#include "test.h" 2513498266Sopenharmony_ci 2613498266Sopenharmony_ci#include "testutil.h" 2713498266Sopenharmony_ci#include "warnless.h" 2813498266Sopenharmony_ci 2913498266Sopenharmony_ci#define NUM_THREADS 100 3013498266Sopenharmony_ci 3113498266Sopenharmony_ci#ifdef _WIN32 3213498266Sopenharmony_ci#ifdef _WIN32_WCE 3313498266Sopenharmony_cistatic DWORD WINAPI run_thread(LPVOID ptr) 3413498266Sopenharmony_ci#else 3513498266Sopenharmony_ci#include <process.h> 3613498266Sopenharmony_cistatic unsigned int WINAPI run_thread(void *ptr) 3713498266Sopenharmony_ci#endif 3813498266Sopenharmony_ci{ 3913498266Sopenharmony_ci CURLcode *result = ptr; 4013498266Sopenharmony_ci 4113498266Sopenharmony_ci *result = curl_global_init(CURL_GLOBAL_ALL); 4213498266Sopenharmony_ci if(*result == CURLE_OK) 4313498266Sopenharmony_ci curl_global_cleanup(); 4413498266Sopenharmony_ci 4513498266Sopenharmony_ci return 0; 4613498266Sopenharmony_ci} 4713498266Sopenharmony_ci 4813498266Sopenharmony_ciint test(char *URL) 4913498266Sopenharmony_ci{ 5013498266Sopenharmony_ci#ifdef _WIN32_WCE 5113498266Sopenharmony_ci typedef HANDLE curl_win_thread_handle_t; 5213498266Sopenharmony_ci#else 5313498266Sopenharmony_ci typedef uintptr_t curl_win_thread_handle_t; 5413498266Sopenharmony_ci#endif 5513498266Sopenharmony_ci CURLcode results[NUM_THREADS]; 5613498266Sopenharmony_ci curl_win_thread_handle_t ths[NUM_THREADS]; 5713498266Sopenharmony_ci unsigned tid_count = NUM_THREADS, i; 5813498266Sopenharmony_ci int test_failure = 0; 5913498266Sopenharmony_ci curl_version_info_data *ver; 6013498266Sopenharmony_ci (void) URL; 6113498266Sopenharmony_ci 6213498266Sopenharmony_ci ver = curl_version_info(CURLVERSION_NOW); 6313498266Sopenharmony_ci if((ver->features & CURL_VERSION_THREADSAFE) == 0) { 6413498266Sopenharmony_ci fprintf(stderr, "%s:%d On Windows but the " 6513498266Sopenharmony_ci "CURL_VERSION_THREADSAFE feature flag is not set\n", 6613498266Sopenharmony_ci __FILE__, __LINE__); 6713498266Sopenharmony_ci return -1; 6813498266Sopenharmony_ci } 6913498266Sopenharmony_ci 7013498266Sopenharmony_ci /* On Windows libcurl global init/cleanup calls LoadLibrary/FreeLibrary for 7113498266Sopenharmony_ci secur32.dll and iphlpapi.dll. Here we load them beforehand so that when 7213498266Sopenharmony_ci libcurl calls LoadLibrary/FreeLibrary it only increases/decreases the 7313498266Sopenharmony_ci library's refcount rather than actually loading/unloading the library, 7413498266Sopenharmony_ci which would affect the test runtime. */ 7513498266Sopenharmony_ci (void)win32_load_system_library(TEXT("secur32.dll")); 7613498266Sopenharmony_ci (void)win32_load_system_library(TEXT("iphlpapi.dll")); 7713498266Sopenharmony_ci 7813498266Sopenharmony_ci for(i = 0; i < tid_count; i++) { 7913498266Sopenharmony_ci curl_win_thread_handle_t th; 8013498266Sopenharmony_ci results[i] = CURL_LAST; /* initialize with invalid value */ 8113498266Sopenharmony_ci#ifdef _WIN32_WCE 8213498266Sopenharmony_ci th = CreateThread(NULL, 0, run_thread, &results[i], 0, NULL); 8313498266Sopenharmony_ci#else 8413498266Sopenharmony_ci th = _beginthreadex(NULL, 0, run_thread, &results[i], 0, NULL); 8513498266Sopenharmony_ci#endif 8613498266Sopenharmony_ci if(!th) { 8713498266Sopenharmony_ci fprintf(stderr, "%s:%d Couldn't create thread, errno %lu\n", 8813498266Sopenharmony_ci __FILE__, __LINE__, GetLastError()); 8913498266Sopenharmony_ci tid_count = i; 9013498266Sopenharmony_ci test_failure = -1; 9113498266Sopenharmony_ci goto cleanup; 9213498266Sopenharmony_ci } 9313498266Sopenharmony_ci ths[i] = th; 9413498266Sopenharmony_ci } 9513498266Sopenharmony_ci 9613498266Sopenharmony_cicleanup: 9713498266Sopenharmony_ci for(i = 0; i < tid_count; i++) { 9813498266Sopenharmony_ci WaitForSingleObject((HANDLE)ths[i], INFINITE); 9913498266Sopenharmony_ci CloseHandle((HANDLE)ths[i]); 10013498266Sopenharmony_ci if(results[i] != CURLE_OK) { 10113498266Sopenharmony_ci fprintf(stderr, "%s:%d thread[%u]: curl_global_init() failed," 10213498266Sopenharmony_ci "with code %d (%s)\n", __FILE__, __LINE__, 10313498266Sopenharmony_ci i, (int) results[i], curl_easy_strerror(results[i])); 10413498266Sopenharmony_ci test_failure = -1; 10513498266Sopenharmony_ci } 10613498266Sopenharmony_ci } 10713498266Sopenharmony_ci 10813498266Sopenharmony_ci return test_failure; 10913498266Sopenharmony_ci} 11013498266Sopenharmony_ci 11113498266Sopenharmony_ci#elif defined(HAVE_PTHREAD_H) 11213498266Sopenharmony_ci#include <pthread.h> 11313498266Sopenharmony_ci#include <unistd.h> 11413498266Sopenharmony_ci 11513498266Sopenharmony_cistatic void *run_thread(void *ptr) 11613498266Sopenharmony_ci{ 11713498266Sopenharmony_ci CURLcode *result = ptr; 11813498266Sopenharmony_ci 11913498266Sopenharmony_ci *result = curl_global_init(CURL_GLOBAL_ALL); 12013498266Sopenharmony_ci if(*result == CURLE_OK) 12113498266Sopenharmony_ci curl_global_cleanup(); 12213498266Sopenharmony_ci 12313498266Sopenharmony_ci return NULL; 12413498266Sopenharmony_ci} 12513498266Sopenharmony_ci 12613498266Sopenharmony_ciint test(char *URL) 12713498266Sopenharmony_ci{ 12813498266Sopenharmony_ci CURLcode results[NUM_THREADS]; 12913498266Sopenharmony_ci pthread_t tids[NUM_THREADS]; 13013498266Sopenharmony_ci unsigned tid_count = NUM_THREADS, i; 13113498266Sopenharmony_ci int test_failure = 0; 13213498266Sopenharmony_ci curl_version_info_data *ver; 13313498266Sopenharmony_ci (void) URL; 13413498266Sopenharmony_ci 13513498266Sopenharmony_ci ver = curl_version_info(CURLVERSION_NOW); 13613498266Sopenharmony_ci if((ver->features & CURL_VERSION_THREADSAFE) == 0) { 13713498266Sopenharmony_ci fprintf(stderr, "%s:%d Have pthread but the " 13813498266Sopenharmony_ci "CURL_VERSION_THREADSAFE feature flag is not set\n", 13913498266Sopenharmony_ci __FILE__, __LINE__); 14013498266Sopenharmony_ci return -1; 14113498266Sopenharmony_ci } 14213498266Sopenharmony_ci 14313498266Sopenharmony_ci for(i = 0; i < tid_count; i++) { 14413498266Sopenharmony_ci int res; 14513498266Sopenharmony_ci results[i] = CURL_LAST; /* initialize with invalid value */ 14613498266Sopenharmony_ci res = pthread_create(&tids[i], NULL, run_thread, &results[i]); 14713498266Sopenharmony_ci if(res) { 14813498266Sopenharmony_ci fprintf(stderr, "%s:%d Couldn't create thread, errno %d\n", 14913498266Sopenharmony_ci __FILE__, __LINE__, res); 15013498266Sopenharmony_ci tid_count = i; 15113498266Sopenharmony_ci test_failure = -1; 15213498266Sopenharmony_ci goto cleanup; 15313498266Sopenharmony_ci } 15413498266Sopenharmony_ci } 15513498266Sopenharmony_ci 15613498266Sopenharmony_cicleanup: 15713498266Sopenharmony_ci for(i = 0; i < tid_count; i++) { 15813498266Sopenharmony_ci pthread_join(tids[i], NULL); 15913498266Sopenharmony_ci if(results[i] != CURLE_OK) { 16013498266Sopenharmony_ci fprintf(stderr, "%s:%d thread[%u]: curl_global_init() failed," 16113498266Sopenharmony_ci "with code %d (%s)\n", __FILE__, __LINE__, 16213498266Sopenharmony_ci i, (int) results[i], curl_easy_strerror(results[i])); 16313498266Sopenharmony_ci test_failure = -1; 16413498266Sopenharmony_ci } 16513498266Sopenharmony_ci } 16613498266Sopenharmony_ci 16713498266Sopenharmony_ci return test_failure; 16813498266Sopenharmony_ci} 16913498266Sopenharmony_ci 17013498266Sopenharmony_ci#else /* without pthread or Windows, this test doesn't work */ 17113498266Sopenharmony_ciint test(char *URL) 17213498266Sopenharmony_ci{ 17313498266Sopenharmony_ci curl_version_info_data *ver; 17413498266Sopenharmony_ci (void)URL; 17513498266Sopenharmony_ci 17613498266Sopenharmony_ci ver = curl_version_info(CURLVERSION_NOW); 17713498266Sopenharmony_ci if((ver->features & CURL_VERSION_THREADSAFE) != 0) { 17813498266Sopenharmony_ci fprintf(stderr, "%s:%d No pthread but the " 17913498266Sopenharmony_ci "CURL_VERSION_THREADSAFE feature flag is set\n", 18013498266Sopenharmony_ci __FILE__, __LINE__); 18113498266Sopenharmony_ci return -1; 18213498266Sopenharmony_ci } 18313498266Sopenharmony_ci return 0; 18413498266Sopenharmony_ci} 18513498266Sopenharmony_ci#endif 186