1/* Tests process-shared barriers.
2 Copyright (C) 2002-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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 <stdint.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <sys/mman.h>
27#include <sys/wait.h>
28
29
30static int
31do_test (void)
32{
33 size_t ps = sysconf (_SC_PAGESIZE);
34 char tmpfname[] = "/tmp/tst-barrier2.XXXXXX";
35 char data[ps];
36 void *mem;
37 int fd;
38 pthread_barrier_t *b;
39 pthread_barrierattr_t a;
40 pid_t pid;
41 int serials = 0;
42 int cnt;
43 int status;
44 int p;
45
46 fd = mkstemp (template: tmpfname);
47 if (fd == -1)
48 {
49 printf (format: "cannot open temporary file: %m\n");
50 return 1;
51 }
52
53 /* Make sure it is always removed. */
54 unlink (name: tmpfname);
55
56 /* Create one page of data. */
57 memset (data, '\0', ps);
58
59 /* Write the data to the file. */
60 if (write (fd, data, ps) != (ssize_t) ps)
61 {
62 puts (s: "short write");
63 return 1;
64 }
65
66 mem = mmap (NULL, len: ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd: fd, offset: 0);
67 if (mem == MAP_FAILED)
68 {
69 printf (format: "mmap failed: %m\n");
70 return 1;
71 }
72
73 b = (pthread_barrier_t *) (((uintptr_t) mem + __alignof (pthread_barrier_t))
74 & ~(__alignof (pthread_barrier_t) - 1));
75
76 if (pthread_barrierattr_init (attr: &a) != 0)
77 {
78 puts (s: "barrierattr_init failed");
79 return 1;
80 }
81
82 if (pthread_barrierattr_getpshared (attr: &a, pshared: &p) != 0)
83 {
84 puts (s: "1st barrierattr_getpshared failed");
85 return 1;
86 }
87
88 if (p != PTHREAD_PROCESS_PRIVATE)
89 {
90 puts (s: "default pshared value wrong");
91 return 1;
92 }
93
94 if (pthread_barrierattr_setpshared (attr: &a, PTHREAD_PROCESS_SHARED) != 0)
95 {
96 puts (s: "barrierattr_setpshared failed");
97 return 1;
98 }
99
100 if (pthread_barrierattr_getpshared (attr: &a, pshared: &p) != 0)
101 {
102 puts (s: "2nd barrierattr_getpshared failed");
103 return 1;
104 }
105
106 if (p != PTHREAD_PROCESS_SHARED)
107 {
108 puts (s: "pshared value after setpshared call wrong");
109 return 1;
110 }
111
112 if (pthread_barrier_init (barrier: b, attr: &a, count: 2) != 0)
113 {
114 puts (s: "barrier_init failed");
115 return 1;
116 }
117
118 if (pthread_barrierattr_destroy (attr: &a) != 0)
119 {
120 puts (s: "barrierattr_destroy failed");
121 return 1;
122 }
123
124 puts (s: "going to fork now");
125 pid = fork ();
126 if (pid == -1)
127 {
128 puts (s: "fork failed");
129 return 1;
130 }
131
132 /* Just to be sure we don't hang forever. */
133 alarm (4);
134
135#define N 30
136 for (cnt = 0; cnt < N; ++cnt)
137 {
138 int e;
139
140 e = pthread_barrier_wait (barrier: b);
141 if (e == PTHREAD_BARRIER_SERIAL_THREAD)
142 ++serials;
143 else if (e != 0)
144 {
145 printf (format: "%s: barrier_wait returned value %d != 0 and PTHREAD_BARRIER_SERIAL_THREAD\n",
146 pid == 0 ? "child" : "parent", e);
147 return 1;
148 }
149 }
150
151 alarm (0);
152
153 printf (format: "%s: was %d times the serial thread\n",
154 pid == 0 ? "child" : "parent", serials);
155
156 if (pid == 0)
157 /* The child. Pass the number of times we had the serializing
158 thread back to the parent. */
159 exit (serials);
160
161 if (waitpid (pid: pid, stat_loc: &status, options: 0) != pid)
162 {
163 puts (s: "waitpid failed");
164 return 1;
165 }
166
167 if (!WIFEXITED (status))
168 {
169 puts (s: "child exited abnormally");
170 return 1;
171 }
172
173 if (WEXITSTATUS (status) + serials != N)
174 {
175 printf (format: "total number of serials is %d, expected %d\n",
176 WEXITSTATUS (status) + serials, N);
177 return 1;
178 }
179
180 return 0;
181}
182
183#define TEST_FUNCTION do_test ()
184#include "../test-skeleton.c"
185

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