Skip to content

Commit 40b6206

Browse files
committed
test3026: add support for Windows using native Win32 threads
Reviewed-by: Viktor Szakats Reviewed-by: Jay Satiro Reviewed-by: Daniel Stenberg Follow up to 7ade9c5 Closes #9012
1 parent 6e241bb commit 40b6206

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

tests/data/test3026

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ slow
2020
<features>
2121
threadsafe
2222
threaded-resolver
23-
!win32
2423
</features>
2524
<server>
2625
none

tests/libtest/lib3026.c

+65-5
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,70 @@
2626
#include "testutil.h"
2727
#include "warnless.h"
2828

29-
#ifdef HAVE_PTHREAD_H
29+
#define NUM_THREADS 100
30+
31+
#ifdef WIN32
32+
static DWORD WINAPI run_thread(LPVOID ptr)
33+
{
34+
CURLcode *result = ptr;
35+
36+
*result = curl_global_init(CURL_GLOBAL_ALL);
37+
if(*result == CURLE_OK)
38+
curl_global_cleanup();
39+
40+
return 0;
41+
}
42+
43+
int test(char *URL)
44+
{
45+
CURLcode results[NUM_THREADS];
46+
HANDLE ths[NUM_THREADS];
47+
unsigned tid_count = NUM_THREADS, i;
48+
int test_failure = 0;
49+
curl_version_info_data *ver;
50+
(void) URL;
51+
52+
ver = curl_version_info(CURLVERSION_NOW);
53+
if((ver->features & CURL_VERSION_THREADSAFE) == 0) {
54+
fprintf(stderr, "%s:%d On Windows but the "
55+
"CURL_VERSION_THREADSAFE feature flag is not set\n",
56+
__FILE__, __LINE__);
57+
return -1;
58+
}
59+
60+
for(i = 0; i < tid_count; i++) {
61+
HANDLE th;
62+
results[i] = CURL_LAST; /* initialize with invalid value */
63+
th = CreateThread(NULL, 0, run_thread, &results[i], 0, NULL);
64+
if(!th) {
65+
fprintf(stderr, "%s:%d Couldn't create thread, errno %d\n",
66+
__FILE__, __LINE__, GetLastError());
67+
tid_count = i;
68+
test_failure = -1;
69+
goto cleanup;
70+
}
71+
ths[i] = th;
72+
}
73+
74+
cleanup:
75+
for(i = 0; i < tid_count; i++) {
76+
WaitForSingleObject(ths[i], INFINITE);
77+
CloseHandle(ths[i]);
78+
if(results[i] != CURLE_OK) {
79+
fprintf(stderr, "%s:%d thread[%u]: curl_global_init() failed,"
80+
"with code %d (%s)\n", __FILE__, __LINE__,
81+
i, (int) results[i], curl_easy_strerror(results[i]));
82+
test_failure = -1;
83+
}
84+
}
85+
86+
return test_failure;
87+
}
88+
89+
#elif defined(HAVE_PTHREAD_H)
3090
#include <pthread.h>
3191
#include <unistd.h>
3292

33-
#define NUM_THREADS 100
34-
3593
static void *run_thread(void *ptr)
3694
{
3795
CURLcode *result = ptr;
@@ -61,7 +119,9 @@ int test(char *URL)
61119
}
62120

63121
for(i = 0; i < tid_count; i++) {
64-
int res = pthread_create(&tids[i], NULL, run_thread, &results[i]);
122+
int res;
123+
results[i] = CURL_LAST; /* initialize with invalid value */
124+
res = pthread_create(&tids[i], NULL, run_thread, &results[i]);
65125
if(res) {
66126
fprintf(stderr, "%s:%d Couldn't create thread, errno %d\n",
67127
__FILE__, __LINE__, res);
@@ -85,7 +145,7 @@ int test(char *URL)
85145
return test_failure;
86146
}
87147

88-
#else /* without pthread, this test doesn't work */
148+
#else /* without pthread or Windows, this test doesn't work */
89149
int test(char *URL)
90150
{
91151
curl_version_info_data *ver;

0 commit comments

Comments
 (0)