tests: avoid CreateThread if _beginthreadex is available
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
This commit is contained in:
parent
3f5a7975a5
commit
81094cb492
@ -29,7 +29,12 @@
|
||||
#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__,
|
||||
|
||||
@ -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) */
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user