Update.
[glibc.git] / nptl / tst-barrier2.c
blobb147ae1bfb0ebf801c493fc64123779105a42f3f
1 /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <pthread.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/mman.h>
28 #include <sys/wait.h>
31 static int
32 do_test (void)
34 #if ! _POSIX_THREAD_PROCESS_SHARED
36 puts ("_POSIX_THREAD_PROCESS_SHARED not supported, test skipped");
38 #else
40 size_t ps = sysconf (_SC_PAGESIZE);
41 char tmpfname[] = "/tmp/tst-barrier2.XXXXXX";
42 char data[ps];
43 void *mem;
44 int fd;
45 pthread_barrier_t *b;
46 pthread_barrierattr_t a;
47 pid_t pid;
48 int serials = 0;
49 int cnt;
50 int status;
51 int p;
53 fd = mkstemp (tmpfname);
54 if (fd == -1)
56 printf ("cannot open temporary file: %m\n");
57 return 1;
60 /* Make sure it is always removed. */
61 unlink (tmpfname);
63 /* Create one page of data. */
64 memset (data, '\0', ps);
66 /* Write the data to the file. */
67 if (write (fd, data, ps) != (ssize_t) ps)
69 puts ("short write");
70 return 1;
73 mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
74 if (mem == MAP_FAILED)
76 printf ("mmap failed: %m\n");
77 return 1;
80 b = (pthread_barrier_t *) (((uintptr_t) mem + __alignof (pthread_barrier_t))
81 & ~(__alignof (pthread_barrier_t) - 1));
83 if (pthread_barrierattr_init (&a) != 0)
85 puts ("barrierattr_init failed");
86 return 1;
89 if (pthread_barrierattr_getpshared (&a, &p) != 0)
91 puts ("1st barrierattr_getpshared failed");
92 return 1;
95 if (p != PTHREAD_PROCESS_PRIVATE)
97 puts ("default pshared value wrong");
98 return 1;
101 if (pthread_barrierattr_setpshared (&a, PTHREAD_PROCESS_SHARED) != 0)
103 puts ("barrierattr_setpshared failed");
104 return 1;
107 if (pthread_barrierattr_getpshared (&a, &p) != 0)
109 puts ("2nd barrierattr_getpshared failed");
110 return 1;
113 if (p != PTHREAD_PROCESS_SHARED)
115 puts ("pshared value after setpshared call wrong");
116 return 1;
119 if (pthread_barrier_init (b, &a, 2) != 0)
121 puts ("barrier_init failed");
122 return 1;
125 if (pthread_barrierattr_destroy (&a) != 0)
127 puts ("barrierattr_destroy failed");
128 return 1;
131 puts ("going to fork now");
132 pid = fork ();
133 if (pid == -1)
135 puts ("fork failed");
136 return 1;
139 /* Just to be sure we don't hang forever. */
140 alarm (4);
142 #define N 30
143 for (cnt = 0; cnt < N; ++cnt)
145 int e;
147 e = pthread_barrier_wait (b);
148 if (e == PTHREAD_BARRIER_SERIAL_THREAD)
149 ++serials;
150 else if (e != 0)
152 printf ("%s: barrier_wait returned value %d != 0 and PTHREAD_BARRIER_SERIAL_THREAD\n",
153 pid == 0 ? "child" : "parent", e);
154 return 1;
158 alarm (0);
160 printf ("%s: was %d times the serial thread\n",
161 pid == 0 ? "child" : "parent", serials);
163 if (pid == 0)
164 /* The child. Pass the number of times we had the serializing
165 thread back to the parent. */
166 exit (serials);
168 if (waitpid (pid, &status, 0) != pid)
170 puts ("waitpid failed");
171 return 1;
174 if (!WIFEXITED (status))
176 puts ("child exited abnormally");
177 return 1;
180 if (WEXITSTATUS (status) + serials != N)
182 printf ("total number of serials is %d, expected %d\n",
183 WEXITSTATUS (status) + serials, N);
184 return 1;
186 #endif
188 return 0;
191 #define TEST_FUNCTION do_test ()
192 #include "../test-skeleton.c"