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