diff --git a/core/iwasm/libraries/lib-wasi-threads/test/common.h b/core/iwasm/libraries/lib-wasi-threads/test/common.h index 86188cc3b2..a531e39dc4 100644 --- a/core/iwasm/libraries/lib-wasi-threads/test/common.h +++ b/core/iwasm/libraries/lib-wasi-threads/test/common.h @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include @@ -25,7 +25,7 @@ static blocking_task_type_t blocking_task_type; #define TIMEOUT_SECONDS 10ll #define NUM_THREADS 3 -static sem_t sem; +static pthread_barrier_t barrier; typedef struct { start_args_t base; @@ -51,17 +51,17 @@ run_long_task() void start_job() { - sem_post(&sem); - run_long_task(); /* Wait to be interrupted */ - assert(false && "Unreachable"); + /* Wait for all threads (including the main thread) to be ready */ + pthread_barrier_wait(&barrier); + run_long_task(); /* Task to be interrupted */ + assert(false && "Thread termination test failed"); } void terminate_process() { - /* Wait for all other threads (including main thread) to be ready */ - for (int i = 0; i < NUM_THREADS; i++) - sem_wait(&sem); + /* Wait for all threads (including the main thread) to be ready */ + pthread_barrier_wait(&barrier); if (termination_by_trap) __builtin_trap(); @@ -91,7 +91,8 @@ test_termination(bool trap, bool main, blocking_task_type_t task_type) int thread_id = -1, i; shared_t data[NUM_THREADS] = { 0 }; - assert(sem_init(&sem, 0, 0) == 0 && "Failed to init semaphore"); + assert(pthread_barrier_init(&barrier, NULL, NUM_THREADS + 1) == 0 + && "Failed to init barrier"); for (i = 0; i < NUM_THREADS; i++) { /* No graceful memory free to simplify the test */ diff --git a/samples/wasi-threads/wasm-apps/thread_termination.c b/samples/wasi-threads/wasm-apps/thread_termination.c index 355bb3f453..9f5cf1fe89 100644 --- a/samples/wasi-threads/wasm-apps/thread_termination.c +++ b/samples/wasi-threads/wasm-apps/thread_termination.c @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include @@ -26,7 +26,7 @@ #define TIMEOUT_SECONDS 10 #define NUM_THREADS 3 -static sem_t sem; +static pthread_barrier_t barrier; typedef struct { start_args_t base; @@ -49,9 +49,10 @@ run_long_task() void start_job() { - sem_post(&sem); - run_long_task(); /* Wait to be interrupted */ - assert(false && "Unreachable"); + /* Wait for all threads (including the main thread) to be ready */ + pthread_barrier_wait(&barrier); + run_long_task(); /* Task to be interrupted */ + assert(false && "Thread termination test failed"); } void @@ -59,8 +60,7 @@ terminate_process() { /* Wait for all other threads (including main thread) to be ready */ printf("Waiting before terminating\n"); - for (int i = 0; i < NUM_THREADS; i++) - sem_wait(&sem); + pthread_barrier_wait(&barrier); printf("Force termination\n"); #if TEST_TERMINATION_BY_TRAP == 1 @@ -91,8 +91,8 @@ main(int argc, char **argv) int thread_id = -1, i; shared_t data[NUM_THREADS] = { 0 }; - if (sem_init(&sem, 0, 0) != 0) { - printf("Failed to init semaphore\n"); + if (pthread_barrier_init(&barrier, NULL, NUM_THREADS + 1) != 0) { + printf("Failed to init barrier\n"); return EXIT_FAILURE; }