Update syscall lists for Linux 6.5
[glibc.git] / sysdeps / pthread / tst-cond15.c
blob1386297abef6566b3c45304249708a899099c5a9
1 /* Copyright (C) 2004-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/>. */
18 #include <errno.h>
19 #include <pthread.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/time.h>
27 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
28 static pthread_mutex_t mut = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
29 static pthread_mutex_t mut2 = PTHREAD_MUTEX_INITIALIZER;
31 static void *
32 tf (void *p)
34 if (pthread_mutex_lock (&mut) != 0)
36 printf ("%s: 1st mutex_lock failed\n", __func__);
37 exit (1);
39 if (pthread_mutex_lock (&mut) != 0)
41 printf ("%s: 2nd mutex_lock failed\n", __func__);
42 exit (1);
44 if (pthread_mutex_lock (&mut) != 0)
46 printf ("%s: 3rd mutex_lock failed\n", __func__);
47 exit (1);
50 if (pthread_mutex_unlock (&mut2) != 0)
52 printf ("%s: mutex_unlock failed\n", __func__);
53 exit (1);
56 struct timeval tv;
57 gettimeofday (&tv, NULL);
58 struct timespec ts;
59 TIMEVAL_TO_TIMESPEC (&tv, &ts);
60 ts.tv_sec += p == NULL ? 100 : 1;
62 int err = pthread_cond_timedwait (&cond, &mut, &ts);
63 if ((err != 0 && p == NULL) || (err != ETIMEDOUT && p != NULL))
65 printf ("%s: cond_wait failed\n", __func__);
66 exit (1);
69 if (pthread_mutex_unlock (&mut) != 0)
71 printf ("%s: 1st mutex_unlock failed\n", __func__);
72 exit (1);
74 if (pthread_mutex_unlock (&mut) != 0)
76 printf ("%s: 2nd mutex_unlock failed\n", __func__);
77 exit (1);
79 if (pthread_mutex_unlock (&mut) != 0)
81 printf ("%s: 3rd mutex_unlock failed\n", __func__);
82 exit (1);
85 puts ("child: done");
87 return NULL;
91 static int
92 do_test (void)
94 if (pthread_mutex_lock (&mut2) != 0)
96 puts ("1st mutex_lock failed");
97 return 1;
100 puts ("parent: create 1st child");
102 pthread_t th;
103 int err = pthread_create (&th, NULL, tf, NULL);
104 if (err != 0)
106 printf ("parent: cannot 1st create thread: %s\n", strerror (err));
107 return 1;
110 /* We have to synchronize with the child. */
111 if (pthread_mutex_lock (&mut2) != 0)
113 puts ("2nd mutex_lock failed");
114 return 1;
117 /* Give the child to reach to pthread_cond_wait. */
118 sleep (1);
120 if (pthread_cond_signal (&cond) != 0)
122 puts ("cond_signal failed");
123 return 1;
126 err = pthread_join (th, NULL);
127 if (err != 0)
129 printf ("parent: failed to join: %s\n", strerror (err));
130 return 1;
134 puts ("parent: create 2nd child");
136 err = pthread_create (&th, NULL, tf, (void *) 1l);
137 if (err != 0)
139 printf ("parent: cannot 2nd create thread: %s\n", strerror (err));
140 return 1;
143 err = pthread_join (th, NULL);
144 if (err != 0)
146 printf ("parent: failed to join: %s\n", strerror (err));
147 return 1;
150 puts ("done");
152 return 0;
156 #define TEST_FUNCTION do_test ()
157 #include "../test-skeleton.c"