1 | /* Test pthread_cond_clockwait timeout. |
2 | |
3 | Copyright (C) 2019-2024 Free Software Foundation, Inc. |
4 | This file is part of the GNU C Library. |
5 | |
6 | The GNU C Library is free software; you can redistribute it and/or |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either |
9 | version 2.1 of the License, or (at your option) any later version. |
10 | |
11 | The GNU C Library is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | Lesser General Public License for more details. |
15 | |
16 | You should have received a copy of the GNU Lesser General Public |
17 | License along with the GNU C Library; if not, see |
18 | <https://www.gnu.org/licenses/>. */ |
19 | |
20 | #include <errno.h> |
21 | #include <pthread.h> |
22 | #include <stdio.h> |
23 | #include <stdlib.h> |
24 | #include <string.h> |
25 | #include <time.h> |
26 | #include <sys/time.h> |
27 | #include <support/check.h> |
28 | #include <support/timespec.h> |
29 | #include <support/xthread.h> |
30 | |
31 | |
32 | static pthread_mutex_t mut = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP; |
33 | static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; |
34 | |
35 | |
36 | static int |
37 | do_test_clock (clockid_t clockid) |
38 | { |
39 | /* Get the mutex. */ |
40 | xpthread_mutex_lock (mutex: &mut); |
41 | |
42 | /* Waiting for the condition will fail. But we want the timeout here. */ |
43 | const struct timespec ts_now = xclock_now (clock: clockid); |
44 | const struct timespec ts_timeout = |
45 | timespec_add (ts_now, make_timespec (s: 0, ns: 500000000)); |
46 | |
47 | /* In theory pthread_cond_clockwait could return zero here due to |
48 | spurious wakeup. However that can't happen without a signal or an |
49 | additional waiter. */ |
50 | TEST_COMPARE (pthread_cond_clockwait (&cond, &mut, clockid, &ts_timeout), |
51 | ETIMEDOUT); |
52 | |
53 | xpthread_mutex_unlock (mutex: &mut); |
54 | |
55 | return 0; |
56 | } |
57 | |
58 | static int |
59 | do_test (void) |
60 | { |
61 | do_test_clock (CLOCK_MONOTONIC); |
62 | do_test_clock (CLOCK_REALTIME); |
63 | return 0; |
64 | } |
65 | |
66 | #include <support/test-driver.c> |
67 | |