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 tcc_define_symbol(s
, a
+2, NULL
);
113 TCCState
*new_state(int w
)
115 TCCState
*s
= tcc_new();
117 fprintf(stderr
, __FILE__
": could not create tcc state\n");
120 tcc_set_error_func(s
, stdout
, handle_error
);
122 if (!w
) tcc_set_options(s
, "-w");
123 tcc_set_output_type(s
, TCC_OUTPUT_MEMORY
);
127 void *reloc_state(TCCState
*s
, const char *entry
)
130 tcc_add_symbol(s
, "add", add
);
131 if (tcc_relocate(s
, TCC_RELOCATE_AUTO
) < 0) {
132 fprintf(stderr
, __FILE__
": could not relocate tcc state.\n");
135 func
= tcc_get_symbol(s
, entry
);
137 fprintf(stderr
, __FILE__
": could not get entry symbol.\n");
141 /* work with several states at the same time */
148 for (n
= 0; n
< M
+ 4; ++n
) {
149 unsigned a
= n
, b
= n
- 1, c
= n
- 2, d
= n
- 3, e
= n
- 4;
153 if (tcc_compile_string(s
[b
], my_program
) == -1)
156 func
[c
] = reloc_state(s
[c
], "foo");
157 if (d
< M
&& func
[d
])
165 /* simple compilation in threads */
166 TF_TYPE(thread_test_simple
, vn
)
175 ret
= tcc_compile_string(s
, my_program
);
178 func
= reloc_state(s
, "foo");
186 /* more complex compilation in threads */
187 TF_TYPE(thread_test_complex
, vn
)
192 char *argv
[30], b
[10];
195 sprintf(b
, "%d", F(n
));
197 for (i
= 1; i
< g_argc
; ++i
) argv
[argc
++] = g_argv
[i
];
199 argv
[argc
++] = "-run";
200 for (i
= 1; i
< g_argc
; ++i
) argv
[argc
++] = g_argv
[i
];
202 argv
[argc
++] = "-DFIB";
203 argv
[argc
++] = "-run";
204 argv
[argc
++] = __FILE__
;
210 ret
= tcc_add_file(s
, argv
[0]);
214 tcc_run(s
, argc
, argv
);
220 void time_tcc(int n
, const char *src
)
226 ret
= tcc_add_file(s
, src
);
233 static unsigned getclock_ms(void)
236 return GetTickCount();
239 gettimeofday(&tv
, NULL
);
240 return tv
.tv_sec
*1000 + (tv
.tv_usec
+500)/1000;
244 int main(int argc
, char **argv
)
253 fprintf(stderr
, "usage: libtcc_test_mt tcc.c <options>\n");
258 printf("running fib with mixed calls\n "), fflush(stdout
);
261 printf("\n (%u ms)\n", getclock_ms() - t
);
264 printf("running fib in threads\n "), fflush(stdout
);
266 for (n
= 0; n
< M
; ++n
)
267 create_thread(thread_test_simple
, n
);
269 printf("\n (%u ms)\n", getclock_ms() - t
);
272 printf("running tcc.c in threads to run fib\n "), fflush(stdout
);
274 for (n
= 0; n
< M
; ++n
)
275 create_thread(thread_test_complex
, n
);
277 printf("\n (%u ms)\n", getclock_ms() - t
);
280 printf("compiling tcc.c 10 times\n"), fflush(stdout
);
282 time_tcc(10, argv
[1]);
283 printf(" (%u ms)\n", getclock_ms() - t
), fflush(stdout
);
292 return (n
<= 2) ? 1 : fib(n
-1) + fib(n
-2);
295 int main(int argc
, char **argv
)
297 printf(" %d", fib(atoi(argv
[1]), 2));