Skip to content

Commit 9cf55f9

Browse files
authored
feat(wasi-threads): Change sample exception_propagation to thread_termination (bytecodealliance#1869)
Trigger wasi_proc_exit in the sample
1 parent c714189 commit 9cf55f9

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

samples/wasi-threads/wasm-apps/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ function (compile_sample SOURCE_FILE)
3636
endfunction ()
3737

3838
compile_sample(no_pthread.c wasi_thread_start.S)
39-
compile_sample(exception_propagation.c wasi_thread_start.S)
39+
compile_sample(thread_termination.c wasi_thread_start.S)

samples/wasi-threads/wasm-apps/exception_propagation.c renamed to samples/wasi-threads/wasm-apps/thread_termination.c

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
#include "wasi_thread_start.h"
1717

18+
#define TEST_TERMINATION_BY_TRAP 0 // Otherwise test `proc_exit` termination
19+
#define TEST_TERMINATION_IN_MAIN_THREAD 1
20+
1821
#define TIMEOUT_SECONDS 10
1922
#define NUM_THREADS 3
2023
static sem_t sem;
@@ -27,7 +30,7 @@ typedef struct {
2730
void
2831
run_long_task()
2932
{
30-
// Busy waiting to be interruptible by exception
33+
// Busy waiting to be interruptible by trap or `proc_exit`
3134
for (int i = 0; i < TIMEOUT_SECONDS; i++)
3235
sleep(1);
3336
}
@@ -39,18 +42,22 @@ __wasi_thread_start_C(int thread_id, int *start_arg)
3942

4043
if (data->throw_exception) {
4144
// Wait for all other threads (including main thread) to be ready
42-
printf("Waiting before throwing exception\n");
45+
printf("Waiting before terminating\n");
4346
for (int i = 0; i < NUM_THREADS; i++)
4447
sem_wait(&sem);
4548

46-
printf("Throwing exception\n");
49+
printf("Force termination\n");
50+
#if TEST_TERMINATION_BY_TRAP == 1
4751
__builtin_trap();
52+
#else
53+
__wasi_proc_exit(1);
54+
#endif
4855
}
4956
else {
5057
printf("Thread running\n");
5158

5259
sem_post(&sem);
53-
run_long_task(); // Wait to be interrupted by exception
60+
run_long_task(); // Wait to be interrupted
5461
assert(false && "Unreachable");
5562
}
5663
}
@@ -74,19 +81,26 @@ main(int argc, char **argv)
7481
}
7582
}
7683

77-
// Create a thread that throws an exception
84+
// Create a thread that forces termination through trap or `proc_exit`
85+
#if TEST_TERMINATION_IN_MAIN_THREAD == 1
86+
data[0].throw_exception = false;
87+
#else
7888
data[0].throw_exception = true;
89+
#endif
7990
thread_id = __wasi_thread_spawn(&data[0]);
8091
if (thread_id < 0) {
8192
printf("Failed to create thread: %d\n", thread_id);
8293
return EXIT_FAILURE;
8394
}
95+
8496
// Create two additional threads to test exception propagation
97+
data[1].throw_exception = false;
8598
thread_id = __wasi_thread_spawn(&data[1]);
8699
if (thread_id < 0) {
87100
printf("Failed to create thread: %d\n", thread_id);
88101
return EXIT_FAILURE;
89102
}
103+
data[2].throw_exception = false;
90104
thread_id = __wasi_thread_spawn(&data[2]);
91105
if (thread_id < 0) {
92106
printf("Failed to create thread: %d\n", thread_id);
@@ -96,8 +110,19 @@ main(int argc, char **argv)
96110
printf("Main thread running\n");
97111

98112
sem_post(&sem);
99-
run_long_task(); // Wait to be interrupted by exception
100-
assert(false && "Unreachable");
101113

114+
#if TEST_TERMINATION_IN_MAIN_THREAD == 1
115+
116+
printf("Force termination (main thread)\n");
117+
#if TEST_TERMINATION_BY_TRAP == 1
118+
__builtin_trap();
119+
#else /* TEST_TERMINATION_BY_TRAP */
120+
__wasi_proc_exit(1);
121+
#endif /* TEST_TERMINATION_BY_TRAP */
122+
123+
#else /* TEST_TERMINATION_IN_MAIN_THREAD */
124+
run_long_task(); // Wait to be interrupted
125+
assert(false && "Unreachable");
126+
#endif /* TEST_TERMINATION_IN_MAIN_THREAD */
102127
return EXIT_SUCCESS;
103128
}

0 commit comments

Comments
 (0)