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
#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;
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;
}
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());
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__,
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];
}
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));
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) */
#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
}
/* 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;
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)
#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