1 /* Copyright (C) 2003-2019 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
24 #include <support/check.h>
25 #include <support/test-driver.h>
26 #include <support/timespec.h>
27 #include <support/xthread.h>
28 #include <support/xtime.h>
30 /* A bogus clock value that tells run_test to use pthread_cond_timedwait
31 rather than pthread_condclockwait. */
32 #define CLOCK_USE_ATTR_CLOCK (-1)
34 #if defined _POSIX_CLOCK_SELECTION && _POSIX_CLOCK_SELECTION >= 0
36 run_test (clockid_t attr_clock
, clockid_t wait_clock
)
38 pthread_condattr_t condattr
;
40 pthread_mutexattr_t mutattr
;
43 verbose_printf ("attr_clock = %d\n", (int) attr_clock
);
45 TEST_COMPARE (pthread_condattr_init (&condattr
), 0);
46 TEST_COMPARE (pthread_condattr_setclock (&condattr
, attr_clock
), 0);
48 clockid_t attr_clock_read
;
49 TEST_COMPARE (pthread_condattr_getclock (&condattr
, &attr_clock_read
), 0);
50 TEST_COMPARE (attr_clock
, attr_clock_read
);
52 TEST_COMPARE (pthread_cond_init (&cond
, &condattr
), 0);
53 TEST_COMPARE (pthread_condattr_destroy (&condattr
), 0);
55 xpthread_mutexattr_init (&mutattr
);
56 xpthread_mutexattr_settype (&mutattr
, PTHREAD_MUTEX_ERRORCHECK
);
57 xpthread_mutex_init (&mut
, &mutattr
);
58 xpthread_mutexattr_destroy (&mutattr
);
60 xpthread_mutex_lock (&mut
);
61 TEST_COMPARE (pthread_mutex_lock (&mut
), EDEADLK
);
63 struct timespec ts_timeout
;
64 xclock_gettime (wait_clock
== CLOCK_USE_ATTR_CLOCK
? attr_clock
: wait_clock
,
67 /* Wait one second. */
70 if (wait_clock
== CLOCK_USE_ATTR_CLOCK
) {
71 TEST_COMPARE (pthread_cond_timedwait (&cond
, &mut
, &ts_timeout
), ETIMEDOUT
);
72 TEST_TIMESPEC_BEFORE_NOW (ts_timeout
, attr_clock
);
74 TEST_COMPARE (pthread_cond_clockwait (&cond
, &mut
, wait_clock
, &ts_timeout
),
76 TEST_TIMESPEC_BEFORE_NOW (ts_timeout
, wait_clock
);
79 xpthread_mutex_unlock (&mut
);
80 xpthread_mutex_destroy (&mut
);
81 TEST_COMPARE (pthread_cond_destroy (&cond
), 0);
91 #if !defined _POSIX_CLOCK_SELECTION || _POSIX_CLOCK_SELECTION == -1
93 FAIL_UNSUPPORTED ("_POSIX_CLOCK_SELECTION not supported, test skipped");
97 run_test (CLOCK_REALTIME
, CLOCK_USE_ATTR_CLOCK
);
99 # if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
100 # if _POSIX_MONOTONIC_CLOCK == 0
101 int e
= sysconf (_SC_MONOTONIC_CLOCK
);
103 puts ("CLOCK_MONOTONIC not supported");
105 FAIL_RET ("sysconf (_SC_MONOTONIC_CLOCK) must not return 0");
109 run_test (CLOCK_MONOTONIC
, CLOCK_USE_ATTR_CLOCK
);
110 run_test (CLOCK_REALTIME
, CLOCK_MONOTONIC
);
111 run_test (CLOCK_MONOTONIC
, CLOCK_MONOTONIC
);
112 run_test (CLOCK_MONOTONIC
, CLOCK_REALTIME
);
115 puts ("_POSIX_MONOTONIC_CLOCK not defined");
122 #include <support/test-driver.c>