1 /* Copyright (C) 2002, 2003, 2004 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
35 size_t ps
= sysconf (_SC_PAGESIZE
);
36 char tmpfname
[] = "/tmp/tst-cond4.XXXXXX";
40 pthread_mutexattr_t ma
;
41 pthread_mutex_t
*mut1
;
42 pthread_mutex_t
*mut2
;
43 pthread_condattr_t ca
;
49 fd
= mkstemp (tmpfname
);
52 printf ("cannot open temporary file: %m\n");
56 /* Make sure it is always removed. */
59 /* Create one page of data. */
60 memset (data
, '\0', ps
);
62 /* Write the data to the file. */
63 if (write (fd
, data
, ps
) != (ssize_t
) ps
)
69 mem
= mmap (NULL
, ps
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
70 if (mem
== MAP_FAILED
)
72 printf ("mmap failed: %m\n");
76 mut1
= (pthread_mutex_t
*) (((uintptr_t) mem
77 + __alignof (pthread_mutex_t
))
78 & ~(__alignof (pthread_mutex_t
) - 1));
81 cond
= (pthread_cond_t
*) (((uintptr_t) (mut2
+ 1)
82 + __alignof (pthread_cond_t
))
83 & ~(__alignof (pthread_cond_t
) - 1));
85 condition
= (int *) (((uintptr_t) (cond
+ 1) + __alignof (int))
86 & ~(__alignof (int) - 1));
88 if (pthread_mutexattr_init (&ma
) != 0)
90 puts ("mutexattr_init failed");
94 if (pthread_mutexattr_getpshared (&ma
, &p
) != 0)
96 puts ("1st mutexattr_getpshared failed");
100 if (p
!= PTHREAD_PROCESS_PRIVATE
)
102 puts ("default pshared value wrong");
106 if (pthread_mutexattr_setpshared (&ma
, PTHREAD_PROCESS_SHARED
) != 0)
108 puts ("mutexattr_setpshared failed");
112 if (pthread_mutexattr_getpshared (&ma
, &p
) != 0)
114 puts ("2nd mutexattr_getpshared failed");
118 if (p
!= PTHREAD_PROCESS_SHARED
)
120 puts ("pshared value after setpshared call wrong");
124 if (pthread_mutex_init (mut1
, &ma
) != 0)
126 puts ("1st mutex_init failed");
130 if (pthread_mutex_init (mut2
, &ma
) != 0)
132 puts ("2nd mutex_init failed");
136 if (pthread_condattr_init (&ca
) != 0)
138 puts ("condattr_init failed");
142 if (pthread_condattr_getpshared (&ca
, &p
) != 0)
144 puts ("1st condattr_getpshared failed");
148 if (p
!= PTHREAD_PROCESS_PRIVATE
)
150 puts ("default value for pshared in condattr wrong");
154 if (pthread_condattr_setpshared (&ca
, PTHREAD_PROCESS_SHARED
) != 0)
156 puts ("condattr_setpshared failed");
160 if (pthread_condattr_getpshared (&ca
, &p
) != 0)
162 puts ("2nd condattr_getpshared failed");
166 if (p
!= PTHREAD_PROCESS_SHARED
)
168 puts ("pshared condattr still not set");
172 if (pthread_cond_init (cond
, &ca
) != 0)
174 puts ("cond_init failed");
178 if (pthread_mutex_lock (mut1
) != 0)
180 puts ("parent: 1st mutex_lock failed");
184 puts ("going to fork now");
188 puts ("fork failed");
193 if (pthread_mutex_lock (mut2
) != 0)
195 puts ("child: mutex_lock failed");
199 if (pthread_mutex_unlock (mut1
) != 0)
201 puts ("child: 1st mutex_unlock failed");
206 if (pthread_cond_wait (cond
, mut2
) != 0)
208 puts ("child: cond_wait failed");
211 while (*condition
== 0);
213 if (pthread_mutex_unlock (mut2
) != 0)
215 puts ("child: 2nd mutex_unlock failed");
225 if (pthread_mutex_lock (mut1
) != 0)
227 puts ("parent: 2nd mutex_lock failed");
231 if (pthread_mutex_lock (mut2
) != 0)
233 puts ("parent: 3rd mutex_lock failed");
237 if (pthread_cond_signal (cond
) != 0)
239 puts ("parent: cond_signal failed");
245 if (pthread_mutex_unlock (mut2
) != 0)
247 puts ("parent: mutex_unlock failed");
251 puts ("waiting for child");
253 waitpid (pid
, &status
, 0);
256 puts ("parent done");
262 #define TEST_FUNCTION do_test ()
263 #include "../test-skeleton.c"