]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
tests: avoid CreateThread if _beginthreadex is available
authorMarc Hoersken <info@marc-hoersken.de>
Tue, 11 Oct 2022 22:21:23 +0000 (00:21 +0200)
committerMarc Hoersken <info@marc-hoersken.de>
Wed, 19 Oct 2022 09:59:00 +0000 (11:59 +0200)
CreateThread is not threadsafe if mixed with CRT calls.
_beginthreadex on the other hand can be mixed with CRT.

Reviewed-by: Marcel Raad
Closes #9705

tests/libtest/lib3026.c
tests/server/sockfilt.c
tests/server/util.c

index 33a2b56a5c49ee9745ad3eeff17d4687d1ccef00..8ad7e949024359ad905de07747655b5af67a8cbf 100644 (file)
 #define NUM_THREADS 100
 
 #ifdef WIN32
+#ifdef _WIN32_WCE
 static DWORD WINAPI run_thread(LPVOID ptr)
+#else
+#include <process.h>
+static unsigned int WINAPI run_thread(void *ptr)
+#endif
 {
   CURLcode *result = ptr;
 
@@ -42,8 +47,15 @@ static DWORD WINAPI run_thread(LPVOID ptr)
 
 int test(char *URL)
 {
+#ifdef _WIN32_WCE
+  typedef HANDLE curl_win_thread_handle_t;
+#elif defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
+  typedef unsigned long curl_win_thread_handle_t;
+#else
+  typedef uintptr_t curl_win_thread_handle_t;
+#endif
   CURLcode results[NUM_THREADS];
-  HANDLE ths[NUM_THREADS];
+  curl_win_thread_handle_t ths[NUM_THREADS];
   unsigned tid_count = NUM_THREADS, i;
   int test_failure = 0;
   curl_version_info_data *ver;
@@ -58,9 +70,13 @@ int test(char *URL)
   }
 
   for(i = 0; i < tid_count; i++) {
-    HANDLE th;
+    curl_win_thread_handle_t th;
     results[i] = CURL_LAST; /* initialize with invalid value */
+#ifdef _WIN32_WCE
     th = CreateThread(NULL, 0, run_thread, &results[i], 0, NULL);
+#else
+    th = _beginthreadex(NULL, 0, run_thread, &results[i], 0, NULL);
+#endif
     if(!th) {
       fprintf(stderr, "%s:%d Couldn't create thread, errno %d\n",
               __FILE__, __LINE__, GetLastError());
@@ -73,8 +89,8 @@ int test(char *URL)
 
 cleanup:
   for(i = 0; i < tid_count; i++) {
-    WaitForSingleObject(ths[i], INFINITE);
-    CloseHandle(ths[i]);
+    WaitForSingleObject((HANDLE)ths[i], INFINITE);
+    CloseHandle((HANDLE)ths[i]);
     if(results[i] != CURLE_OK) {
       fprintf(stderr, "%s:%d thread[%u]: curl_global_init() failed,"
               "with code %d (%s)\n", __FILE__, __LINE__,
index 34cc8fcdab0d1d941bc0b4705bd00489d7f16f26..39cca069f8db2f2cbdba9fb529cdabaf6f1b178a 100644 (file)
@@ -406,7 +406,12 @@ struct select_ws_wait_data {
   HANDLE signal; /* internal event to signal handle trigger */
   HANDLE abort;  /* internal event to abort waiting threads */
 };
+#ifdef _WIN32_WCE
 static DWORD WINAPI select_ws_wait_thread(LPVOID lpParameter)
+#else
+#include <process.h>
+static unsigned int WINAPI select_ws_wait_thread(void *lpParameter)
+#endif
 {
   struct select_ws_wait_data *data;
   HANDLE signal, handle, handles[2];
@@ -549,8 +554,15 @@ static DWORD WINAPI select_ws_wait_thread(LPVOID lpParameter)
 }
 static HANDLE select_ws_wait(HANDLE handle, HANDLE signal, HANDLE abort)
 {
+#ifdef _WIN32_WCE
+  typedef HANDLE curl_win_thread_handle_t;
+#elif defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
+  typedef unsigned long curl_win_thread_handle_t;
+#else
+  typedef uintptr_t curl_win_thread_handle_t;
+#endif
   struct select_ws_wait_data *data;
-  HANDLE thread = NULL;
+  curl_win_thread_handle_t thread;
 
   /* allocate internal waiting data structure */
   data = malloc(sizeof(struct select_ws_wait_data));
@@ -560,17 +572,19 @@ static HANDLE select_ws_wait(HANDLE handle, HANDLE signal, HANDLE abort)
     data->abort = abort;
 
     /* launch waiting thread */
-    thread = CreateThread(NULL, 0,
-                          &select_ws_wait_thread,
-                          data, 0, NULL);
+#ifdef _WIN32_WCE
+    thread = CreateThread(NULL, 0,  &select_ws_wait_thread, data, 0, NULL);
+#else
+    thread = _beginthreadex(NULL, 0, &select_ws_wait_thread, data, 0, NULL);
+#endif
 
     /* free data if thread failed to launch */
     if(!thread) {
       free(data);
     }
+    return (HANDLE)thread;
   }
-
-  return thread;
+  return NULL;
 }
 struct select_ws_data {
   int fd;                /* provided file descriptor  (indexed by nfd) */
index 9af7305ed63c371f0f7db63308bc4d98fcec3b76..d2214ee6e495cb3a6703e270240603e9196d0e66 100644 (file)
@@ -521,7 +521,11 @@ static SIGHANDLER_T old_sigbreak_handler = SIG_ERR;
 #endif
 
 #ifdef WIN32
+#ifdef _WIN32_WCE
 static DWORD thread_main_id = 0;
+#else
+static unsigned int thread_main_id = 0;
+#endif
 static HANDLE thread_main_window = NULL;
 static HWND hidden_main_window = NULL;
 #endif
@@ -624,7 +628,12 @@ static LRESULT CALLBACK main_window_proc(HWND hwnd, UINT uMsg,
 }
 /* Window message queue loop for hidden main window, details see above.
  */
+#ifdef _WIN32_WCE
 static DWORD WINAPI main_window_loop(LPVOID lpParameter)
+#else
+#include <process.h>
+static unsigned int WINAPI main_window_loop(void *lpParameter)
+#endif
 {
   WNDCLASS wc;
   BOOL ret;
@@ -705,6 +714,14 @@ static SIGHANDLER_T set_signal(int signum, SIGHANDLER_T handler,
 void install_signal_handlers(bool keep_sigalrm)
 {
 #ifdef WIN32
+#ifdef _WIN32_WCE
+  typedef HANDLE curl_win_thread_handle_t;
+#elif defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
+  typedef unsigned long curl_win_thread_handle_t;
+#else
+  typedef uintptr_t curl_win_thread_handle_t;
+#endif
+  curl_win_thread_handle_t thread;
   /* setup windows exit event before any signal can trigger */
   exit_event = CreateEvent(NULL, TRUE, FALSE, NULL);
   if(!exit_event)
@@ -753,10 +770,14 @@ void install_signal_handlers(bool keep_sigalrm)
 #ifdef WIN32
   if(!SetConsoleCtrlHandler(ctrl_event_handler, TRUE))
     logmsg("cannot install CTRL event handler");
-  thread_main_window = CreateThread(NULL, 0,
-                                    &main_window_loop,
-                                    (LPVOID)GetModuleHandle(NULL),
-                                    0, &thread_main_id);
+#ifdef _WIN32_WCE
+  thread = CreateThread(NULL, 0, &main_window_loop,
+                        (LPVOID)GetModuleHandle(NULL), 0, &thread_main_id);
+#else
+  thread = _beginthreadex(NULL, 0, &main_window_loop,
+                          (void *)GetModuleHandle(NULL), 0, &thread_main_id);
+#endif
+  thread_main_window = (HANDLE)thread;
   if(!thread_main_window || !thread_main_id)
     logmsg("cannot start main window loop");
 #endif