1 /* Copyright (C) 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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
30 static char fname[] = "/tmp/tst-cond12-XXXXXX";
34 static void prepare (void);
35 #define PREPARE(argc, argv) prepare ()
37 static int do_test (void);
38 #define TEST_FUNCTION do_test ()
40 #include "../test-skeleton.c"
49 printf ("mkstemp failed: %m\n");
52 add_temp_file (fname);
53 if (ftruncate (fd, 1000) < 0)
55 printf ("ftruncate failed: %m\n");
69 } *p = mmap (NULL, sizeof (*p), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
72 printf ("initial mmap failed: %m\n");
76 pthread_mutexattr_t ma;
77 if (pthread_mutexattr_init (&ma) != 0)
79 puts ("mutexattr_init failed");
82 if (pthread_mutexattr_setpshared (&ma, 1) != 0)
84 puts ("mutexattr_setpshared failed");
87 if (pthread_mutex_init (&p->m, &ma) != 0)
89 puts ("mutex_init failed");
92 if (pthread_mutexattr_destroy (&ma) != 0)
94 puts ("mutexattr_destroy failed");
98 pthread_condattr_t ca;
99 if (pthread_condattr_init (&ca) != 0)
101 puts ("condattr_init failed");
104 if (pthread_condattr_setpshared (&ca, 1) != 0)
106 puts ("condattr_setpshared failed");
109 if (pthread_cond_init (&p->c, &ca) != 0)
111 puts ("mutex_init failed");
114 if (pthread_condattr_destroy (&ca) != 0)
116 puts ("condattr_destroy failed");
120 if (pthread_mutex_lock (&p->m) != 0)
122 puts ("initial mutex_lock failed");
131 printf ("fork failed: %m\n");
138 p = mmap (NULL, sizeof (*p), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
142 puts ("child: mapped to same address");
143 kill (getppid (), SIGKILL);
147 munmap (oldp, sizeof (*p));
149 if (pthread_mutex_lock (&p->m) != 0)
151 puts ("child: mutex_lock failed");
152 kill (getppid (), SIGKILL);
158 #ifndef USE_COND_SIGNAL
159 if (pthread_cond_broadcast (&p->c) != 0)
161 puts ("child: cond_broadcast failed");
162 kill (getppid (), SIGKILL);
166 if (pthread_cond_signal (&p->c) != 0)
168 puts ("child: cond_signal failed");
169 kill (getppid (), SIGKILL);
174 if (pthread_mutex_unlock (&p->m) != 0)
176 puts ("child: mutex_unlock failed");
177 kill (getppid (), SIGKILL);
185 pthread_cond_wait (&p->c, &p->m);
188 if (TEMP_FAILURE_RETRY (waitpid (pid, NULL, 0)) != pid)
190 printf ("waitpid failed: %m\n");