1 /* Copyright (C) 2003-2015 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, see
17 <http://www.gnu.org/licenses/>. */
29 static char fname
[] = "/tmp/tst-cond12-XXXXXX";
33 static void prepare (void);
34 #define PREPARE(argc, argv) prepare ()
36 static int do_test (void);
37 #define TEST_FUNCTION do_test ()
39 #include "../test-skeleton.c"
48 printf ("mkstemp failed: %m\n");
51 add_temp_file (fname
);
52 if (ftruncate (fd
, 1000) < 0)
54 printf ("ftruncate failed: %m\n");
68 } *p
= mmap (NULL
, sizeof (*p
), PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
71 printf ("initial mmap failed: %m\n");
75 pthread_mutexattr_t ma
;
76 if (pthread_mutexattr_init (&ma
) != 0)
78 puts ("mutexattr_init failed");
81 if (pthread_mutexattr_setpshared (&ma
, 1) != 0)
83 puts ("mutexattr_setpshared failed");
86 if (pthread_mutex_init (&p
->m
, &ma
) != 0)
88 puts ("mutex_init failed");
91 if (pthread_mutexattr_destroy (&ma
) != 0)
93 puts ("mutexattr_destroy failed");
97 pthread_condattr_t ca
;
98 if (pthread_condattr_init (&ca
) != 0)
100 puts ("condattr_init failed");
103 if (pthread_condattr_setpshared (&ca
, 1) != 0)
105 puts ("condattr_setpshared failed");
108 if (pthread_cond_init (&p
->c
, &ca
) != 0)
110 puts ("mutex_init failed");
113 if (pthread_condattr_destroy (&ca
) != 0)
115 puts ("condattr_destroy failed");
119 if (pthread_mutex_lock (&p
->m
) != 0)
121 puts ("initial mutex_lock failed");
130 printf ("fork failed: %m\n");
137 p
= mmap (NULL
, sizeof (*p
), PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
141 puts ("child: mapped to same address");
142 kill (getppid (), SIGKILL
);
146 munmap (oldp
, sizeof (*p
));
148 if (pthread_mutex_lock (&p
->m
) != 0)
150 puts ("child: mutex_lock failed");
151 kill (getppid (), SIGKILL
);
157 #ifndef USE_COND_SIGNAL
158 if (pthread_cond_broadcast (&p
->c
) != 0)
160 puts ("child: cond_broadcast failed");
161 kill (getppid (), SIGKILL
);
165 if (pthread_cond_signal (&p
->c
) != 0)
167 puts ("child: cond_signal failed");
168 kill (getppid (), SIGKILL
);
173 if (pthread_mutex_unlock (&p
->m
) != 0)
175 puts ("child: mutex_unlock failed");
176 kill (getppid (), SIGKILL
);
184 pthread_cond_wait (&p
->c
, &p
->m
);
187 if (TEMP_FAILURE_RETRY (waitpid (pid
, NULL
, 0)) != pid
)
189 printf ("waitpid failed: %m\n");