Debian Codegraph @

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