15
15
16
16
#include "wasi_thread_start.h"
17
17
18
+ #define TEST_TERMINATION_BY_TRAP 0 // Otherwise test `proc_exit` termination
19
+ #define TEST_TERMINATION_IN_MAIN_THREAD 1
20
+
18
21
#define TIMEOUT_SECONDS 10
19
22
#define NUM_THREADS 3
20
23
static sem_t sem ;
@@ -27,7 +30,7 @@ typedef struct {
27
30
void
28
31
run_long_task ()
29
32
{
30
- // Busy waiting to be interruptible by exception
33
+ // Busy waiting to be interruptible by trap or `proc_exit`
31
34
for (int i = 0 ; i < TIMEOUT_SECONDS ; i ++ )
32
35
sleep (1 );
33
36
}
@@ -39,18 +42,22 @@ __wasi_thread_start_C(int thread_id, int *start_arg)
39
42
40
43
if (data -> throw_exception ) {
41
44
// Wait for all other threads (including main thread) to be ready
42
- printf ("Waiting before throwing exception \n" );
45
+ printf ("Waiting before terminating \n" );
43
46
for (int i = 0 ; i < NUM_THREADS ; i ++ )
44
47
sem_wait (& sem );
45
48
46
- printf ("Throwing exception\n" );
49
+ printf ("Force termination\n" );
50
+ #if TEST_TERMINATION_BY_TRAP == 1
47
51
__builtin_trap ();
52
+ #else
53
+ __wasi_proc_exit (1 );
54
+ #endif
48
55
}
49
56
else {
50
57
printf ("Thread running\n" );
51
58
52
59
sem_post (& sem );
53
- run_long_task (); // Wait to be interrupted by exception
60
+ run_long_task (); // Wait to be interrupted
54
61
assert (false && "Unreachable" );
55
62
}
56
63
}
@@ -74,19 +81,26 @@ main(int argc, char **argv)
74
81
}
75
82
}
76
83
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
78
88
data [0 ].throw_exception = true;
89
+ #endif
79
90
thread_id = __wasi_thread_spawn (& data [0 ]);
80
91
if (thread_id < 0 ) {
81
92
printf ("Failed to create thread: %d\n" , thread_id );
82
93
return EXIT_FAILURE ;
83
94
}
95
+
84
96
// Create two additional threads to test exception propagation
97
+ data [1 ].throw_exception = false;
85
98
thread_id = __wasi_thread_spawn (& data [1 ]);
86
99
if (thread_id < 0 ) {
87
100
printf ("Failed to create thread: %d\n" , thread_id );
88
101
return EXIT_FAILURE ;
89
102
}
103
+ data [2 ].throw_exception = false;
90
104
thread_id = __wasi_thread_spawn (& data [2 ]);
91
105
if (thread_id < 0 ) {
92
106
printf ("Failed to create thread: %d\n" , thread_id );
@@ -96,8 +110,19 @@ main(int argc, char **argv)
96
110
printf ("Main thread running\n" );
97
111
98
112
sem_post (& sem );
99
- run_long_task (); // Wait to be interrupted by exception
100
- assert (false && "Unreachable" );
101
113
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 */
102
127
return EXIT_SUCCESS ;
103
128
}
0 commit comments