2 * Multi-thread Test for libtcc
11 #define M 20 /* number of states */
12 #define F(n) (n % 20 + 2) /* fib argument */
16 #define TF_TYPE(func, param) DWORD WINAPI func(void *param)
17 typedef TF_TYPE(ThreadFunc
, x
);
19 void create_thread(ThreadFunc f
, int n
)
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
);
30 void sleep_ms(unsigned n
)
38 #define TF_TYPE(func, param) void* func(void *param)
39 typedef TF_TYPE(ThreadFunc
, x
);
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
)
48 pthread_join(hh
[--n
], NULL
);
51 void sleep_ms(unsigned n
)
57 void handle_error(void *opaque
, const char *msg
)
59 fprintf(opaque
, "%s\n", msg
);
62 /* this function is called by the generated code */
69 #define str(s) _str(s)
70 /* as a trick, prepend #line directive for better error/warning messages */
72 char lbl[] = "#line " str(__LINE__) " " str(__FILE__) "\n\n"
75 "#include <tcclib.h>\n" /* include the "Simple libc header for TCC" */
76 "int add(int a, int b);\n"
82 " return add(fib(n-1),fib(n-2));\n"
87 " printf(\" %d\", fib(n));\n"
89 "# warning is this the correct file:line...\n"
92 int g_argc
; char **g_argv
;
94 void parse_args(TCCState
*s
)
97 /* if tcclib.h and libtcc1.a are not installed, where can we find them */
98 for (i
= 1; i
< g_argc
; ++i
) {
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, '=');
111 tcc_define_symbol(s
, a
+2, eq
+1);
114 tcc_define_symbol(s
, a
+2, NULL
);
120 TCCState
*new_state(int w
)
122 TCCState
*s
= tcc_new();
124 fprintf(stderr
, __FILE__
": could not create tcc state\n");
127 tcc_set_error_func(s
, stdout
, handle_error
);
129 if (!w
) tcc_set_options(s
, "-w");
130 tcc_set_output_type(s
, TCC_OUTPUT_MEMORY
);
134 void *reloc_state(TCCState
*s
, const char *entry
)
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");
142 func
= tcc_get_symbol(s
, entry
);
144 fprintf(stderr
, __FILE__
": could not get entry symbol.\n");
148 /* work with several states at the same time */
155 for (n
= 0; n
< M
+ 4; ++n
) {
156 unsigned a
= n
, b
= n
- 1, c
= n
- 2, d
= n
- 3, e
= n
- 4;
160 if (tcc_compile_string(s
[b
], my_program
) == -1)
163 func
[c
] = reloc_state(s
[c
], "foo");
164 if (d
< M
&& func
[d
])
172 /* simple compilation in threads */
173 TF_TYPE(thread_test_simple
, vn
)
182 ret
= tcc_compile_string(s
, my_program
);
185 func
= reloc_state(s
, "foo");
193 /* more complex compilation in threads */
194 TF_TYPE(thread_test_complex
, vn
)
199 char *argv
[30], b
[10];
202 sprintf(b
, "%d", F(n
));
204 argv
[argc
++] = "../tcc.c";
205 for (i
= 1; i
< g_argc
; ++i
) argv
[argc
++] = g_argv
[i
];
207 argv
[argc
++] = "-run";
208 argv
[argc
++] = "../tcc.c";
209 for (i
= 1; i
< g_argc
; ++i
) argv
[argc
++] = g_argv
[i
];
212 argv
[argc
++] = "-DFIB";
213 argv
[argc
++] = "-run";
214 argv
[argc
++] = __FILE__
;
220 ret
= tcc_add_file(s
, argv
[0]);
223 tcc_run(s
, argc
, argv
);
234 ret
= tcc_add_file(s
, "../tcc.c");
241 static unsigned getclock_ms(void)
244 return GetTickCount();
247 gettimeofday(&tv
, NULL
);
248 return tv
.tv_sec
*1000 + (tv
.tv_usec
+500)/1000;
252 int main(int argc
, char **argv
)
261 printf("----- libtest : mixed calls -------\n"), fflush(stdout
);
264 printf("\n(%u ms)\n", getclock_ms() - t
);
267 printf("----- libtest : threads ------------\n"), fflush(stdout
);
269 for (n
= 0; n
< M
; ++n
)
270 create_thread(thread_test_simple
, n
);
272 printf("\n(%u ms)\n", getclock_ms() - t
);
275 printf("----- libtest : tcc in threads -----\n"), fflush(stdout
);
277 for (n
= 0; n
< M
; ++n
)
278 create_thread(thread_test_complex
, n
);
280 printf("\n(%u ms)\n", getclock_ms() - t
);
283 printf("----- compilation of tcc -----------\n"), fflush(stdout
);
286 printf("(%u ms)\n", (getclock_ms() - t
) / 10), fflush(stdout
);
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));