1 /* Copyright (C) 2003-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
28 static char fname
[] = "/tmp/tst-cond12-XXXXXX";
32 static void prepare (void);
33 #define PREPARE(argc, argv) prepare ()
35 static int do_test (void);
36 #define TEST_FUNCTION do_test ()
38 #include "../test-skeleton.c"
47 printf ("mkstemp failed: %m\n");
50 add_temp_file (fname
);
51 if (ftruncate (fd
, 1000) < 0)
53 printf ("ftruncate failed: %m\n");
67 } *p
= mmap (NULL
, sizeof (*p
), PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
70 printf ("initial mmap failed: %m\n");
74 pthread_mutexattr_t ma
;
75 if (pthread_mutexattr_init (&ma
) != 0)
77 puts ("mutexattr_init failed");
80 if (pthread_mutexattr_setpshared (&ma
, 1) != 0)
82 puts ("mutexattr_setpshared failed");
85 if (pthread_mutex_init (&p
->m
, &ma
) != 0)
87 puts ("mutex_init failed");
90 if (pthread_mutexattr_destroy (&ma
) != 0)
92 puts ("mutexattr_destroy failed");
96 pthread_condattr_t ca
;
97 if (pthread_condattr_init (&ca
) != 0)
99 puts ("condattr_init failed");
102 if (pthread_condattr_setpshared (&ca
, 1) != 0)
104 puts ("condattr_setpshared failed");
107 if (pthread_cond_init (&p
->c
, &ca
) != 0)
109 puts ("mutex_init failed");
112 if (pthread_condattr_destroy (&ca
) != 0)
114 puts ("condattr_destroy failed");
118 if (pthread_mutex_lock (&p
->m
) != 0)
120 puts ("initial mutex_lock failed");
129 printf ("fork failed: %m\n");
136 p
= mmap (NULL
, sizeof (*p
), PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
140 puts ("child: mapped to same address");
141 kill (getppid (), SIGKILL
);
145 munmap (oldp
, sizeof (*p
));
147 if (pthread_mutex_lock (&p
->m
) != 0)
149 puts ("child: mutex_lock failed");
150 kill (getppid (), SIGKILL
);
156 #ifndef USE_COND_SIGNAL
157 if (pthread_cond_broadcast (&p
->c
) != 0)
159 puts ("child: cond_broadcast failed");
160 kill (getppid (), SIGKILL
);
164 if (pthread_cond_signal (&p
->c
) != 0)
166 puts ("child: cond_signal failed");
167 kill (getppid (), SIGKILL
);
172 if (pthread_mutex_unlock (&p
->m
) != 0)
174 puts ("child: mutex_unlock failed");
175 kill (getppid (), SIGKILL
);
183 pthread_cond_wait (&p
->c
, &p
->m
);
186 if (TEMP_FAILURE_RETRY (waitpid (pid
, NULL
, 0)) != pid
)
188 printf ("waitpid failed: %m\n");