From: Ivan Maidanski Date: Wed, 31 Aug 2011 15:01:42 +0000 (+0400) Subject: FIX: MinGW/MingwCE: Use CreateThread in initsecondarythread test and X-Git-Tag: gc7_2~138^2~5 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=005792c614d9932735290e9314a3db0d5e757fda;p=gc FIX: MinGW/MingwCE: Use CreateThread in initsecondarythread test and thread_leak_test. * tests/initsecondarythread.c: Include windows.h instead of pthread.h unless GC_PTHREADS. * tests/thread_leak_test.c: Likewise. * tests/initsecondarythread.c (thread): Use WINAPI and set return type to DWORD unless GC_PTHREADS. * tests/thread_leak_test.c (test): Likewise. * tests/initsecondarythread.c (main): Use HANDLE, CreateThread and WaitForSingleObject instead of pthread_t, pthread_create and pthread_join, respectively, unless GC_PTHREADS. * tests/thread_leak_test.c (main): Likewise. --- diff --git a/tests/initsecondarythread.c b/tests/initsecondarythread.c index 054d672a..f6a81177 100644 --- a/tests/initsecondarythread.c +++ b/tests/initsecondarythread.c @@ -24,25 +24,42 @@ #include "gc.h" -#include +#ifdef GC_PTHREADS +# include +#else +# include +#endif #include #include -static void *thread(void *arg) +#ifdef GC_PTHREADS + static void *thread(void *arg) +#else + static DWORD WINAPI thread(LPVOID arg) +#endif { GC_INIT(); (void)GC_MALLOC(123); (void)GC_MALLOC(12345); - return arg; +# ifdef GC_PTHREADS + return arg; +# else + return (DWORD)(GC_word)arg; +# endif } #include "private/gcconfig.h" int main(void) { - int code; - pthread_t t; +# ifdef GC_PTHREADS + int code; + pthread_t t; +# else + HANDLE t; + DWORD thread_id; +# endif # if !(defined(BEOS) || defined(MSWIN32) || defined(MSWINCE) \ || defined(CYGWIN32) || defined(GC_OPENBSD_THREADS) \ || (defined(DARWIN) && !defined(NO_PTHREAD_GET_STACKADDR_NP)) \ @@ -53,13 +70,27 @@ int main(void) /* GC_INIT() must be called from main thread only. */ GC_INIT(); # endif - if ((code = pthread_create (&t, NULL, thread, NULL)) != 0) { - printf("Thread creation failed %d\n", code); - return 1; - } - if ((code = pthread_join (t, NULL)) != 0) { - printf("Thread join failed %d\n", code); - return 1; - } +# ifdef GC_PTHREADS + if ((code = pthread_create (&t, NULL, thread, NULL)) != 0) { + printf("Thread creation failed %d\n", code); + return 1; + } + if ((code = pthread_join (t, NULL)) != 0) { + printf("Thread join failed %d\n", code); + return 1; + } +# else + t = CreateThread(NULL, 0, thread, 0, 0, &thread_id); + if (t == NULL) { + printf("Thread creation failed %d\n", (int)GetLastError()); + return 1; + } + if (WaitForSingleObject(t, INFINITE) != WAIT_OBJECT_0) { + printf("Thread join failed %d\n", (int)GetLastError()); + CloseHandle(t); + return 1; + } + CloseHandle(t); +# endif return 0; } diff --git a/tests/thread_leak_test.c b/tests/thread_leak_test.c index df363a50..3c3fc1e8 100644 --- a/tests/thread_leak_test.c +++ b/tests/thread_leak_test.c @@ -1,11 +1,24 @@ + #ifndef GC_THREADS -# define GC_THREADS -#endif /* GC_THREADS */ +# define GC_THREADS +#endif + #include "leak_detector.h" -#include + +#ifdef GC_PTHREADS +# include +#else +# include +#endif + #include -void * test(void * arg) { +#ifdef GC_PTHREADS + void * test(void * arg) +#else + DWORD WINAPI test(LPVOID arg) +#endif +{ int *p[10]; int i; for (i = 0; i < 10; ++i) { @@ -15,28 +28,53 @@ void * test(void * arg) { for (i = 1; i < 10; ++i) { free(p[i]); } - return 0; +# ifdef GC_PTHREADS + return arg; +# else + return (DWORD)(GC_word)arg; +# endif } #define NTHREADS 5 int main(void) { int i; - pthread_t t[NTHREADS]; +# ifdef GC_PTHREADS + pthread_t t[NTHREADS]; +# else + HANDLE t[NTHREADS]; + DWORD thread_id; +# endif int code; + GC_set_find_leak(1); /* for new collect versions not compiled */ /* with -DFIND_LEAK. */ GC_INIT(); + for (i = 0; i < NTHREADS; ++i) { - if ((code = pthread_create(t + i, 0, test, 0)) != 0) { +# ifdef GC_PTHREADS + code = pthread_create(t + i, 0, test, 0); +# else + t[i] = CreateThread(NULL, 0, test, 0, 0, &thread_id); + code = t[i] != NULL ? 0 : (int)GetLastError(); +# endif + if (code != 0) { printf("Thread creation failed %d\n", code); } } + for (i = 0; i < NTHREADS; ++i) { - if ((code = pthread_join(t[i], 0)) != 0) { +# ifdef GC_PTHREADS + code = pthread_join(t[i], 0); +# else + code = WaitForSingleObject(t[i], INFINITE) == WAIT_OBJECT_0 ? 0 : + (int)GetLastError(); +# endif + if (code != 0) { printf("Thread join failed %d\n", code); } } + CHECK_LEAKS(); CHECK_LEAKS(); CHECK_LEAKS();