1/* Copyright (C) 2004-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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/>. */
17
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>
25
26
27static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
28static pthread_mutex_t mut = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
29static pthread_mutex_t mut2 = PTHREAD_MUTEX_INITIALIZER;
30
31static void *
32tf (void *p)
33{
34 if (pthread_mutex_lock (mutex: &mut) != 0)
35 {
36 printf (format: "%s: 1st mutex_lock failed\n", __func__);
37 exit (1);
38 }
39 if (pthread_mutex_lock (mutex: &mut) != 0)
40 {
41 printf (format: "%s: 2nd mutex_lock failed\n", __func__);
42 exit (1);
43 }
44 if (pthread_mutex_lock (mutex: &mut) != 0)
45 {
46 printf (format: "%s: 3rd mutex_lock failed\n", __func__);
47 exit (1);
48 }
49
50 if (pthread_mutex_unlock (mutex: &mut2) != 0)
51 {
52 printf (format: "%s: mutex_unlock failed\n", __func__);
53 exit (1);
54 }
55
56 struct timeval tv;
57 gettimeofday (tv: &tv, NULL);
58 struct timespec ts;
59 TIMEVAL_TO_TIMESPEC (&tv, &ts);
60 ts.tv_sec += p == NULL ? 100 : 1;
61
62 int err = pthread_cond_timedwait (cond: &cond, mutex: &mut, abstime: &ts);
63 if ((err != 0 && p == NULL) || (err != ETIMEDOUT && p != NULL))
64 {
65 printf (format: "%s: cond_wait failed\n", __func__);
66 exit (1);
67 }
68
69 if (pthread_mutex_unlock (mutex: &mut) != 0)
70 {
71 printf (format: "%s: 1st mutex_unlock failed\n", __func__);
72 exit (1);
73 }
74 if (pthread_mutex_unlock (mutex: &mut) != 0)
75 {
76 printf (format: "%s: 2nd mutex_unlock failed\n", __func__);
77 exit (1);
78 }
79 if (pthread_mutex_unlock (mutex: &mut) != 0)
80 {
81 printf (format: "%s: 3rd mutex_unlock failed\n", __func__);
82 exit (1);
83 }
84
85 puts (s: "child: done");
86
87 return NULL;
88}
89
90
91static int
92do_test (void)
93{
94 if (pthread_mutex_lock (mutex: &mut2) != 0)
95 {
96 puts (s: "1st mutex_lock failed");
97 return 1;
98 }
99
100 puts (s: "parent: create 1st child");
101
102 pthread_t th;
103 int err = pthread_create (newthread: &th, NULL, start_routine: tf, NULL);
104 if (err != 0)
105 {
106 printf (format: "parent: cannot 1st create thread: %s\n", strerror (errnum: err));
107 return 1;
108 }
109
110 /* We have to synchronize with the child. */
111 if (pthread_mutex_lock (mutex: &mut2) != 0)
112 {
113 puts (s: "2nd mutex_lock failed");
114 return 1;
115 }
116
117 /* Give the child to reach to pthread_cond_wait. */
118 sleep (seconds: 1);
119
120 if (pthread_cond_signal (cond: &cond) != 0)
121 {
122 puts (s: "cond_signal failed");
123 return 1;
124 }
125
126 err = pthread_join (th: th, NULL);
127 if (err != 0)
128 {
129 printf (format: "parent: failed to join: %s\n", strerror (errnum: err));
130 return 1;
131 }
132
133
134 puts (s: "parent: create 2nd child");
135
136 err = pthread_create (newthread: &th, NULL, start_routine: tf, arg: (void *) 1l);
137 if (err != 0)
138 {
139 printf (format: "parent: cannot 2nd create thread: %s\n", strerror (errnum: err));
140 return 1;
141 }
142
143 err = pthread_join (th: th, NULL);
144 if (err != 0)
145 {
146 printf (format: "parent: failed to join: %s\n", strerror (errnum: err));
147 return 1;
148 }
149
150 puts (s: "done");
151
152 return 0;
153}
154
155
156#define TEST_FUNCTION do_test ()
157#include "../test-skeleton.c"
158

source code of glibc/sysdeps/pthread/tst-cond15.c