Debian Codegraph @

external/bdwgc/tests/thread_leak_test.c

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