NPTL: swap comments for THREAD_SETMEM and THREAD_SETMEM_NC for i386 and x86_64
[glibc.git] / nptl / tst-barrier2.c
blobf78bfaf4ec4d1b2cb90bd42cd19ac3090be95c7c
1 /* Copyright (C) 2002-2015 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, see
17 <http://www.gnu.org/licenses/>. */
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>
30 static int
31 do_test (void)
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;
46 fd = mkstemp (tmpfname);
47 if (fd == -1)
49 printf ("cannot open temporary file: %m\n");
50 return 1;
53 /* Make sure it is always removed. */
54 unlink (tmpfname);
56 /* Create one page of data. */
57 memset (data, '\0', ps);
59 /* Write the data to the file. */
60 if (write (fd, data, ps) != (ssize_t) ps)
62 puts ("short write");
63 return 1;
66 mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
67 if (mem == MAP_FAILED)
69 printf ("mmap failed: %m\n");
70 return 1;
73 b = (pthread_barrier_t *) (((uintptr_t) mem + __alignof (pthread_barrier_t))
74 & ~(__alignof (pthread_barrier_t) - 1));
76 if (pthread_barrierattr_init (&a) != 0)
78 puts ("barrierattr_init failed");
79 return 1;
82 if (pthread_barrierattr_getpshared (&a, &p) != 0)
84 puts ("1st barrierattr_getpshared failed");
85 return 1;
88 if (p != PTHREAD_PROCESS_PRIVATE)
90 puts ("default pshared value wrong");
91 return 1;
94 if (pthread_barrierattr_setpshared (&a, PTHREAD_PROCESS_SHARED) != 0)
96 puts ("barrierattr_setpshared failed");
97 return 1;
100 if (pthread_barrierattr_getpshared (&a, &p) != 0)
102 puts ("2nd barrierattr_getpshared failed");
103 return 1;
106 if (p != PTHREAD_PROCESS_SHARED)
108 puts ("pshared value after setpshared call wrong");
109 return 1;
112 if (pthread_barrier_init (b, &a, 2) != 0)
114 puts ("barrier_init failed");
115 return 1;
118 if (pthread_barrierattr_destroy (&a) != 0)
120 puts ("barrierattr_destroy failed");
121 return 1;
124 puts ("going to fork now");
125 pid = fork ();
126 if (pid == -1)
128 puts ("fork failed");
129 return 1;
132 /* Just to be sure we don't hang forever. */
133 alarm (4);
135 #define N 30
136 for (cnt = 0; cnt < N; ++cnt)
138 int e;
140 e = pthread_barrier_wait (b);
141 if (e == PTHREAD_BARRIER_SERIAL_THREAD)
142 ++serials;
143 else if (e != 0)
145 printf ("%s: barrier_wait returned value %d != 0 and PTHREAD_BARRIER_SERIAL_THREAD\n",
146 pid == 0 ? "child" : "parent", e);
147 return 1;
151 alarm (0);
153 printf ("%s: was %d times the serial thread\n",
154 pid == 0 ? "child" : "parent", serials);
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);
161 if (waitpid (pid, &status, 0) != pid)
163 puts ("waitpid failed");
164 return 1;
167 if (!WIFEXITED (status))
169 puts ("child exited abnormally");
170 return 1;
173 if (WEXITSTATUS (status) + serials != N)
175 printf ("total number of serials is %d, expected %d\n",
176 WEXITSTATUS (status) + serials, N);
177 return 1;
180 return 0;
183 #define TEST_FUNCTION do_test ()
184 #include "../test-skeleton.c"