1/* Copyright (C) 2003-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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/>. */
17
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>
25
26
27static int do_test (void);
28
29#define TEST_FUNCTION do_test ()
30#include "../test-skeleton.c"
31
32static pthread_t receiver;
33static sem_t sem;
34static pthread_barrier_t b;
35
36static void
37handler (int sig)
38{
39 if (sig != SIGUSR1)
40 {
41 write_message (message: "wrong signal\n");
42 _exit (1);
43 }
44
45 if (pthread_self () != receiver)
46 {
47 write_message (message: "not the intended receiver\n");
48 _exit (1);
49 }
50
51 if (sem_post (sem: &sem) != 0)
52 {
53 write_message (message: "sem_post failed\n");
54 _exit (1);
55 }
56}
57
58
59static void *
60tf (void *a)
61{
62 int e = pthread_barrier_wait (barrier: &b);
63 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
64 {
65 puts (s: "child: barrier_wait failed");
66 exit (1);
67 }
68
69 return NULL;
70}
71
72
73int
74do_test (void)
75{
76 struct sigaction sa;
77 sigemptyset (&sa.sa_mask);
78 sa.sa_flags = 0;
79 sa.sa_handler = handler;
80 if (sigaction (SIGUSR1, act: &sa, NULL) != 0)
81 {
82 puts (s: "sigaction failed");
83 exit (1);
84 }
85
86#define N 20
87
88 if (pthread_barrier_init (barrier: &b, NULL, N + 1) != 0)
89 {
90 puts (s: "barrier_init failed");
91 exit (1);
92 }
93
94 pthread_attr_t a;
95
96 if (pthread_attr_init (attr: &a) != 0)
97 {
98 puts (s: "attr_init failed");
99 exit (1);
100 }
101
102 if (pthread_attr_setstacksize (attr: &a, stacksize: 1 * 1024 * 1024) != 0)
103 {
104 puts (s: "attr_setstacksize failed");
105 return 1;
106 }
107
108 pthread_t th[N];
109 int i;
110 for (i = 0; i < N; ++i)
111 if (pthread_create (newthread: &th[i], attr: &a, start_routine: tf, NULL) != 0)
112 {
113 puts (s: "create failed");
114 exit (1);
115 }
116
117 if (pthread_attr_destroy (attr: &a) != 0)
118 {
119 puts (s: "attr_destroy failed");
120 exit (1);
121 }
122
123 if (sem_init (sem: &sem, pshared: 0, value: 0) != 0)
124 {
125 puts (s: "sem_init failed");
126 exit (1);
127 }
128
129 for (i = 0; i < N * 10; ++i)
130 {
131 receiver = th[i % N];
132
133 if (pthread_kill (threadid: receiver, SIGUSR1) != 0)
134 {
135 puts (s: "kill failed");
136 exit (1);
137 }
138
139 if (TEMP_FAILURE_RETRY (sem_wait (&sem)) != 0)
140 {
141 puts (s: "sem_wait failed");
142 exit (1);
143 }
144 }
145
146 int e = pthread_barrier_wait (barrier: &b);
147 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
148 {
149 puts (s: "barrier_wait failed");
150 exit (1);
151 }
152
153 for (i = 0; i < N; ++i)
154 if (pthread_join (th: th[i], NULL) != 0)
155 {
156 puts (s: "join failed");
157 exit (1);
158 }
159
160 return 0;
161}
162

source code of glibc/sysdeps/pthread/tst-kill6.c