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
3030typedef struct {
3131 start_args_t base ;
@@ -51,17 +51,17 @@ run_long_task()
5151void
5252start_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
5960void
6061terminate_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 */
0 commit comments