1 /* Copyright (C) 2002-2013 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/>. */
33 size_t ps
= sysconf (_SC_PAGESIZE
);
34 char tmpfname
[] = "/tmp/tst-barrier2.XXXXXX";
39 pthread_barrierattr_t a
;
46 fd
= mkstemp (tmpfname
);
49 printf ("cannot open temporary file: %m\n");
53 /* Make sure it is always removed. */
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
)
66 mem
= mmap (NULL
, ps
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
67 if (mem
== MAP_FAILED
)
69 printf ("mmap failed: %m\n");
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");
82 if (pthread_barrierattr_getpshared (&a
, &p
) != 0)
84 puts ("1st barrierattr_getpshared failed");
88 if (p
!= PTHREAD_PROCESS_PRIVATE
)
90 puts ("default pshared value wrong");
94 if (pthread_barrierattr_setpshared (&a
, PTHREAD_PROCESS_SHARED
) != 0)
96 puts ("barrierattr_setpshared failed");
100 if (pthread_barrierattr_getpshared (&a
, &p
) != 0)
102 puts ("2nd barrierattr_getpshared failed");
106 if (p
!= PTHREAD_PROCESS_SHARED
)
108 puts ("pshared value after setpshared call wrong");
112 if (pthread_barrier_init (b
, &a
, 2) != 0)
114 puts ("barrier_init failed");
118 if (pthread_barrierattr_destroy (&a
) != 0)
120 puts ("barrierattr_destroy failed");
124 puts ("going to fork now");
128 puts ("fork failed");
132 /* Just to be sure we don't hang forever. */
136 for (cnt
= 0; cnt
< N
; ++cnt
)
140 e
= pthread_barrier_wait (b
);
141 if (e
== PTHREAD_BARRIER_SERIAL_THREAD
)
145 printf ("%s: barrier_wait returned value %d != 0 and PTHREAD_BARRIER_SERIAL_THREAD\n",
146 pid
== 0 ? "child" : "parent", e
);
153 printf ("%s: was %d times the serial thread\n",
154 pid
== 0 ? "child" : "parent", serials
);
157 /* The child. Pass the number of times we had the serializing
158 thread back to the parent. */
161 if (waitpid (pid
, &status
, 0) != pid
)
163 puts ("waitpid failed");
167 if (!WIFEXITED (status
))
169 puts ("child exited abnormally");
173 if (WEXITSTATUS (status
) + serials
!= N
)
175 printf ("total number of serials is %d, expected %d\n",
176 WEXITSTATUS (status
) + serials
, N
);
183 #define TEST_FUNCTION do_test ()
184 #include "../test-skeleton.c"