Update copyright dates with scripts/update-copyrights
[glibc.git] / sysdeps / pthread / tst-kill6.c
blob3098280531eee1a4b78c4270cf861b00c7d13b8a
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/>. */
18 #include <errno.h>
19 #include <pthread.h>
20 #include <semaphore.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
27 static int do_test (void);
29 #define TEST_FUNCTION do_test ()
30 #include "../test-skeleton.c"
32 static pthread_t receiver;
33 static sem_t sem;
34 static pthread_barrier_t b;
36 static void
37 handler (int sig)
39 if (sig != SIGUSR1)
41 write_message ("wrong signal\n");
42 _exit (1);
45 if (pthread_self () != receiver)
47 write_message ("not the intended receiver\n");
48 _exit (1);
51 if (sem_post (&sem) != 0)
53 write_message ("sem_post failed\n");
54 _exit (1);
59 static void *
60 tf (void *a)
62 int e = pthread_barrier_wait (&b);
63 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
65 puts ("child: barrier_wait failed");
66 exit (1);
69 return NULL;
73 int
74 do_test (void)
76 struct sigaction sa;
77 sigemptyset (&sa.sa_mask);
78 sa.sa_flags = 0;
79 sa.sa_handler = handler;
80 if (sigaction (SIGUSR1, &sa, NULL) != 0)
82 puts ("sigaction failed");
83 exit (1);
86 #define N 20
88 if (pthread_barrier_init (&b, NULL, N + 1) != 0)
90 puts ("barrier_init failed");
91 exit (1);
94 pthread_attr_t a;
96 if (pthread_attr_init (&a) != 0)
98 puts ("attr_init failed");
99 exit (1);
102 if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
104 puts ("attr_setstacksize failed");
105 return 1;
108 pthread_t th[N];
109 int i;
110 for (i = 0; i < N; ++i)
111 if (pthread_create (&th[i], &a, tf, NULL) != 0)
113 puts ("create failed");
114 exit (1);
117 if (pthread_attr_destroy (&a) != 0)
119 puts ("attr_destroy failed");
120 exit (1);
123 if (sem_init (&sem, 0, 0) != 0)
125 puts ("sem_init failed");
126 exit (1);
129 for (i = 0; i < N * 10; ++i)
131 receiver = th[i % N];
133 if (pthread_kill (receiver, SIGUSR1) != 0)
135 puts ("kill failed");
136 exit (1);
139 if (TEMP_FAILURE_RETRY (sem_wait (&sem)) != 0)
141 puts ("sem_wait failed");
142 exit (1);
146 int e = pthread_barrier_wait (&b);
147 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
149 puts ("barrier_wait failed");
150 exit (1);
153 for (i = 0; i < N; ++i)
154 if (pthread_join (th[i], NULL) != 0)
156 puts ("join failed");
157 exit (1);
160 return 0;