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 <pthread.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23
24
25static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
26static pthread_mutex_t mut = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
27static pthread_mutex_t mut2 = PTHREAD_MUTEX_INITIALIZER;
28
29static void *
30tf (void *p)
31{
32 if (pthread_mutex_lock (mutex: &mut) != 0)
33 {
34 printf (format: "%s: 1st mutex_lock failed\n", __func__);
35 exit (1);
36 }
37 if (pthread_mutex_lock (mutex: &mut) != 0)
38 {
39 printf (format: "%s: 2nd mutex_lock failed\n", __func__);
40 exit (1);
41 }
42 if (pthread_mutex_lock (mutex: &mut) != 0)
43 {
44 printf (format: "%s: 3rd mutex_lock failed\n", __func__);
45 exit (1);
46 }
47
48 if (pthread_mutex_unlock (mutex: &mut2) != 0)
49 {
50 printf (format: "%s: mutex_unlock failed\n", __func__);
51 exit (1);
52 }
53
54 if (pthread_cond_wait (cond: &cond, mutex: &mut) != 0)
55 {
56 printf (format: "%s: cond_wait failed\n", __func__);
57 exit (1);
58 }
59
60 puts (s: "child: done");
61
62 return NULL;
63}
64
65
66static int
67do_test (void)
68{
69 if (pthread_mutex_lock (mutex: &mut2) != 0)
70 {
71 puts (s: "1st mutex_lock failed");
72 return 1;
73 }
74
75 puts (s: "parent: create child");
76
77 pthread_t th;
78 int err = pthread_create (newthread: &th, NULL, start_routine: tf, NULL);
79 if (err != 0)
80 {
81 printf (format: "parent: cannot create thread: %s\n", strerror (errnum: err));
82 return 1;
83 }
84
85 /* We have to synchronize with the child. */
86 if (pthread_mutex_lock (mutex: &mut2) != 0)
87 {
88 puts (s: "2nd mutex_lock failed");
89 return 1;
90 }
91
92 /* Give the child to reach to pthread_cond_wait. */
93 sleep (seconds: 1);
94
95 if (pthread_cond_signal (cond: &cond) != 0)
96 {
97 puts (s: "cond_signal failed");
98 return 1;
99 }
100
101 err = pthread_join (th: th, NULL);
102 if (err != 0)
103 {
104 printf (format: "parent: failed to join: %s\n", strerror (errnum: err));
105 return 1;
106 }
107
108 puts (s: "done");
109
110 return 0;
111}
112
113
114#define TEST_FUNCTION do_test ()
115#include "../test-skeleton.c"
116

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