re PR target/78594 (Bug in November 11th, 2016 change to rs6000.md)
[official-gcc.git] / boehm-gc / testsuite / boehm-gc.c / thread_leak_test.c
blob89014651261bc02ec30ecf9f14c504af90c21b55
1 #ifndef GC_THREADS
2 # define GC_THREADS
3 #endif
4 #include "leak_detector.h"
5 #ifdef GC_PTHREADS
6 # include <pthread.h>
7 #else
8 # include <windows.h>
9 #endif
10 #include <stdio.h>
12 #ifdef GC_PTHREADS
13 void * test(void * arg)
14 #else
15 DWORD WINAPI test(LPVOID arg)
16 #endif
18 int *p[10];
19 int i;
20 for (i = 0; i < 10; ++i) {
21 p[i] = malloc(sizeof(int)+i);
23 CHECK_LEAKS();
24 for (i = 1; i < 10; ++i) {
25 free(p[i]);
27 #ifdef GC_PTHREADS
28 return arg;
29 #else
30 return (DWORD)(GC_word)arg;
31 #endif
34 #define NTHREADS 5
36 int main(void) {
37 int i;
38 #ifdef GC_PTHREADS
39 pthread_t t[NTHREADS];
40 #else
41 HANDLE t[NTHREADS];
42 DWORD thread_id;
43 #endif
44 int code;
46 GC_find_leak = 1; /* for new collect versions not compiled */
47 GC_INIT();
48 for (i = 0; i < NTHREADS; ++i) {
49 #ifdef GC_PTHREADS
50 code = pthread_create(t + i, 0, test, 0);
51 #else
52 t[i] = CreateThread(NULL, 0, test, 0, 0, &thread_id);
53 code = t[i] != NULL ? 0 : (int)GetLastError();
54 #endif
55 if (code != 0) {
56 printf("Thread creation failed %d\n", code);
59 for (i = 0; i < NTHREADS; ++i) {
60 #ifdef GC_PTHREADS
61 code = pthread_join(t[i], 0);
62 #else
63 code = WaitForSingleObject(t[i], INFINITE) == WAIT_OBJECT_0 ? 0 :
64 (int)GetLastError();
65 #endif
66 if (code != 0) {
67 printf("Thread join failed %d\n", code);
70 CHECK_LEAKS();
71 CHECK_LEAKS();
72 CHECK_LEAKS();
73 return 0;