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
18+ #define BUSY_WAIT 0
19+ #define ATOMIC_WAIT 1
20+ #define POLL_ONEOFF 2
21+
22+ /* Change parameters here to modify the sample behavior */
23+ #define TEST_TERMINATION_BY_TRAP 0 /* Otherwise `proc_exit` termination */
24+ #define TEST_TERMINATION_IN_MAIN_THREAD 1 /* Otherwise in spawn thread */
25+ #define LONG_TASK_IMPL ATOMIC_WAIT
2026
2127#define TIMEOUT_SECONDS 10
2228#define NUM_THREADS 3
@@ -30,23 +36,28 @@ typedef struct {
3036void
3137run_long_task ()
3238{
33- // Busy waiting to be interruptible by trap or `proc_exit`
39+ #if LONG_TASK_IMPL == BUSY_WAIT
3440 for (int i = 0 ; i < TIMEOUT_SECONDS ; i ++ )
3541 sleep (1 );
42+ #elif LONG_TASK_IMPL == ATOMIC_WAIT
43+ __builtin_wasm_memory_atomic_wait32 (0 , 0 , -1 );
44+ #else
45+ sleep (TIMEOUT_SECONDS );
46+ #endif
3647}
3748
3849void
3950start_job ()
4051{
4152 sem_post (& sem );
42- run_long_task (); // Wait to be interrupted
53+ run_long_task (); /* Wait to be interrupted */
4354 assert (false && "Unreachable" );
4455}
4556
4657void
4758terminate_process ()
4859{
49- // Wait for all other threads (including main thread) to be ready
60+ /* Wait for all other threads (including main thread) to be ready */
5061 printf ("Waiting before terminating\n" );
5162 for (int i = 0 ; i < NUM_THREADS ; i ++ )
5263 sem_wait (& sem );
@@ -55,7 +66,7 @@ terminate_process()
5566#if TEST_TERMINATION_BY_TRAP == 1
5667 __builtin_trap ();
5768#else
58- __wasi_proc_exit (1 );
69+ __wasi_proc_exit (33 );
5970#endif
6071}
6172
@@ -86,14 +97,14 @@ main(int argc, char **argv)
8697 }
8798
8899 for (i = 0 ; i < NUM_THREADS ; i ++ ) {
89- // No graceful memory free to simplify the example
100+ /* No graceful memory free to simplify the example */
90101 if (!start_args_init (& data [i ].base )) {
91102 printf ("Failed to allocate thread's stack\n" );
92103 return EXIT_FAILURE ;
93104 }
94105 }
95106
96- // Create a thread that forces termination through trap or `proc_exit`
107+ /* Create a thread that forces termination through trap or `proc_exit` */
97108#if TEST_TERMINATION_IN_MAIN_THREAD == 1
98109 data [0 ].throw_exception = false;
99110#else
@@ -105,7 +116,7 @@ main(int argc, char **argv)
105116 return EXIT_FAILURE ;
106117 }
107118
108- // Create two additional threads to test exception propagation
119+ /* Create two additional threads to test exception propagation */
109120 data [1 ].throw_exception = false;
110121 thread_id = __wasi_thread_spawn (& data [1 ]);
111122 if (thread_id < 0 ) {
@@ -128,4 +139,4 @@ main(int argc, char **argv)
128139 start_job ();
129140#endif /* TEST_TERMINATION_IN_MAIN_THREAD */
130141 return EXIT_SUCCESS ;
131- }
142+ }
0 commit comments