Skip to content

Commit ccce28b

Browse files
committed
fix: use barrier instead of semaphore in thread termination tests
1 parent 70b8d50 commit ccce28b

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

core/iwasm/libraries/lib-wasi-threads/test/common.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <stdlib.h>
77
#include <stdio.h>
88
#include <assert.h>
9-
#include <semaphore.h>
9+
#include <pthread.h>
1010
#include <stdbool.h>
1111
#include <unistd.h>
1212

@@ -25,7 +25,7 @@ static blocking_task_type_t blocking_task_type;
2525

2626
#define TIMEOUT_SECONDS 10ll
2727
#define NUM_THREADS 3
28-
static sem_t sem;
28+
static pthread_barrier_t barrier;
2929

3030
typedef struct {
3131
start_args_t base;
@@ -51,17 +51,17 @@ run_long_task()
5151
void
5252
start_job()
5353
{
54-
sem_post(&sem);
55-
run_long_task(); /* Wait to be interrupted */
56-
assert(false && "Unreachable");
54+
/* Wait for all threads (including the main thread) to be ready */
55+
pthread_barrier_wait(&barrier);
56+
run_long_task(); /* Task to be interrupted */
57+
assert(false && "Thread termination test failed");
5758
}
5859

5960
void
6061
terminate_process()
6162
{
62-
/* Wait for all other threads (including main thread) to be ready */
63-
for (int i = 0; i < NUM_THREADS; i++)
64-
sem_wait(&sem);
63+
/* Wait for all threads (including the main thread) to be ready */
64+
pthread_barrier_wait(&barrier);
6565

6666
if (termination_by_trap)
6767
__builtin_trap();
@@ -91,7 +91,8 @@ test_termination(bool trap, bool main, blocking_task_type_t task_type)
9191

9292
int thread_id = -1, i;
9393
shared_t data[NUM_THREADS] = { 0 };
94-
assert(sem_init(&sem, 0, 0) == 0 && "Failed to init semaphore");
94+
assert(pthread_barrier_init(&barrier, NULL, NUM_THREADS + 1) == 0
95+
&& "Failed to init barrier");
9596

9697
for (i = 0; i < NUM_THREADS; i++) {
9798
/* No graceful memory free to simplify the test */

samples/wasi-threads/wasm-apps/thread_termination.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <stdlib.h>
1010
#include <stdio.h>
1111
#include <assert.h>
12-
#include <semaphore.h>
12+
#include <pthread.h>
1313
#include <stdbool.h>
1414
#include <unistd.h>
1515

@@ -26,7 +26,7 @@
2626

2727
#define TIMEOUT_SECONDS 10
2828
#define NUM_THREADS 3
29-
static sem_t sem;
29+
static pthread_barrier_t barrier;
3030

3131
typedef struct {
3232
start_args_t base;
@@ -49,18 +49,18 @@ run_long_task()
4949
void
5050
start_job()
5151
{
52-
sem_post(&sem);
53-
run_long_task(); /* Wait to be interrupted */
54-
assert(false && "Unreachable");
52+
/* Wait for all threads (including the main thread) to be ready */
53+
pthread_barrier_wait(&barrier);
54+
run_long_task(); /* Task to be interrupted */
55+
assert(false && "Thread termination test failed");
5556
}
5657

5758
void
5859
terminate_process()
5960
{
6061
/* Wait for all other threads (including main thread) to be ready */
6162
printf("Waiting before terminating\n");
62-
for (int i = 0; i < NUM_THREADS; i++)
63-
sem_wait(&sem);
63+
pthread_barrier_wait(&barrier);
6464

6565
printf("Force termination\n");
6666
#if TEST_TERMINATION_BY_TRAP == 1
@@ -91,8 +91,8 @@ main(int argc, char **argv)
9191
int thread_id = -1, i;
9292
shared_t data[NUM_THREADS] = { 0 };
9393

94-
if (sem_init(&sem, 0, 0) != 0) {
95-
printf("Failed to init semaphore\n");
94+
if (pthread_barrier_init(&barrier, NULL, NUM_THREADS + 1) != 0) {
95+
printf("Failed to init barrier\n");
9696
return EXIT_FAILURE;
9797
}
9898

0 commit comments

Comments
 (0)