]> git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/tst-cond3.c
c82b199860b70d5e65f81a075b964d10476f6be1
[thirdparty/glibc.git] / nptl / tst-cond3.c
1 /* Copyright (C) 2002-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
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.
9
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.
14
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/>. */
18
19 #include <pthread.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 static int do_test (void);
26
27 #define TEST_FUNCTION do_test ()
28 #include "../test-skeleton.c"
29
30 /* Note that this test requires more than the standard. It is
31 required that there are no spurious wakeups if only more readers
32 are added. This is a reasonable demand. */
33
34
35 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
36 static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
37
38
39 #define N 10
40
41
42 static void *
43 tf (void *arg)
44 {
45 int i = (long int) arg;
46 int err;
47
48 /* Get the mutex. */
49 err = pthread_mutex_lock (&mut);
50 if (err != 0)
51 {
52 printf ("child %d mutex_lock failed: %s\n", i, strerror (err));
53 exit (1);
54 }
55
56 /* This call should never return. */
57 xpthread_cond_wait (&cond, &mut);
58 puts ("error: pthread_cond_wait in tf returned");
59
60 /* We should never get here. */
61 exit (1);
62
63 return NULL;
64 }
65
66
67 static int
68 do_test (void)
69 {
70 int err;
71 int i;
72
73 for (i = 0; i < N; ++i)
74 {
75 pthread_t th;
76
77 if (i != 0)
78 {
79 /* Release the mutex. */
80 err = pthread_mutex_unlock (&mut);
81 if (err != 0)
82 {
83 printf ("mutex_unlock %d failed: %s\n", i, strerror (err));
84 return 1;
85 }
86 }
87
88 err = pthread_create (&th, NULL, tf, (void *) (long int) i);
89 if (err != 0)
90 {
91 printf ("create %d failed: %s\n", i, strerror (err));
92 return 1;
93 }
94
95 /* Get the mutex. */
96 err = pthread_mutex_lock (&mut);
97 if (err != 0)
98 {
99 printf ("mutex_lock %d failed: %s\n", i, strerror (err));
100 return 1;
101 }
102 }
103
104 delayed_exit (1);
105
106 /* This call should never return. */
107 xpthread_cond_wait (&cond, &mut);
108
109 puts ("error: pthread_cond_wait in do_test returned");
110 return 1;
111 }