implemented Setup.hs to build boehm cpp libs and install them;
[hs-boehmgc.git] / gc-7.2 / tests / thread_leak_test.c
1
2 #ifdef HAVE_CONFIG_H
3 # include "private/config.h"
4 #endif
5
6 #ifndef GC_THREADS
7 # define GC_THREADS
8 #endif
9
10 #include "leak_detector.h"
11
12 #ifdef GC_PTHREADS
13 # include <pthread.h>
14 #else
15 # include <windows.h>
16 #endif
17
18 #include <stdio.h>
19
20 #ifdef GC_PTHREADS
21   void * test(void * arg)
22 #else
23   DWORD WINAPI test(LPVOID arg)
24 #endif
25 {
26     int *p[10];
27     int i;
28     for (i = 0; i < 10; ++i) {
29         p[i] = malloc(sizeof(int)+i);
30     }
31     CHECK_LEAKS();
32     for (i = 1; i < 10; ++i) {
33         free(p[i]);
34     }
35 #   ifdef GC_PTHREADS
36       return arg;
37 #   else
38       return (DWORD)(GC_word)arg;
39 #   endif
40 }
41
42 #define NTHREADS 5
43
44 int main(void) {
45     int i;
46 #   ifdef GC_PTHREADS
47       pthread_t t[NTHREADS];
48 #   else
49       HANDLE t[NTHREADS];
50       DWORD thread_id;
51 #   endif
52     int code;
53
54     GC_set_find_leak(1); /* for new collect versions not compiled       */
55                          /* with -DFIND_LEAK.                           */
56     GC_INIT();
57
58     for (i = 0; i < NTHREADS; ++i) {
59 #       ifdef GC_PTHREADS
60           code = pthread_create(t + i, 0, test, 0);
61 #       else
62           t[i] = CreateThread(NULL, 0, test, 0, 0, &thread_id);
63           code = t[i] != NULL ? 0 : (int)GetLastError();
64 #       endif
65         if (code != 0) {
66             printf("Thread creation failed %d\n", code);
67         }
68     }
69
70     for (i = 0; i < NTHREADS; ++i) {
71 #       ifdef GC_PTHREADS
72           code = pthread_join(t[i], 0);
73 #       else
74           code = WaitForSingleObject(t[i], INFINITE) == WAIT_OBJECT_0 ? 0 :
75                                                         (int)GetLastError();
76 #       endif
77         if (code != 0) {
78             printf("Thread join failed %d\n", code);
79         }
80     }
81
82     CHECK_LEAKS();
83     CHECK_LEAKS();
84     CHECK_LEAKS();
85     return 0;
86 }