mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-02-11 09:25:20 +00:00
Fix thread termination example (#1915)
The example wasn't fully implemented the intention - it didn't work as expected when the trap/proc_exit was executed on the main thread, because main thread never waited for all the threads to start.
This commit is contained in:
parent
9cf55f953b
commit
879563047f
|
@ -35,30 +35,42 @@ run_long_task()
|
|||
sleep(1);
|
||||
}
|
||||
|
||||
void
|
||||
start_job()
|
||||
{
|
||||
sem_post(&sem);
|
||||
run_long_task(); // Wait to be interrupted
|
||||
assert(false && "Unreachable");
|
||||
}
|
||||
|
||||
void
|
||||
terminate_process()
|
||||
{
|
||||
// Wait for all other threads (including main thread) to be ready
|
||||
printf("Waiting before terminating\n");
|
||||
for (int i = 0; i < NUM_THREADS; i++)
|
||||
sem_wait(&sem);
|
||||
|
||||
printf("Force termination\n");
|
||||
#if TEST_TERMINATION_BY_TRAP == 1
|
||||
__builtin_trap();
|
||||
#else
|
||||
__wasi_proc_exit(1);
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
__wasi_thread_start_C(int thread_id, int *start_arg)
|
||||
{
|
||||
shared_t *data = (shared_t *)start_arg;
|
||||
|
||||
if (data->throw_exception) {
|
||||
// Wait for all other threads (including main thread) to be ready
|
||||
printf("Waiting before terminating\n");
|
||||
for (int i = 0; i < NUM_THREADS; i++)
|
||||
sem_wait(&sem);
|
||||
|
||||
printf("Force termination\n");
|
||||
#if TEST_TERMINATION_BY_TRAP == 1
|
||||
__builtin_trap();
|
||||
#else
|
||||
__wasi_proc_exit(1);
|
||||
#endif
|
||||
terminate_process();
|
||||
}
|
||||
else {
|
||||
printf("Thread running\n");
|
||||
|
||||
sem_post(&sem);
|
||||
run_long_task(); // Wait to be interrupted
|
||||
assert(false && "Unreachable");
|
||||
start_job();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -107,22 +119,13 @@ main(int argc, char **argv)
|
|||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
#if TEST_TERMINATION_IN_MAIN_THREAD == 1
|
||||
printf("Force termination (main thread)\n");
|
||||
terminate_process();
|
||||
#else /* TEST_TERMINATION_IN_MAIN_THREAD */
|
||||
printf("Main thread running\n");
|
||||
|
||||
sem_post(&sem);
|
||||
|
||||
#if TEST_TERMINATION_IN_MAIN_THREAD == 1
|
||||
|
||||
printf("Force termination (main thread)\n");
|
||||
#if TEST_TERMINATION_BY_TRAP == 1
|
||||
__builtin_trap();
|
||||
#else /* TEST_TERMINATION_BY_TRAP */
|
||||
__wasi_proc_exit(1);
|
||||
#endif /* TEST_TERMINATION_BY_TRAP */
|
||||
|
||||
#else /* TEST_TERMINATION_IN_MAIN_THREAD */
|
||||
run_long_task(); // Wait to be interrupted
|
||||
assert(false && "Unreachable");
|
||||
start_job();
|
||||
#endif /* TEST_TERMINATION_IN_MAIN_THREAD */
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Loading…
Reference in New Issue
Block a user