Debian Codegraph @

external/bdwgc/tests/subthread_create.c

raw
1
2 #ifdef HAVE_CONFIG_H
3 /* For PARALLEL_MARK */
4 # include "config.h"
5 #endif
6
7 #ifndef GC_THREADS
8 # define GC_THREADS
9 #endif
10 #include "gc.h"
11
12 #ifdef PARALLEL_MARK
13 # define AO_REQUIRE_CAS
14 #endif
15 #include "private/gc_atomic_ops.h"
16
17 #include <stdio.h>
18
19 #ifdef AO_HAVE_fetch_and_add1
20
21 #ifdef GC_PTHREADS
22 # include <pthread.h>
23 #else
24 # include <windows.h>
25 #endif
26
27 #if defined(__HAIKU__)
28 # include <errno.h>
29 #endif
30
31 #include <stdlib.h>
32 #include <string.h>
33
34 #ifndef NTHREADS
35 # define NTHREADS 31 /* number of initial threads */
36 #endif
37
38 #ifndef MAX_SUBTHREAD_DEPTH
39 # define MAX_ALIVE_THREAD_COUNT 55
40 # define MAX_SUBTHREAD_DEPTH 7
41 # define MAX_SUBTHREAD_COUNT 200
42 #endif
43
44 #ifndef DECAY_NUMER
45 # define DECAY_NUMER 15
46 # define DECAY_DENOM 16
47 #endif
48
49 volatile AO_t thread_created_cnt = 0;
50 volatile AO_t thread_ended_cnt = 0;
51
52 #ifdef GC_PTHREADS
53 void *entry(void *arg)
54 #else
55 DWORD WINAPI entry(LPVOID arg)
56 #endif
57 {
58 int thread_num = AO_fetch_and_add1(&thread_created_cnt);
59 GC_word my_depth = (GC_word)arg + 1;
60
61 if (my_depth <= MAX_SUBTHREAD_DEPTH
62 && thread_num < MAX_SUBTHREAD_COUNT
63 && (thread_num % DECAY_DENOM) < DECAY_NUMER
64 && thread_num - (int)AO_load(&thread_ended_cnt)
65 <= MAX_ALIVE_THREAD_COUNT) {
66 # ifdef GC_PTHREADS
67 int err;
68 pthread_t th;
69
70 err = pthread_create(&th, NULL, entry, (void *)my_depth);
71 if (err != 0) {
72 fprintf(stderr, "Thread #%d creation failed: %s\n", thread_num,
73 strerror(err));
74 exit(2);
75 }
76 err = pthread_detach(th);
77 if (err != 0) {
78 fprintf(stderr, "Thread #%d detach failed: %s\n", thread_num,
79 strerror(err));
80 exit(2);
81 }
82 # else
83 HANDLE th;
84 DWORD thread_id;
85
86 th = CreateThread(NULL, 0, entry, (LPVOID)my_depth, 0, &thread_id);
87 if (th == NULL) {
88 fprintf(stderr, "Thread #%d creation failed: %d\n", thread_num,
89 (int)GetLastError());
90 exit(2);
91 }
92 CloseHandle(th);
93 # endif
94 }
95
96 (void)AO_fetch_and_add1(&thread_ended_cnt);
97 return 0;
98 }
99
100 int main(void)
101 {
102 #if NTHREADS > 0
103 int i;
104 # ifdef GC_PTHREADS
105 int err;
106 pthread_t th[NTHREADS];
107 # else
108 HANDLE th[NTHREADS];
109 # endif
110
111 GC_INIT();
112 for (i = 0; i < NTHREADS; ++i) {
113 # ifdef GC_PTHREADS
114 err = pthread_create(&th[i], NULL, entry, 0);
115 if (err) {
116 fprintf(stderr, "Thread creation failed: %s\n", strerror(err));
117 exit(1);
118 }
119 # else
120 DWORD thread_id;
121 th[i] = CreateThread(NULL, 0, entry, 0, 0, &thread_id);
122 if (th[i] == NULL) {
123 fprintf(stderr, "Thread creation failed: %d\n",
124 (int)GetLastError());
125 exit(1);
126 }
127 # endif
128 }
129
130 for (i = 0; i < NTHREADS; ++i) {
131 # ifdef GC_PTHREADS
132 void *res;
133 err = pthread_join(th[i], &res);
134 if (err) {
135 fprintf(stderr, "Failed to join thread: %s\n", strerror(err));
136 # if defined(__HAIKU__)
137 /* The error is just ignored (and the test is ended) to */
138 /* workaround some bug in Haiku pthread_join. */
139 /* TODO: The thread is not deleted from GC_threads. */
140 if (ESRCH == err) break;
141 # endif
142 exit(1);
143 }
144 # else
145 if (WaitForSingleObject(th[i], INFINITE) != WAIT_OBJECT_0) {
146 fprintf(stderr, "Failed to join thread: %d\n",
147 (int)GetLastError());
148 CloseHandle(th[i]);
149 exit(1);
150 }
151 CloseHandle(th[i]);
152 # endif
153 }
154 #endif
155 printf("subthread_create: created %d threads (%d ended)\n",
156 (int)AO_load(&thread_created_cnt), (int)AO_load(&thread_ended_cnt));
157 return 0;
158 }
159
160 #else
161
162 int main(void)
163 {
164 printf("subthread_create test skipped\n");
165 return 0;
166 }
167
168 #endif /* !AO_HAVE_fetch_and_add1 */