| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Copyright (C) 2003-2016 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 |  | 
|  | 25 |  | 
|  | 26 | #if defined _POSIX_CLOCK_SELECTION && _POSIX_CLOCK_SELECTION >= 0 | 
|  | 27 | static int | 
|  | 28 | run_test (clockid_t cl) | 
|  | 29 | { | 
|  | 30 | pthread_condattr_t condattr; | 
|  | 31 | pthread_cond_t cond; | 
|  | 32 | pthread_mutexattr_t mutattr; | 
|  | 33 | pthread_mutex_t mut; | 
|  | 34 |  | 
|  | 35 | printf ("clock = %d\n", (int) cl); | 
|  | 36 |  | 
|  | 37 | if (pthread_condattr_init (&condattr) != 0) | 
|  | 38 | { | 
|  | 39 | puts ("condattr_init failed"); | 
|  | 40 | return 1; | 
|  | 41 | } | 
|  | 42 |  | 
|  | 43 | if (pthread_condattr_setclock (&condattr, cl) != 0) | 
|  | 44 | { | 
|  | 45 | puts ("condattr_setclock failed"); | 
|  | 46 | return 1; | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | clockid_t cl2; | 
|  | 50 | if (pthread_condattr_getclock (&condattr, &cl2) != 0) | 
|  | 51 | { | 
|  | 52 | puts ("condattr_getclock failed"); | 
|  | 53 | return 1; | 
|  | 54 | } | 
|  | 55 | if (cl != cl2) | 
|  | 56 | { | 
|  | 57 | printf ("condattr_getclock returned wrong value: %d, expected %d\n", | 
|  | 58 | (int) cl2, (int) cl); | 
|  | 59 | return 1; | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | if (pthread_cond_init (&cond, &condattr) != 0) | 
|  | 63 | { | 
|  | 64 | puts ("cond_init failed"); | 
|  | 65 | return 1; | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | if (pthread_condattr_destroy (&condattr) != 0) | 
|  | 69 | { | 
|  | 70 | puts ("condattr_destroy failed"); | 
|  | 71 | return 1; | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | if (pthread_mutexattr_init (&mutattr) != 0) | 
|  | 75 | { | 
|  | 76 | puts ("mutexattr_init failed"); | 
|  | 77 | return 1; | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | if (pthread_mutexattr_settype (&mutattr, PTHREAD_MUTEX_ERRORCHECK) != 0) | 
|  | 81 | { | 
|  | 82 | puts ("mutexattr_settype failed"); | 
|  | 83 | return 1; | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | if (pthread_mutex_init (&mut, &mutattr) != 0) | 
|  | 87 | { | 
|  | 88 | puts ("mutex_init failed"); | 
|  | 89 | return 1; | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | if (pthread_mutexattr_destroy (&mutattr) != 0) | 
|  | 93 | { | 
|  | 94 | puts ("mutexattr_destroy failed"); | 
|  | 95 | return 1; | 
|  | 96 | } | 
|  | 97 |  | 
|  | 98 | if (pthread_mutex_lock (&mut) != 0) | 
|  | 99 | { | 
|  | 100 | puts ("mutex_lock failed"); | 
|  | 101 | return 1; | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | if (pthread_mutex_lock (&mut) != EDEADLK) | 
|  | 105 | { | 
|  | 106 | puts ("2nd mutex_lock did not return EDEADLK"); | 
|  | 107 | return 1; | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | struct timespec ts; | 
|  | 111 | if (clock_gettime (cl, &ts) != 0) | 
|  | 112 | { | 
|  | 113 | puts ("clock_gettime failed"); | 
|  | 114 | return 1; | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | /* Wait one second.  */ | 
|  | 118 | ++ts.tv_sec; | 
|  | 119 |  | 
|  | 120 | int e = pthread_cond_timedwait (&cond, &mut, &ts); | 
|  | 121 | if (e == 0) | 
|  | 122 | { | 
|  | 123 | puts ("cond_timedwait succeeded"); | 
|  | 124 | return 1; | 
|  | 125 | } | 
|  | 126 | else if (e != ETIMEDOUT) | 
|  | 127 | { | 
|  | 128 | puts ("cond_timedwait did not return ETIMEDOUT"); | 
|  | 129 | return 1; | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | struct timespec ts2; | 
|  | 133 | if (clock_gettime (cl, &ts2) != 0) | 
|  | 134 | { | 
|  | 135 | puts ("second clock_gettime failed"); | 
|  | 136 | return 1; | 
|  | 137 | } | 
|  | 138 |  | 
|  | 139 | if (ts2.tv_sec < ts.tv_sec | 
|  | 140 | || (ts2.tv_sec == ts.tv_sec && ts2.tv_nsec < ts.tv_nsec)) | 
|  | 141 | { | 
|  | 142 | puts ("timeout too short"); | 
|  | 143 | return 1; | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | if (pthread_mutex_unlock (&mut) != 0) | 
|  | 147 | { | 
|  | 148 | puts ("mutex_unlock failed"); | 
|  | 149 | return 1; | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | if (pthread_mutex_destroy (&mut) != 0) | 
|  | 153 | { | 
|  | 154 | puts ("mutex_destroy failed"); | 
|  | 155 | return 1; | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | if (pthread_cond_destroy (&cond) != 0) | 
|  | 159 | { | 
|  | 160 | puts ("cond_destroy failed"); | 
|  | 161 | return 1; | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 | return 0; | 
|  | 165 | } | 
|  | 166 | #endif | 
|  | 167 |  | 
|  | 168 |  | 
|  | 169 | static int | 
|  | 170 | do_test (void) | 
|  | 171 | { | 
|  | 172 | #if !defined _POSIX_CLOCK_SELECTION || _POSIX_CLOCK_SELECTION == -1 | 
|  | 173 |  | 
|  | 174 | puts ("_POSIX_CLOCK_SELECTION not supported, test skipped"); | 
|  | 175 | return 0; | 
|  | 176 |  | 
|  | 177 | #else | 
|  | 178 |  | 
|  | 179 | int res = run_test (CLOCK_REALTIME); | 
|  | 180 |  | 
|  | 181 | # if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0 | 
|  | 182 | #  if _POSIX_MONOTONIC_CLOCK == 0 | 
|  | 183 | int e = sysconf (_SC_MONOTONIC_CLOCK); | 
|  | 184 | if (e < 0) | 
|  | 185 | puts ("CLOCK_MONOTONIC not supported"); | 
|  | 186 | else if (e == 0) | 
|  | 187 | { | 
|  | 188 | puts ("sysconf (_SC_MONOTONIC_CLOCK) must not return 0"); | 
|  | 189 | res = 1; | 
|  | 190 | } | 
|  | 191 | else | 
|  | 192 | #  endif | 
|  | 193 | res |= run_test (CLOCK_MONOTONIC); | 
|  | 194 | # else | 
|  | 195 | puts ("_POSIX_MONOTONIC_CLOCK not defined"); | 
|  | 196 | # endif | 
|  | 197 |  | 
|  | 198 | return res; | 
|  | 199 | #endif | 
|  | 200 | } | 
|  | 201 |  | 
|  | 202 | #define TIMEOUT 3 | 
|  | 203 | #define TEST_FUNCTION do_test () | 
|  | 204 | #include "../test-skeleton.c" |