Просмотр исходного кода

feat(wasi-threads): Change sample exception_propagation to thread_termination (#1869)

Trigger wasi_proc_exit in the sample
Enrico Loparco 3 лет назад
Родитель
Сommit
9cf55f953b

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

@@ -36,4 +36,4 @@ function (compile_sample SOURCE_FILE)
 endfunction ()
 
 compile_sample(no_pthread.c wasi_thread_start.S)
-compile_sample(exception_propagation.c wasi_thread_start.S)
+compile_sample(thread_termination.c wasi_thread_start.S)

+ 32 - 7
samples/wasi-threads/wasm-apps/exception_propagation.c → samples/wasi-threads/wasm-apps/thread_termination.c

@@ -15,6 +15,9 @@
 
 #include "wasi_thread_start.h"
 
+#define TEST_TERMINATION_BY_TRAP 0 // Otherwise test `proc_exit` termination
+#define TEST_TERMINATION_IN_MAIN_THREAD 1
+
 #define TIMEOUT_SECONDS 10
 #define NUM_THREADS 3
 static sem_t sem;
@@ -27,7 +30,7 @@ typedef struct {
 void
 run_long_task()
 {
-    // Busy waiting to be interruptible by exception
+    // Busy waiting to be interruptible by trap or `proc_exit`
     for (int i = 0; i < TIMEOUT_SECONDS; i++)
         sleep(1);
 }
@@ -39,18 +42,22 @@ __wasi_thread_start_C(int thread_id, int *start_arg)
 
     if (data->throw_exception) {
         // Wait for all other threads (including main thread) to be ready
-        printf("Waiting before throwing exception\n");
+        printf("Waiting before terminating\n");
         for (int i = 0; i < NUM_THREADS; i++)
             sem_wait(&sem);
 
-        printf("Throwing exception\n");
+        printf("Force termination\n");
+#if TEST_TERMINATION_BY_TRAP == 1
         __builtin_trap();
+#else
+        __wasi_proc_exit(1);
+#endif
     }
     else {
         printf("Thread running\n");
 
         sem_post(&sem);
-        run_long_task(); // Wait to be interrupted by exception
+        run_long_task(); // Wait to be interrupted
         assert(false && "Unreachable");
     }
 }
@@ -74,19 +81,26 @@ main(int argc, char **argv)
         }
     }
 
-    // Create a thread that throws an exception
+// Create a thread that forces termination through trap or `proc_exit`
+#if TEST_TERMINATION_IN_MAIN_THREAD == 1
+    data[0].throw_exception = false;
+#else
     data[0].throw_exception = true;
+#endif
     thread_id = __wasi_thread_spawn(&data[0]);
     if (thread_id < 0) {
         printf("Failed to create thread: %d\n", thread_id);
         return EXIT_FAILURE;
     }
+
     // Create two additional threads to test exception propagation
+    data[1].throw_exception = false;
     thread_id = __wasi_thread_spawn(&data[1]);
     if (thread_id < 0) {
         printf("Failed to create thread: %d\n", thread_id);
         return EXIT_FAILURE;
     }
+    data[2].throw_exception = false;
     thread_id = __wasi_thread_spawn(&data[2]);
     if (thread_id < 0) {
         printf("Failed to create thread: %d\n", thread_id);
@@ -96,8 +110,19 @@ main(int argc, char **argv)
     printf("Main thread running\n");
 
     sem_post(&sem);
-    run_long_task(); // Wait to be interrupted by exception
-    assert(false && "Unreachable");
 
+#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");
+#endif /* TEST_TERMINATION_IN_MAIN_THREAD */
     return EXIT_SUCCESS;
 }