History

Fix libtcc_test_mt

The test failed because atexit/on_exit code was not correct. Make it look more like the g_exit_context. Added a sleep to libtcc_test_mt to allow overlap of threads to detect this problem.

herman ten brugge committed 3 years ago

Commit 85b27431 parent e2db3a52 files · +39 −21Browse files

Unified · Split

2 files
  1. tccrun.c +34 −20
  2. tests/libtcc_test_mt.c +5 −1
tccrun.c+34−20
@@ -147,31 +147,41 @@ static void run_cdtors(TCCState *s1, const char *start, const char *end, · 146 unmodified lines
147147
148148 #define NR_AT_EXIT 32
149149
150−static int exit_called = 0;
151−static int nr_exit = 0;
152−static void (*exitfunc[NR_AT_EXIT])(int, void *);
153−static void *exitarg[NR_AT_EXIT];
150+static struct exit_context {
151+ int exit_called;
152+ int nr_exit;
153+ void (*exitfunc[NR_AT_EXIT])(int, void *);
154+ void *exitarg[NR_AT_EXIT];
154155 #ifndef CONFIG_TCC_BACKTRACE
155−static jmp_buf run_jmp_buf;
156+ jmp_buf run_jmp_buf;
156157 #endif
158+} g_exit_context;
157159
158160 static void init_exit(void)
159161 {
160− exit_called = 0;
161− nr_exit = 0;
162+ struct exit_context *e = &g_exit_context;
163+
164+ e->exit_called = 0;
165+ e->nr_exit = 0;
162166 }
163167
164168 static void call_exit(int ret)
165169 {
166− while (nr_exit--)
167− exitfunc[nr_exit](ret, exitarg[nr_exit]);
170+ struct exit_context *e = &g_exit_context;
171+
172+ while (e->nr_exit) {
173+ e->nr_exit--;
174+ e->exitfunc[e->nr_exit](ret, e->exitarg[e->nr_exit]);
175+ }
168176 }
169177
170178 static int rt_atexit(void (*function)(void))
171179 {
172− if (nr_exit < NR_AT_EXIT) {
173− exitfunc[nr_exit] = (void (*)(int, void *))function;
174− exitarg[nr_exit++] = NULL;
180+ struct exit_context *e = &g_exit_context;
181+
182+ if (e->nr_exit < NR_AT_EXIT) {
183+ e->exitfunc[e->nr_exit] = (void (*)(int, void *))function;
184+ e->exitarg[e->nr_exit++] = NULL;
175185 return 0;
176186 }
177187 return 1;
@@ -179,9 +189,11 @@ static int rt_atexit(void (*function)(void)) · 1 unmodified line
179189
180190 static int rt_on_exit(void (*function)(int, void *), void *arg)
181191 {
182− if (nr_exit < NR_AT_EXIT) {
183− exitfunc[nr_exit] = function;
184− exitarg[nr_exit++] = arg;
192+ struct exit_context *e = &g_exit_context;
193+
194+ if (e->nr_exit < NR_AT_EXIT) {
195+ e->exitfunc[e->nr_exit] = function;
196+ e->exitarg[e->nr_exit++] = arg;
185197 return 0;
186198 }
187199 return 1;
@@ -189,11 +201,13 @@ static int rt_on_exit(void (*function)(int, void *), void *arg) · 1 unmodified line
189201
190202 static void run_exit(int code)
191203 {
192− exit_called = 1;
204+ struct exit_context *e = &g_exit_context;
205+
206+ e->exit_called = 1;
193207 #ifdef CONFIG_TCC_BACKTRACE
194− longjmp(g_rtctxt.jmp_buf, code ? code : 256);
208+ longjmp((&g_rtctxt)->jmp_buf, code ? code : 256);
195209 #else
196− longjmp(run_jmp_buf, code ? code : 256);
210+ longjmp(e->run_jmp_buf, code ? code : 256);
197211 #endif
198212 }
199213
@@ -277,7 +291,7 @@ LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv) · 77 unmodified lines
277291 #ifdef CONFIG_TCC_BACKTRACE
278292 if (!(ret = setjmp(rc->jmp_buf)))
279293 #else
280− if (!(ret = setjmp(run_jmp_buf)))
294+ if (!(ret = setjmp((&g_exit_context)->run_jmp_buf)))
281295 #endif
282296 {
283297 ret = prog_main(argc, argv, envp);
@@ -286,7 +300,7 @@ LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv) · 2 unmodified lines
286300 call_exit(ret);
287301 if ((s1->dflag & 16) && ret)
288302 fprintf(s1->ppfp, "[returns %d]\n", ret), fflush(s1->ppfp);
289− if ((s1->dflag & 16) == 0 && exit_called)
303+ if ((s1->dflag & 16) == 0 && (&g_exit_context)->exit_called)
290304 exit(ret);
291305 return ret;
292306 }
tests/libtcc_test_mt.c+5−1
@@ -288,6 +288,9 @@ int main(int argc, char **argv) · 287 unmodified lines
288288
289289 #else
290290 #include <tcclib.h>
291+
292+unsigned int sleep(unsigned int seconds);
293+
291294 int fib(n)
292295 {
293296 return (n <= 2) ? 1 : fib(n-1) + fib(n-2);
@@ -295,7 +298,8 @@ int fib(n) · 1 unmodified line
295298
296299 int main(int argc, char **argv)
297300 {
298− printf(" %d", fib(atoi(argv[1]), 2));
301+ sleep(1);
302+ printf(" %d", fib(atoi(argv[1])));
299303 return 0;
300304 }
301305 #endif

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t