]> git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/tst-cond11.c
nptl: Convert tst-cond11.c to use libsupport
[thirdparty/glibc.git] / nptl / tst-cond11.c
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.
4
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.
9
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.
14
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 <http://www.gnu.org/licenses/>. */
18
19 #include <errno.h>
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <time.h>
23 #include <unistd.h>
24 #include <support/check.h>
25 #include <support/timespec.h>
26 #include <support/xthread.h>
27 #include <support/xtime.h>
28
29
30 #if defined _POSIX_CLOCK_SELECTION && _POSIX_CLOCK_SELECTION >= 0
31 static int
32 run_test (clockid_t cl)
33 {
34 pthread_condattr_t condattr;
35 pthread_cond_t cond;
36 pthread_mutexattr_t mutattr;
37 pthread_mutex_t mut;
38
39 printf ("clock = %d\n", (int) cl);
40
41 TEST_COMPARE (pthread_condattr_init (&condattr), 0);
42 TEST_COMPARE (pthread_condattr_setclock (&condattr, cl), 0);
43
44 clockid_t cl2;
45 TEST_COMPARE (pthread_condattr_getclock (&condattr, &cl2), 0);
46 TEST_COMPARE (cl, cl2);
47
48 TEST_COMPARE (pthread_cond_init (&cond, &condattr), 0);
49 TEST_COMPARE (pthread_condattr_destroy (&condattr), 0);
50
51 xpthread_mutexattr_init (&mutattr);
52 xpthread_mutexattr_settype (&mutattr, PTHREAD_MUTEX_ERRORCHECK);
53 xpthread_mutex_init (&mut, &mutattr);
54 xpthread_mutexattr_destroy (&mutattr);
55
56 xpthread_mutex_lock (&mut);
57 TEST_COMPARE (pthread_mutex_lock (&mut), EDEADLK);
58
59 struct timespec ts_timeout;
60 xclock_gettime (cl, &ts_timeout);
61
62 /* Wait one second. */
63 ++ts_timeout.tv_sec;
64
65 TEST_COMPARE (pthread_cond_timedwait (&cond, &mut, &ts_timeout), ETIMEDOUT);
66 TEST_TIMESPEC_BEFORE_NOW (ts_timeout, cl);
67
68 xpthread_mutex_unlock (&mut);
69 xpthread_mutex_destroy (&mut);
70 TEST_COMPARE (pthread_cond_destroy (&cond), 0);
71
72 return 0;
73 }
74 #endif
75
76
77 static int
78 do_test (void)
79 {
80 #if !defined _POSIX_CLOCK_SELECTION || _POSIX_CLOCK_SELECTION == -1
81
82 FAIL_UNSUPPORTED ("_POSIX_CLOCK_SELECTION not supported, test skipped");
83
84 #else
85
86 run_test (CLOCK_REALTIME);
87
88 # if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
89 # if _POSIX_MONOTONIC_CLOCK == 0
90 int e = sysconf (_SC_MONOTONIC_CLOCK);
91 if (e < 0)
92 puts ("CLOCK_MONOTONIC not supported");
93 else if (e == 0)
94 FAIL_RET ("sysconf (_SC_MONOTONIC_CLOCK) must not return 0");
95 else
96 # endif
97 run_test (CLOCK_MONOTONIC);
98 # else
99 puts ("_POSIX_MONOTONIC_CLOCK not defined");
100 # endif
101
102 return 0;
103 #endif
104 }
105
106 #include <support/test-driver.c>