26
26
#include "testutil.h"
27
27
#include "warnless.h"
28
28
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 )
30
90
#include <pthread.h>
31
91
#include <unistd.h>
32
92
33
- #define NUM_THREADS 100
34
-
35
93
static void * run_thread (void * ptr )
36
94
{
37
95
CURLcode * result = ptr ;
@@ -61,7 +119,9 @@ int test(char *URL)
61
119
}
62
120
63
121
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 ]);
65
125
if (res ) {
66
126
fprintf (stderr , "%s:%d Couldn't create thread, errno %d\n" ,
67
127
__FILE__ , __LINE__ , res );
@@ -85,7 +145,7 @@ int test(char *URL)
85
145
return test_failure ;
86
146
}
87
147
88
- #else /* without pthread, this test doesn't work */
148
+ #else /* without pthread or Windows , this test doesn't work */
89
149
int test (char * URL )
90
150
{
91
151
curl_version_info_data * ver ;
0 commit comments