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 <errno.h> |
19 | #include <pthread.h> |
20 | #include <stdio.h> |
21 | #include <stdlib.h> |
22 | #include <string.h> |
23 | #include <time.h> |
24 | #include <sys/time.h> |
25 | |
26 | |
27 | static pthread_mutex_t mut; |
28 | static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; |
29 | |
30 | |
31 | static int |
32 | do_test (void) |
33 | { |
34 | pthread_mutexattr_t ma; |
35 | int err; |
36 | struct timespec ts; |
37 | struct timeval tv; |
38 | |
39 | if (pthread_mutexattr_init (attr: &ma) != 0) |
40 | { |
41 | puts (s: "mutexattr_init failed" ); |
42 | exit (1); |
43 | } |
44 | |
45 | if (pthread_mutexattr_settype (attr: &ma, kind: PTHREAD_MUTEX_ERRORCHECK) != 0) |
46 | { |
47 | puts (s: "mutexattr_settype failed" ); |
48 | exit (1); |
49 | } |
50 | |
51 | if (pthread_mutex_init (mutex: &mut, mutexattr: &ma) != 0) |
52 | { |
53 | puts (s: "mutex_init failed" ); |
54 | exit (1); |
55 | } |
56 | |
57 | /* Get the mutex. */ |
58 | if (pthread_mutex_lock (mutex: &mut) != 0) |
59 | { |
60 | puts (s: "mutex_lock failed" ); |
61 | exit (1); |
62 | } |
63 | |
64 | /* Waiting for the condition will fail. But we want the timeout here. */ |
65 | if (gettimeofday (tv: &tv, NULL) != 0) |
66 | { |
67 | puts (s: "gettimeofday failed" ); |
68 | exit (1); |
69 | } |
70 | |
71 | TIMEVAL_TO_TIMESPEC (&tv, &ts); |
72 | ts.tv_nsec += 500000000; |
73 | if (ts.tv_nsec >= 1000000000) |
74 | { |
75 | ts.tv_nsec -= 1000000000; |
76 | ++ts.tv_sec; |
77 | } |
78 | err = pthread_cond_timedwait (cond: &cond, mutex: &mut, abstime: &ts); |
79 | if (err == 0) |
80 | { |
81 | /* This could in theory happen but here without any signal and |
82 | additional waiter it should not. */ |
83 | puts (s: "cond_timedwait succeeded" ); |
84 | exit (1); |
85 | } |
86 | else if (err != ETIMEDOUT) |
87 | { |
88 | printf (format: "cond_timedwait returned with %s\n" , strerror (errnum: err)); |
89 | exit (1); |
90 | } |
91 | |
92 | err = pthread_mutex_unlock (mutex: &mut); |
93 | if (err != 0) |
94 | { |
95 | printf (format: "mutex_unlock failed: %s\n" , strerror (errnum: err)); |
96 | exit (1); |
97 | } |
98 | |
99 | return 0; |
100 | } |
101 | |
102 | |
103 | #define TEST_FUNCTION do_test () |
104 | #include "../test-skeleton.c" |
105 | |