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
34 #if ! _POSIX_THREAD_PROCESS_SHARED
36 puts ("_POSIX_THREAD_PROCESS_SHARED not supported, test skipped");
40 size_t ps
= sysconf (_SC_PAGESIZE
);
41 char tmpfname
[] = "/tmp/tst-barrier2.XXXXXX";
46 pthread_barrierattr_t a
;
53 fd
= mkstemp (tmpfname
);
56 printf ("cannot open temporary file: %m\n");
60 /* Make sure it is always removed. */
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
)
73 mem
= mmap (NULL
, ps
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
74 if (mem
== MAP_FAILED
)
76 printf ("mmap failed: %m\n");
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");
89 if (pthread_barrierattr_getpshared (&a
, &p
) != 0)
91 puts ("1st barrierattr_getpshared failed");
95 if (p
!= PTHREAD_PROCESS_PRIVATE
)
97 puts ("default pshared value wrong");
101 if (pthread_barrierattr_setpshared (&a
, PTHREAD_PROCESS_SHARED
) != 0)
103 puts ("barrierattr_setpshared failed");
107 if (pthread_barrierattr_getpshared (&a
, &p
) != 0)
109 puts ("2nd barrierattr_getpshared failed");
113 if (p
!= PTHREAD_PROCESS_SHARED
)
115 puts ("pshared value after setpshared call wrong");
119 if (pthread_barrier_init (b
, &a
, 2) != 0)
121 puts ("barrier_init failed");
125 if (pthread_barrierattr_destroy (&a
) != 0)
127 puts ("barrierattr_destroy failed");
131 puts ("going to fork now");
135 puts ("fork failed");
139 /* Just to be sure we don't hang forever. */
143 for (cnt
= 0; cnt
< N
; ++cnt
)
147 e
= pthread_barrier_wait (b
);
148 if (e
== PTHREAD_BARRIER_SERIAL_THREAD
)
152 printf ("%s: barrier_wait returned value %d != 0 and PTHREAD_BARRIER_SERIAL_THREAD\n",
153 pid
== 0 ? "child" : "parent", e
);
160 printf ("%s: was %d times the serial thread\n",
161 pid
== 0 ? "child" : "parent", serials
);
164 /* The child. Pass the number of times we had the serializing
165 thread back to the parent. */
168 if (waitpid (pid
, &status
, 0) != pid
)
170 puts ("waitpid failed");
174 if (!WIFEXITED (status
))
176 puts ("child exited abnormally");
180 if (WEXITSTATUS (status
) + serials
!= N
)
182 printf ("total number of serials is %d, expected %d\n",
183 WEXITSTATUS (status
) + serials
, N
);
191 #define TEST_FUNCTION do_test ()
192 #include "../test-skeleton.c"