xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 2008-2016 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Jakub Jelinek <jakub@redhat.com>, 2008. |
| 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 | check (pthread_condattr_t *condattr, int pshared, clockid_t cl) |
| 29 | { |
| 30 | clockid_t cl2; |
| 31 | if (pthread_condattr_getclock (condattr, &cl2) != 0) |
| 32 | { |
| 33 | puts ("condattr_getclock failed"); |
| 34 | return 1; |
| 35 | } |
| 36 | if (cl != cl2) |
| 37 | { |
| 38 | printf ("condattr_getclock returned wrong value: %d, expected %d\n", |
| 39 | (int) cl2, (int) cl); |
| 40 | return 1; |
| 41 | } |
| 42 | |
| 43 | int p; |
| 44 | if (pthread_condattr_getpshared (condattr, &p) != 0) |
| 45 | { |
| 46 | puts ("condattr_getpshared failed"); |
| 47 | return 1; |
| 48 | } |
| 49 | else if (p != pshared) |
| 50 | { |
| 51 | printf ("condattr_getpshared returned wrong value: %d, expected %d\n", |
| 52 | p, pshared); |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static int |
| 60 | run_test (clockid_t cl) |
| 61 | { |
| 62 | pthread_condattr_t condattr; |
| 63 | |
| 64 | printf ("clock = %d\n", (int) cl); |
| 65 | |
| 66 | if (pthread_condattr_init (&condattr) != 0) |
| 67 | { |
| 68 | puts ("condattr_init failed"); |
| 69 | return 1; |
| 70 | } |
| 71 | |
| 72 | if (check (&condattr, PTHREAD_PROCESS_PRIVATE, CLOCK_REALTIME)) |
| 73 | return 1; |
| 74 | |
| 75 | if (pthread_condattr_setpshared (&condattr, PTHREAD_PROCESS_SHARED) != 0) |
| 76 | { |
| 77 | puts ("1st condattr_setpshared failed"); |
| 78 | return 1; |
| 79 | } |
| 80 | |
| 81 | if (check (&condattr, PTHREAD_PROCESS_SHARED, CLOCK_REALTIME)) |
| 82 | return 1; |
| 83 | |
| 84 | if (pthread_condattr_setclock (&condattr, cl) != 0) |
| 85 | { |
| 86 | puts ("1st condattr_setclock failed"); |
| 87 | return 1; |
| 88 | } |
| 89 | |
| 90 | if (check (&condattr, PTHREAD_PROCESS_SHARED, cl)) |
| 91 | return 1; |
| 92 | |
| 93 | if (pthread_condattr_setpshared (&condattr, PTHREAD_PROCESS_PRIVATE) != 0) |
| 94 | { |
| 95 | puts ("2nd condattr_setpshared failed"); |
| 96 | return 1; |
| 97 | } |
| 98 | |
| 99 | if (check (&condattr, PTHREAD_PROCESS_PRIVATE, cl)) |
| 100 | return 1; |
| 101 | |
| 102 | if (pthread_condattr_setclock (&condattr, CLOCK_REALTIME) != 0) |
| 103 | { |
| 104 | puts ("2nd condattr_setclock failed"); |
| 105 | return 1; |
| 106 | } |
| 107 | |
| 108 | if (check (&condattr, PTHREAD_PROCESS_PRIVATE, CLOCK_REALTIME)) |
| 109 | return 1; |
| 110 | |
| 111 | if (pthread_condattr_setclock (&condattr, cl) != 0) |
| 112 | { |
| 113 | puts ("3rd condattr_setclock failed"); |
| 114 | return 1; |
| 115 | } |
| 116 | |
| 117 | if (check (&condattr, PTHREAD_PROCESS_PRIVATE, cl)) |
| 118 | return 1; |
| 119 | |
| 120 | if (pthread_condattr_setpshared (&condattr, PTHREAD_PROCESS_SHARED) != 0) |
| 121 | { |
| 122 | puts ("3rd condattr_setpshared failed"); |
| 123 | return 1; |
| 124 | } |
| 125 | |
| 126 | if (check (&condattr, PTHREAD_PROCESS_SHARED, cl)) |
| 127 | return 1; |
| 128 | |
| 129 | if (pthread_condattr_setclock (&condattr, CLOCK_REALTIME) != 0) |
| 130 | { |
| 131 | puts ("4th condattr_setclock failed"); |
| 132 | return 1; |
| 133 | } |
| 134 | |
| 135 | if (check (&condattr, PTHREAD_PROCESS_SHARED, CLOCK_REALTIME)) |
| 136 | return 1; |
| 137 | |
| 138 | if (pthread_condattr_destroy (&condattr) != 0) |
| 139 | { |
| 140 | puts ("condattr_destroy failed"); |
| 141 | return 1; |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | #endif |
| 147 | |
| 148 | |
| 149 | static int |
| 150 | do_test (void) |
| 151 | { |
| 152 | #if !defined _POSIX_CLOCK_SELECTION || _POSIX_CLOCK_SELECTION == -1 |
| 153 | |
| 154 | puts ("_POSIX_CLOCK_SELECTION not supported, test skipped"); |
| 155 | return 0; |
| 156 | |
| 157 | #else |
| 158 | |
| 159 | int res = run_test (CLOCK_REALTIME); |
| 160 | |
| 161 | # if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0 |
| 162 | # if _POSIX_MONOTONIC_CLOCK == 0 |
| 163 | int e = sysconf (_SC_MONOTONIC_CLOCK); |
| 164 | if (e < 0) |
| 165 | puts ("CLOCK_MONOTONIC not supported"); |
| 166 | else if (e == 0) |
| 167 | { |
| 168 | puts ("sysconf (_SC_MONOTONIC_CLOCK) must not return 0"); |
| 169 | res = 1; |
| 170 | } |
| 171 | else |
| 172 | # endif |
| 173 | res |= run_test (CLOCK_MONOTONIC); |
| 174 | # else |
| 175 | puts ("_POSIX_MONOTONIC_CLOCK not defined"); |
| 176 | # endif |
| 177 | |
| 178 | return res; |
| 179 | #endif |
| 180 | } |
| 181 | |
| 182 | #define TEST_FUNCTION do_test () |
| 183 | #include "../test-skeleton.c" |