]> granicus.if.org Git - gc/commitdiff
FIX: MinGW/MingwCE: Use CreateThread in initsecondarythread test and
authorIvan Maidanski <ivmai@mail.ru>
Wed, 31 Aug 2011 15:01:42 +0000 (19:01 +0400)
committerIvan Maidanski <ivmai@mail.ru>
Wed, 31 Aug 2011 15:01:42 +0000 (19:01 +0400)
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.

tests/initsecondarythread.c
tests/thread_leak_test.c

index 0f97ad318596302481ad34f0699e01646f4facfe..93088653873e53defa8c8e63eddd2109301a052d 100644 (file)
 
 #include "gc.h"
 
-#include <pthread.h>
+#ifdef GC_PTHREADS
+# include <pthread.h>
+#else
+# include <windows.h>
+#endif
 
 #include <stdlib.h>
 #include <stdio.h>
 
-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;
 }
index df363a500fc8a73fbd659de682723d24e3e11b12..3c3fc1e8599e1895944944a748c41c4ff8da6a0a 100644 (file)
@@ -1,11 +1,24 @@
+
 #ifndef GC_THREADS
-#  define GC_THREADS
-#endif /* GC_THREADS */
+# define GC_THREADS
+#endif
+
 #include "leak_detector.h"
-#include <pthread.h>
+
+#ifdef GC_PTHREADS
+# include <pthread.h>
+#else
+# include <windows.h>
+#endif
+
 #include <stdio.h>
 
-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();