Include some headers in new multi-thread test
[tinycc.git] / tests / libtcc_test_mt.c
blob266870fec2883aa9bc6d732353269cfe84132bd1
1 /*
2 * Multi-thread Test for libtcc
3 */
5 #ifndef FIB
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include "libtcc.h"
11 #define M 20 /* number of states */
12 #define F(n) (n % 20 + 2) /* fib argument */
14 #ifdef _WIN32
15 #include <windows.h>
16 #define TF_TYPE(func, param) DWORD WINAPI func(void *param)
17 typedef TF_TYPE(ThreadFunc, x);
18 HANDLE hh[M];
19 void create_thread(ThreadFunc f, int n)
21 DWORD tid;
22 hh[n] = CreateThread(NULL, 0, f, (void*)(size_t)n, 0, &tid);
24 void wait_threads(int n)
26 WaitForMultipleObjects(n, hh, TRUE, INFINITE);
27 while (n)
28 CloseHandle(hh[--n]);
30 void sleep_ms(unsigned n)
32 Sleep(n);
34 #else
35 #include <sys/time.h>
36 #include <unistd.h>
37 #include <pthread.h>
38 #define TF_TYPE(func, param) void* func(void *param)
39 typedef TF_TYPE(ThreadFunc, x);
40 pthread_t hh[M];
41 void create_thread(ThreadFunc f, int n)
43 pthread_create(&hh[n], NULL, f, (void*)(size_t)n);
45 void wait_threads(int n)
47 while (n)
48 pthread_join(hh[--n], NULL);
51 void sleep_ms(unsigned n)
53 usleep(n * 1000);
55 #endif
57 void handle_error(void *opaque, const char *msg)
59 fprintf(opaque, "%s\n", msg);
62 /* this function is called by the generated code */
63 int add(int a, int b)
65 return a + b;
68 #define _str(s) #s
69 #define str(s) _str(s)
70 /* as a trick, prepend #line directive for better error/warning messages */
71 #define PROG(lbl) \
72 char lbl[] = "#line " str(__LINE__) " " str(__FILE__) "\n\n"
74 PROG(my_program)
75 "#include <tcclib.h>\n" /* include the "Simple libc header for TCC" */
76 "int add(int a, int b);\n"
77 "int fib(int n)\n"
78 "{\n"
79 " if (n <= 2)\n"
80 " return 1;\n"
81 " else\n"
82 " return add(fib(n-1),fib(n-2));\n"
83 "}\n"
84 "\n"
85 "int foo(int n)\n"
86 "{\n"
87 " printf(\" %d\", fib(n));\n"
88 " return 0;\n"
89 "# warning is this the correct file:line...\n"
90 "}\n";
92 int g_argc; char **g_argv;
94 void parse_args(TCCState *s)
96 int i;
97 /* if tcclib.h and libtcc1.a are not installed, where can we find them */
98 for (i = 1; i < g_argc; ++i) {
99 char *a = g_argv[i];
100 if (a[0] == '-') {
101 if (a[1] == 'B')
102 tcc_set_lib_path(s, a+2);
103 else if (a[1] == 'I')
104 tcc_add_include_path(s, a+2);
105 else if (a[1] == 'L')
106 tcc_add_library_path(s, a+2);
107 else if (a[1] == 'D') {
108 char *eq = strchr(a+2, '=');
109 if (eq) {
110 *eq = 0;
111 tcc_define_symbol(s, a+2, eq+1);
112 *eq = '=';
113 } else
114 tcc_define_symbol(s, a+2, NULL);
120 TCCState *new_state(int w)
122 TCCState *s = tcc_new();
123 if (!s) {
124 fprintf(stderr, __FILE__ ": could not create tcc state\n");
125 exit(1);
127 tcc_set_error_func(s, stdout, handle_error);
128 parse_args(s);
129 if (!w) tcc_set_options(s, "-w");
130 tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
131 return s;
134 void *reloc_state(TCCState *s, const char *entry)
136 void *func;
137 tcc_add_symbol(s, "add", add);
138 if (tcc_relocate(s, TCC_RELOCATE_AUTO) < 0) {
139 fprintf(stderr, __FILE__ ": could not relocate tcc state.\n");
140 return NULL;
142 func = tcc_get_symbol(s, entry);
143 if (!func)
144 fprintf(stderr, __FILE__ ": could not get entry symbol.\n");
145 return func;
148 /* work with several states at the same time */
149 int state_test(void)
151 TCCState *s[M];
152 int (*func[M])(int);
153 int n;
155 for (n = 0; n < M + 4; ++n) {
156 unsigned a = n, b = n - 1, c = n - 2, d = n - 3, e = n - 4;
157 if (a < M)
158 s[a] = new_state(0);
159 if (b < M)
160 if (tcc_compile_string(s[b], my_program) == -1)
161 break;
162 if (c < M)
163 func[c] = reloc_state(s[c], "foo");
164 if (d < M && func[d])
165 func[d](F(d));
166 if (e < M)
167 tcc_delete(s[e]);
169 return 0;
172 /* simple compilation in threads */
173 TF_TYPE(thread_test_simple, vn)
175 TCCState *s;
176 int (*func)(int);
177 int ret;
178 int n = (size_t)vn;
180 s = new_state(0);
181 sleep_ms(1);
182 ret = tcc_compile_string(s, my_program);
183 sleep_ms(1);
184 if (ret >= 0) {
185 func = reloc_state(s, "foo");
186 if (func)
187 func(F(n));
189 tcc_delete(s);
190 return 0;
193 /* more complex compilation in threads */
194 TF_TYPE(thread_test_complex, vn)
196 TCCState *s;
197 int ret;
198 int n = (size_t)vn;
199 char *argv[30], b[10];
200 int argc = 0, i;
202 sprintf(b, "%d", F(n));
204 argv[argc++] = "../tcc.c";
205 for (i = 1; i < g_argc; ++i) argv[argc++] = g_argv[i];
206 #if 0
207 argv[argc++] = "-run";
208 argv[argc++] = "../tcc.c";
209 for (i = 1; i < g_argc; ++i) argv[argc++] = g_argv[i];
210 #endif
211 argv[argc++] = "-g";
212 argv[argc++] = "-DFIB";
213 argv[argc++] = "-run";
214 argv[argc++] = __FILE__;
215 argv[argc++] = b;
216 argv[argc] = NULL;
218 s = new_state(1);
219 sleep_ms(1);
220 ret = tcc_add_file(s, argv[0]);
221 sleep_ms(1);
222 if (ret >= 0)
223 tcc_run(s, argc, argv);
224 tcc_delete(s);
225 return 0;
228 void time_tcc(int n)
230 TCCState *s;
231 int ret;
232 while (--n >= 0) {
233 s = new_state(1);
234 ret = tcc_add_file(s, "../tcc.c");
235 tcc_delete(s);
236 if (ret < 0)
237 break;
241 static unsigned getclock_ms(void)
243 #ifdef _WIN32
244 return GetTickCount();
245 #else
246 struct timeval tv;
247 gettimeofday(&tv, NULL);
248 return tv.tv_sec*1000 + (tv.tv_usec+500)/1000;
249 #endif
252 int main(int argc, char **argv)
254 int n;
255 unsigned t;
257 g_argc = argc;
258 g_argv = argv;
260 #if 1
261 printf("----- libtest : mixed calls -------\n"), fflush(stdout);
262 t = getclock_ms();
263 state_test();
264 printf("\n(%u ms)\n", getclock_ms() - t);
265 #endif
266 #if 1
267 printf("----- libtest : threads ------------\n"), fflush(stdout);
268 t = getclock_ms();
269 for (n = 0; n < M; ++n)
270 create_thread(thread_test_simple, n);
271 wait_threads(n);
272 printf("\n(%u ms)\n", getclock_ms() - t);
273 #endif
274 #if 1
275 printf("----- libtest : tcc in threads -----\n"), fflush(stdout);
276 t = getclock_ms();
277 for (n = 0; n < M; ++n)
278 create_thread(thread_test_complex, n);
279 wait_threads(n);
280 printf("\n(%u ms)\n", getclock_ms() - t);
281 #endif
282 #if 1
283 printf("----- compilation of tcc -----------\n"), fflush(stdout);
284 t = getclock_ms();
285 time_tcc(10);
286 printf("(%u ms)\n", (getclock_ms() - t) / 10), fflush(stdout);
287 #endif
288 return 0;
291 #else
292 #include <tcclib.h>
293 int fib(n)
295 return (n <= 2) ? 1 : fib(n-1) + fib(n-2);
298 int main(int argc, char **argv)
300 printf(" %d", fib(atoi(argv[1]), 2));
301 return 0;
303 #endif