blob: ff904e4378ea39abc09dcf0a936b8003aca00b74 [file] [log] [blame]
xf.libdd93d52023-05-12 07:10:14 -07001/* Copyright (C) 2002-2016 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
26/* Note that this test requires more than the standard. It is
27 required that there are no spurious wakeups if only more readers
28 are added. This is a reasonable demand. */
29
30
31static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
32static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
33
34
35#define N 10
36
37
38static void *
39tf (void *arg)
40{
41 int i = (long int) arg;
42 int err;
43
44 /* Get the mutex. */
45 err = pthread_mutex_lock (&mut);
46 if (err != 0)
47 {
48 printf ("child %d mutex_lock failed: %s\n", i, strerror (err));
49 exit (1);
50 }
51
52 /* This call should never return. */
53 pthread_cond_wait (&cond, &mut);
54
55 /* We should never get here. */
56 exit (1);
57
58 return NULL;
59}
60
61
62static int
63do_test (void)
64{
65 int err;
66 int i;
67
68 for (i = 0; i < N; ++i)
69 {
70 pthread_t th;
71
72 if (i != 0)
73 {
74 /* Release the mutex. */
75 err = pthread_mutex_unlock (&mut);
76 if (err != 0)
77 {
78 printf ("mutex_unlock %d failed: %s\n", i, strerror (err));
79 return 1;
80 }
81 }
82
83 err = pthread_create (&th, NULL, tf, (void *) (long int) i);
84 if (err != 0)
85 {
86 printf ("create %d failed: %s\n", i, strerror (err));
87 return 1;
88 }
89
90 /* Get the mutex. */
91 err = pthread_mutex_lock (&mut);
92 if (err != 0)
93 {
94 printf ("mutex_lock %d failed: %s\n", i, strerror (err));
95 return 1;
96 }
97 }
98
99 /* Set an alarm for 1 second. The wrapper will expect this. */
100 alarm (1);
101
102 /* This call should never return. */
103 pthread_cond_wait (&cond, &mut);
104
105 puts ("cond_wait returned");
106 return 1;
107}
108
109
110#define EXPECTED_SIGNAL SIGALRM
111#define TEST_FUNCTION do_test ()
112#include "../test-skeleton.c"