Debian Codegraph @

gc/tests/subthread_create.c

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