]>
Commit | Line | Data |
---|---|---|
6d7e8eda | 1 | /* Copyright (C) 2003-2023 Free Software Foundation, Inc. |
604b15cf | 2 | This file is part of the GNU C Library. |
604b15cf UD |
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 | |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
604b15cf UD |
17 | |
18 | #include <errno.h> | |
19 | #include <pthread.h> | |
20 | #include <stdio.h> | |
21 | #include <time.h> | |
22 | #include <unistd.h> | |
d8e0b901 | 23 | #include <support/check.h> |
afe4de7d | 24 | #include <support/test-driver.h> |
d8e0b901 MC |
25 | #include <support/timespec.h> |
26 | #include <support/xthread.h> | |
27 | #include <support/xtime.h> | |
604b15cf | 28 | |
afe4de7d MC |
29 | /* A bogus clock value that tells run_test to use pthread_cond_timedwait |
30 | rather than pthread_condclockwait. */ | |
31 | #define CLOCK_USE_ATTR_CLOCK (-1) | |
604b15cf | 32 | |
f38a3086 | 33 | #if defined _POSIX_CLOCK_SELECTION && _POSIX_CLOCK_SELECTION >= 0 |
604b15cf | 34 | static int |
afe4de7d | 35 | run_test (clockid_t attr_clock, clockid_t wait_clock) |
604b15cf UD |
36 | { |
37 | pthread_condattr_t condattr; | |
38 | pthread_cond_t cond; | |
39 | pthread_mutexattr_t mutattr; | |
40 | pthread_mutex_t mut; | |
41 | ||
afe4de7d | 42 | verbose_printf ("attr_clock = %d\n", (int) attr_clock); |
604b15cf | 43 | |
d8e0b901 | 44 | TEST_COMPARE (pthread_condattr_init (&condattr), 0); |
afe4de7d | 45 | TEST_COMPARE (pthread_condattr_setclock (&condattr, attr_clock), 0); |
604b15cf | 46 | |
afe4de7d MC |
47 | clockid_t attr_clock_read; |
48 | TEST_COMPARE (pthread_condattr_getclock (&condattr, &attr_clock_read), 0); | |
49 | TEST_COMPARE (attr_clock, attr_clock_read); | |
d8e0b901 MC |
50 | |
51 | TEST_COMPARE (pthread_cond_init (&cond, &condattr), 0); | |
52 | TEST_COMPARE (pthread_condattr_destroy (&condattr), 0); | |
53 | ||
54 | xpthread_mutexattr_init (&mutattr); | |
55 | xpthread_mutexattr_settype (&mutattr, PTHREAD_MUTEX_ERRORCHECK); | |
56 | xpthread_mutex_init (&mut, &mutattr); | |
57 | xpthread_mutexattr_destroy (&mutattr); | |
58 | ||
59 | xpthread_mutex_lock (&mut); | |
60 | TEST_COMPARE (pthread_mutex_lock (&mut), EDEADLK); | |
61 | ||
62 | struct timespec ts_timeout; | |
afe4de7d MC |
63 | xclock_gettime (wait_clock == CLOCK_USE_ATTR_CLOCK ? attr_clock : wait_clock, |
64 | &ts_timeout); | |
604b15cf UD |
65 | |
66 | /* Wait one second. */ | |
d8e0b901 MC |
67 | ++ts_timeout.tv_sec; |
68 | ||
afe4de7d MC |
69 | if (wait_clock == CLOCK_USE_ATTR_CLOCK) { |
70 | TEST_COMPARE (pthread_cond_timedwait (&cond, &mut, &ts_timeout), ETIMEDOUT); | |
71 | TEST_TIMESPEC_BEFORE_NOW (ts_timeout, attr_clock); | |
72 | } else { | |
73 | TEST_COMPARE (pthread_cond_clockwait (&cond, &mut, wait_clock, &ts_timeout), | |
74 | ETIMEDOUT); | |
75 | TEST_TIMESPEC_BEFORE_NOW (ts_timeout, wait_clock); | |
76 | } | |
d8e0b901 MC |
77 | |
78 | xpthread_mutex_unlock (&mut); | |
79 | xpthread_mutex_destroy (&mut); | |
80 | TEST_COMPARE (pthread_cond_destroy (&cond), 0); | |
604b15cf UD |
81 | |
82 | return 0; | |
83 | } | |
b910f788 | 84 | #endif |
604b15cf UD |
85 | |
86 | ||
87 | static int | |
88 | do_test (void) | |
89 | { | |
e4bb4853 | 90 | #if !defined _POSIX_CLOCK_SELECTION || _POSIX_CLOCK_SELECTION == -1 |
b910f788 | 91 | |
d8e0b901 | 92 | FAIL_UNSUPPORTED ("_POSIX_CLOCK_SELECTION not supported, test skipped"); |
b910f788 RM |
93 | |
94 | #else | |
95 | ||
afe4de7d | 96 | run_test (CLOCK_REALTIME, CLOCK_USE_ATTR_CLOCK); |
604b15cf | 97 | |
e4bb4853 | 98 | # if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0 |
b910f788 | 99 | # if _POSIX_MONOTONIC_CLOCK == 0 |
604b15cf UD |
100 | int e = sysconf (_SC_MONOTONIC_CLOCK); |
101 | if (e < 0) | |
102 | puts ("CLOCK_MONOTONIC not supported"); | |
103 | else if (e == 0) | |
d8e0b901 | 104 | FAIL_RET ("sysconf (_SC_MONOTONIC_CLOCK) must not return 0"); |
604b15cf | 105 | else |
b910f788 | 106 | # endif |
34788592 | 107 | { |
afe4de7d MC |
108 | run_test (CLOCK_MONOTONIC, CLOCK_USE_ATTR_CLOCK); |
109 | run_test (CLOCK_REALTIME, CLOCK_MONOTONIC); | |
110 | run_test (CLOCK_MONOTONIC, CLOCK_MONOTONIC); | |
111 | run_test (CLOCK_MONOTONIC, CLOCK_REALTIME); | |
112 | } | |
b910f788 | 113 | # else |
604b15cf | 114 | puts ("_POSIX_MONOTONIC_CLOCK not defined"); |
b910f788 | 115 | # endif |
604b15cf | 116 | |
d8e0b901 | 117 | return 0; |
b910f788 | 118 | #endif |
604b15cf UD |
119 | } |
120 | ||
d8e0b901 | 121 | #include <support/test-driver.c> |