1 /* Copyright (C) 2003-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
21 #include <semaphore.h>
28 static int do_test (void);
30 #define TEST_FUNCTION do_test ()
31 #include "../test-skeleton.c"
33 static pthread_t receiver
;
35 static pthread_barrier_t b
;
42 write_message ("wrong signal\n");
46 if (pthread_self () != receiver
)
48 write_message ("not the intended receiver\n");
52 if (sem_post (&sem
) != 0)
54 write_message ("sem_post failed\n");
63 int e
= pthread_barrier_wait (&b
);
64 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
66 puts ("child: barrier_wait failed");
78 sigemptyset (&sa
.sa_mask
);
80 sa
.sa_handler
= handler
;
81 if (sigaction (SIGUSR1
, &sa
, NULL
) != 0)
83 puts ("sigaction failed");
89 if (pthread_barrier_init (&b
, NULL
, N
+ 1) != 0)
91 puts ("barrier_init failed");
97 if (pthread_attr_init (&a
) != 0)
99 puts ("attr_init failed");
103 if (pthread_attr_setstacksize (&a
, 1 * 1024 * 1024) != 0)
105 puts ("attr_setstacksize failed");
111 for (i
= 0; i
< N
; ++i
)
112 if (pthread_create (&th
[i
], &a
, tf
, NULL
) != 0)
114 puts ("create failed");
118 if (pthread_attr_destroy (&a
) != 0)
120 puts ("attr_destroy failed");
124 if (sem_init (&sem
, 0, 0) != 0)
126 puts ("sem_init failed");
130 for (i
= 0; i
< N
* 10; ++i
)
132 receiver
= th
[i
% N
];
134 if (pthread_kill (receiver
, SIGUSR1
) != 0)
136 puts ("kill failed");
140 if (TEMP_FAILURE_RETRY (sem_wait (&sem
)) != 0)
142 puts ("sem_wait failed");
147 int e
= pthread_barrier_wait (&b
);
148 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
150 puts ("barrier_wait failed");
154 for (i
= 0; i
< N
; ++i
)
155 if (pthread_join (th
[i
], NULL
) != 0)
157 puts ("join failed");