|
@@ -9,15 +9,21 @@
|
|
|
#include <stdlib.h>
|
|
#include <stdlib.h>
|
|
|
#include <stdio.h>
|
|
#include <stdio.h>
|
|
|
#include <assert.h>
|
|
#include <assert.h>
|
|
|
-#include <wasi/api.h>
|
|
|
|
|
#include <semaphore.h>
|
|
#include <semaphore.h>
|
|
|
#include <stdbool.h>
|
|
#include <stdbool.h>
|
|
|
#include <unistd.h>
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
+#include "wasi_thread_start.h"
|
|
|
|
|
+
|
|
|
#define TIMEOUT_SECONDS 10
|
|
#define TIMEOUT_SECONDS 10
|
|
|
#define NUM_THREADS 3
|
|
#define NUM_THREADS 3
|
|
|
static sem_t sem;
|
|
static sem_t sem;
|
|
|
|
|
|
|
|
|
|
+typedef struct {
|
|
|
|
|
+ start_args_t base;
|
|
|
|
|
+ bool throw_exception;
|
|
|
|
|
+} shared_t;
|
|
|
|
|
+
|
|
|
void
|
|
void
|
|
|
run_long_task()
|
|
run_long_task()
|
|
|
{
|
|
{
|
|
@@ -26,12 +32,12 @@ run_long_task()
|
|
|
sleep(1);
|
|
sleep(1);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-__attribute__((export_name("wasi_thread_start"))) void
|
|
|
|
|
-wasi_thread_start(int thread_id, int *start_arg)
|
|
|
|
|
|
|
+void
|
|
|
|
|
+__wasi_thread_start_C(int thread_id, int *start_arg)
|
|
|
{
|
|
{
|
|
|
- bool has_to_throw_exception = (bool)start_arg;
|
|
|
|
|
|
|
+ shared_t *data = (shared_t *)start_arg;
|
|
|
|
|
|
|
|
- if (has_to_throw_exception) {
|
|
|
|
|
|
|
+ if (data->throw_exception) {
|
|
|
// Wait for all other threads (including main thread) to be ready
|
|
// Wait for all other threads (including main thread) to be ready
|
|
|
printf("Waiting before throwing exception\n");
|
|
printf("Waiting before throwing exception\n");
|
|
|
for (int i = 0; i < NUM_THREADS; i++)
|
|
for (int i = 0; i < NUM_THREADS; i++)
|
|
@@ -52,26 +58,36 @@ wasi_thread_start(int thread_id, int *start_arg)
|
|
|
int
|
|
int
|
|
|
main(int argc, char **argv)
|
|
main(int argc, char **argv)
|
|
|
{
|
|
{
|
|
|
- int thread_id = -1;
|
|
|
|
|
|
|
+ int thread_id = -1, i;
|
|
|
|
|
+ shared_t data[NUM_THREADS] = { 0 };
|
|
|
|
|
+
|
|
|
if (sem_init(&sem, 0, 0) != 0) {
|
|
if (sem_init(&sem, 0, 0) != 0) {
|
|
|
printf("Failed to init semaphore\n");
|
|
printf("Failed to init semaphore\n");
|
|
|
return EXIT_FAILURE;
|
|
return EXIT_FAILURE;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ for (i = 0; i < NUM_THREADS; i++) {
|
|
|
|
|
+ // No graceful memory free to simplify the example
|
|
|
|
|
+ if (!start_args_init(&data[i].base)) {
|
|
|
|
|
+ printf("Failed to allocate thread's stack\n");
|
|
|
|
|
+ return EXIT_FAILURE;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// Create a thread that throws an exception
|
|
// Create a thread that throws an exception
|
|
|
- thread_id = __wasi_thread_spawn((void *)true);
|
|
|
|
|
|
|
+ data[0].throw_exception = true;
|
|
|
|
|
+ thread_id = __wasi_thread_spawn(&data[0]);
|
|
|
if (thread_id < 0) {
|
|
if (thread_id < 0) {
|
|
|
printf("Failed to create thread: %d\n", thread_id);
|
|
printf("Failed to create thread: %d\n", thread_id);
|
|
|
return EXIT_FAILURE;
|
|
return EXIT_FAILURE;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
// Create two additional threads to test exception propagation
|
|
// Create two additional threads to test exception propagation
|
|
|
- thread_id = __wasi_thread_spawn((void *)false);
|
|
|
|
|
|
|
+ thread_id = __wasi_thread_spawn(&data[1]);
|
|
|
if (thread_id < 0) {
|
|
if (thread_id < 0) {
|
|
|
printf("Failed to create thread: %d\n", thread_id);
|
|
printf("Failed to create thread: %d\n", thread_id);
|
|
|
return EXIT_FAILURE;
|
|
return EXIT_FAILURE;
|
|
|
}
|
|
}
|
|
|
- thread_id = __wasi_thread_spawn((void *)false);
|
|
|
|
|
|
|
+ thread_id = __wasi_thread_spawn(&data[2]);
|
|
|
if (thread_id < 0) {
|
|
if (thread_id < 0) {
|
|
|
printf("Failed to create thread: %d\n", thread_id);
|
|
printf("Failed to create thread: %d\n", thread_id);
|
|
|
return EXIT_FAILURE;
|
|
return EXIT_FAILURE;
|