1 /* Copyright (C) 2003-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
20 #include <semaphore.h>
27 static int do_test (void);
29 #define TEST_FUNCTION do_test ()
30 #include "../test-skeleton.c"
32 static pthread_t receiver
;
34 static pthread_barrier_t b
;
41 write_message ("wrong signal\n");
45 if (pthread_self () != receiver
)
47 write_message ("not the intended receiver\n");
51 if (sem_post (&sem
) != 0)
53 write_message ("sem_post failed\n");
62 int e
= pthread_barrier_wait (&b
);
63 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
65 puts ("child: barrier_wait failed");
77 sigemptyset (&sa
.sa_mask
);
79 sa
.sa_handler
= handler
;
80 if (sigaction (SIGUSR1
, &sa
, NULL
) != 0)
82 puts ("sigaction failed");
88 if (pthread_barrier_init (&b
, NULL
, N
+ 1) != 0)
90 puts ("barrier_init failed");
96 if (pthread_attr_init (&a
) != 0)
98 puts ("attr_init failed");
102 if (pthread_attr_setstacksize (&a
, 1 * 1024 * 1024) != 0)
104 puts ("attr_setstacksize failed");
110 for (i
= 0; i
< N
; ++i
)
111 if (pthread_create (&th
[i
], &a
, tf
, NULL
) != 0)
113 puts ("create failed");
117 if (pthread_attr_destroy (&a
) != 0)
119 puts ("attr_destroy failed");
123 if (sem_init (&sem
, 0, 0) != 0)
125 puts ("sem_init failed");
129 for (i
= 0; i
< N
* 10; ++i
)
131 receiver
= th
[i
% N
];
133 if (pthread_kill (receiver
, SIGUSR1
) != 0)
135 puts ("kill failed");
139 if (TEMP_FAILURE_RETRY (sem_wait (&sem
)) != 0)
141 puts ("sem_wait failed");
146 int e
= pthread_barrier_wait (&b
);
147 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
149 puts ("barrier_wait failed");
153 for (i
= 0; i
< N
; ++i
)
154 if (pthread_join (th
[i
], NULL
) != 0)
156 puts ("join failed");