]> git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/tst-kill6.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / nptl / tst-kill6.c
1 /* Copyright (C) 2003-2019 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
4
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.
9
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.
14
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 <https://www.gnu.org/licenses/>. */
18
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>
26
27
28 static int do_test (void);
29
30 #define TEST_FUNCTION do_test ()
31 #include "../test-skeleton.c"
32
33 static pthread_t receiver;
34 static sem_t sem;
35 static pthread_barrier_t b;
36
37 static void
38 handler (int sig)
39 {
40 if (sig != SIGUSR1)
41 {
42 write_message ("wrong signal\n");
43 _exit (1);
44 }
45
46 if (pthread_self () != receiver)
47 {
48 write_message ("not the intended receiver\n");
49 _exit (1);
50 }
51
52 if (sem_post (&sem) != 0)
53 {
54 write_message ("sem_post failed\n");
55 _exit (1);
56 }
57 }
58
59
60 static void *
61 tf (void *a)
62 {
63 int e = pthread_barrier_wait (&b);
64 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
65 {
66 puts ("child: barrier_wait failed");
67 exit (1);
68 }
69
70 return NULL;
71 }
72
73
74 int
75 do_test (void)
76 {
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)
82 {
83 puts ("sigaction failed");
84 exit (1);
85 }
86
87 #define N 20
88
89 if (pthread_barrier_init (&b, NULL, N + 1) != 0)
90 {
91 puts ("barrier_init failed");
92 exit (1);
93 }
94
95 pthread_attr_t a;
96
97 if (pthread_attr_init (&a) != 0)
98 {
99 puts ("attr_init failed");
100 exit (1);
101 }
102
103 if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
104 {
105 puts ("attr_setstacksize failed");
106 return 1;
107 }
108
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)
113 {
114 puts ("create failed");
115 exit (1);
116 }
117
118 if (pthread_attr_destroy (&a) != 0)
119 {
120 puts ("attr_destroy failed");
121 exit (1);
122 }
123
124 if (sem_init (&sem, 0, 0) != 0)
125 {
126 puts ("sem_init failed");
127 exit (1);
128 }
129
130 for (i = 0; i < N * 10; ++i)
131 {
132 receiver = th[i % N];
133
134 if (pthread_kill (receiver, SIGUSR1) != 0)
135 {
136 puts ("kill failed");
137 exit (1);
138 }
139
140 if (TEMP_FAILURE_RETRY (sem_wait (&sem)) != 0)
141 {
142 puts ("sem_wait failed");
143 exit (1);
144 }
145 }
146
147 int e = pthread_barrier_wait (&b);
148 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
149 {
150 puts ("barrier_wait failed");
151 exit (1);
152 }
153
154 for (i = 0; i < N; ++i)
155 if (pthread_join (th[i], NULL) != 0)
156 {
157 puts ("join failed");
158 exit (1);
159 }
160
161 return 0;
162 }