From c750e3fe85433e8a0500b46e4d00879ee7051c8f Mon Sep 17 00:00:00 2001 From: Marc Hoersken Date: Tue, 14 Jun 2022 21:55:15 +0200 Subject: [PATCH] tests/libtest/lib3026.c: try to implement test for Windows Work in Progress - untested - do not merge - PR for testing --- tests/data/test3026 | 1 - tests/libtest/lib3026.c | 57 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/tests/data/test3026 b/tests/data/test3026 index fb80cc83f68879..c6b421576aa066 100644 --- a/tests/data/test3026 +++ b/tests/data/test3026 @@ -19,7 +19,6 @@ slow # be used for it threaded-resolver -!win32 none diff --git a/tests/libtest/lib3026.c b/tests/libtest/lib3026.c index 43fe33529e1f00..f3bf9dbc1a3332 100644 --- a/tests/libtest/lib3026.c +++ b/tests/libtest/lib3026.c @@ -26,10 +26,6 @@ #include "testutil.h" #include "warnless.h" -#ifdef HAVE_PTHREAD_H -#include -#include - #define NUM_THREADS 1000 static void *run_thread(void *ptr) @@ -43,6 +39,56 @@ static void *run_thread(void *ptr) return NULL; } +#ifdef WIN32 +int test(char *URL) +{ + CURLcode results[NUM_THREADS]; + HANDLE tids[NUM_THREADS]; + unsigned tid_count = NUM_THREADS, i; + int test_failure = 0; + curl_version_info_data *ver; + (void) URL; + + ver = curl_version_info(CURLVERSION_NOW); + if((ver->features & CURL_VERSION_THREADSAFE) == 0) { + fprintf(stderr, "%s:%d On Windows but the " + "CURL_VERSION_THREADSAFE feature flag is not set\n", + __FILE__, __LINE__); + return -1; + } + + for(i = 0; i < tid_count; i++) { + results[i] = CURL_LAST; + HANDLE tid = CreateThread(NULL, 0, run_thread, &results[i], 0, NULL); + if(tid == NULL) { + fprintf(stderr, "%s:%d Couldn't create thread, errno %d\n", + __FILE__, __LINE__, res); + tid_count = i; + test_failure = -1; + goto cleanup; + } + tids[i] = tid; + } + +cleanup: + for(i = 0; i < tid_count; i++) { + WaitForSingleObject(tids[i], INFINITE); + CloseHandle(tids[i]); + if(results[i] != CURLE_OK) { + fprintf(stderr, "%s:%d thread[%u]: curl_global_init() failed," + "with code %d (%s)\n", __FILE__, __LINE__, + i, (int) results[i], curl_easy_strerror(results[i])); + test_failure = -1; + } + } + + return test_failure; +} + +#elif defined(HAVE_PTHREAD_H) +#include +#include + int test(char *URL) { CURLcode results[NUM_THREADS]; @@ -61,6 +107,7 @@ int test(char *URL) } for(i = 0; i < tid_count; i++) { + results[i] = CURL_LAST; int res = pthread_create(&tids[i], NULL, run_thread, &results[i]); if(res) { fprintf(stderr, "%s:%d Couldn't create thread, errno %d\n", @@ -85,7 +132,7 @@ int test(char *URL) return test_failure; } -#else /* without pthread, this test doesn't work */ +#else /* without pthread or Windows, this test doesn't work */ int test(char *URL) { curl_version_info_data *ver;