Fix posix/tst-glob_lstat_compat on alpha [BZ #22818]
[glibc.git] / nptl / tst-kill6.c
blob37d321a1fc95760a4855d269b9b42b065d2c90f6
1 /* Copyright (C) 2003-2018 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/>. */
19 #include <errno.h>
20 #include <pthread.h>
21 #include <semaphore.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
28 static int do_test (void);
30 #define TEST_FUNCTION do_test ()
31 #include "../test-skeleton.c"
33 static pthread_t receiver;
34 static sem_t sem;
35 static pthread_barrier_t b;
37 static void
38 handler (int sig)
40 if (sig != SIGUSR1)
42 write_message ("wrong signal\n");
43 _exit (1);
46 if (pthread_self () != receiver)
48 write_message ("not the intended receiver\n");
49 _exit (1);
52 if (sem_post (&sem) != 0)
54 write_message ("sem_post failed\n");
55 _exit (1);
60 static void *
61 tf (void *a)
63 int e = pthread_barrier_wait (&b);
64 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
66 puts ("child: barrier_wait failed");
67 exit (1);
70 return NULL;
74 int
75 do_test (void)
77 struct sigaction sa;
78 sigemptyset (&sa.sa_mask);
79 sa.sa_flags = 0;
80 sa.sa_handler = handler;
81 if (sigaction (SIGUSR1, &sa, NULL) != 0)
83 puts ("sigaction failed");
84 exit (1);
87 #define N 20
89 if (pthread_barrier_init (&b, NULL, N + 1) != 0)
91 puts ("barrier_init failed");
92 exit (1);
95 pthread_attr_t a;
97 if (pthread_attr_init (&a) != 0)
99 puts ("attr_init failed");
100 exit (1);
103 if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
105 puts ("attr_setstacksize failed");
106 return 1;
109 pthread_t th[N];
110 int i;
111 for (i = 0; i < N; ++i)
112 if (pthread_create (&th[i], &a, tf, NULL) != 0)
114 puts ("create failed");
115 exit (1);
118 if (pthread_attr_destroy (&a) != 0)
120 puts ("attr_destroy failed");
121 exit (1);
124 if (sem_init (&sem, 0, 0) != 0)
126 puts ("sem_init failed");
127 exit (1);
130 for (i = 0; i < N * 10; ++i)
132 receiver = th[i % N];
134 if (pthread_kill (receiver, SIGUSR1) != 0)
136 puts ("kill failed");
137 exit (1);
140 if (TEMP_FAILURE_RETRY (sem_wait (&sem)) != 0)
142 puts ("sem_wait failed");
143 exit (1);
147 int e = pthread_barrier_wait (&b);
148 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
150 puts ("barrier_wait failed");
151 exit (1);
154 for (i = 0; i < N; ++i)
155 if (pthread_join (th[i], NULL) != 0)
157 puts ("join failed");
158 exit (1);
161 return 0;