1 /* Copyright (C) 2003-2022 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 <time.h>
23 #include <sys/time.h>
24
25
26 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
27 static pthread_mutex_t mut = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
28
29
30 static void *
tf(void * arg)31 tf (void *arg)
32 {
33 int err = pthread_cond_wait (&cond, &mut);
34 if (err == 0)
35 {
36 puts ("cond_wait did not fail");
37 exit (1);
38 }
39
40 if (err != EPERM)
41 {
42 printf ("cond_wait didn't return EPERM but %d\n", err);
43 exit (1);
44 }
45
46
47 /* Current time. */
48 struct timeval tv;
49 (void) gettimeofday (&tv, NULL);
50 /* +1000 seconds in correct format. */
51 struct timespec ts;
52 TIMEVAL_TO_TIMESPEC (&tv, &ts);
53 ts.tv_sec += 1000;
54
55 err = pthread_cond_timedwait (&cond, &mut, &ts);
56 if (err == 0)
57 {
58 puts ("cond_timedwait did not fail");
59 exit (1);
60 }
61
62 if (err != EPERM)
63 {
64 printf ("cond_timedwait didn't return EPERM but %d\n", err);
65 exit (1);
66 }
67
68 return (void *) 1l;
69 }
70
71
72 static int
do_test(void)73 do_test (void)
74 {
75 pthread_t th;
76 int err;
77
78 printf ("&cond = %p\n&mut = %p\n", &cond, &mut);
79
80 err = pthread_cond_wait (&cond, &mut);
81 if (err == 0)
82 {
83 puts ("cond_wait did not fail");
84 exit (1);
85 }
86
87 if (err != EPERM)
88 {
89 printf ("cond_wait didn't return EPERM but %d\n", err);
90 exit (1);
91 }
92
93
94 /* Current time. */
95 struct timeval tv;
96 (void) gettimeofday (&tv, NULL);
97 /* +1000 seconds in correct format. */
98 struct timespec ts;
99 TIMEVAL_TO_TIMESPEC (&tv, &ts);
100 ts.tv_sec += 1000;
101
102 err = pthread_cond_timedwait (&cond, &mut, &ts);
103 if (err == 0)
104 {
105 puts ("cond_timedwait did not fail");
106 exit (1);
107 }
108
109 if (err != EPERM)
110 {
111 printf ("cond_timedwait didn't return EPERM but %d\n", err);
112 exit (1);
113 }
114
115 if (pthread_mutex_lock (&mut) != 0)
116 {
117 puts ("parent: mutex_lock failed");
118 exit (1);
119 }
120
121 puts ("creating thread");
122
123 if (pthread_create (&th, NULL, tf, NULL) != 0)
124 {
125 puts ("create failed");
126 exit (1);
127 }
128
129 void *r;
130 if (pthread_join (th, &r) != 0)
131 {
132 puts ("join failed");
133 exit (1);
134 }
135 if (r != (void *) 1l)
136 {
137 puts ("thread has wrong return value");
138 exit (1);
139 }
140
141 puts ("done");
142
143 return 0;
144 }
145
146
147 #define TEST_FUNCTION do_test ()
148 #include "../test-skeleton.c"
149