xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Copyright (C) 2004-2016 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2004. |
| 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 <stdlib.h> |
| 23 | #include <time.h> |
| 24 | |
| 25 | |
| 26 | static pthread_barrier_t b; |
| 27 | static pthread_rwlock_t r = PTHREAD_RWLOCK_INITIALIZER; |
| 28 | |
| 29 | |
| 30 | static void * |
| 31 | tf (void *arg) |
| 32 | { |
| 33 | /* Lock the read-write lock. */ |
| 34 | if (pthread_rwlock_wrlock (&r) != 0) |
| 35 | { |
| 36 | puts ("tf: cannot lock rwlock"); |
| 37 | exit (EXIT_FAILURE); |
| 38 | } |
| 39 | |
| 40 | pthread_t mt = *(pthread_t *) arg; |
| 41 | |
| 42 | pthread_barrier_wait (&b); |
| 43 | |
| 44 | /* This call will never return. */ |
| 45 | pthread_join (mt, NULL); |
| 46 | |
| 47 | return NULL; |
| 48 | } |
| 49 | |
| 50 | |
| 51 | static int |
| 52 | do_test (void) |
| 53 | { |
| 54 | int result = 0; |
| 55 | struct timespec ts; |
| 56 | |
| 57 | if (clock_gettime (CLOCK_REALTIME, &ts) != 0) |
| 58 | { |
| 59 | puts ("clock_gettime failed"); |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | if (pthread_barrier_init (&b, NULL, 2) != 0) |
| 64 | { |
| 65 | puts ("barrier_init failed"); |
| 66 | return 1; |
| 67 | } |
| 68 | |
| 69 | pthread_t me = pthread_self (); |
| 70 | pthread_t th; |
| 71 | if (pthread_create (&th, NULL, tf, &me) != 0) |
| 72 | { |
| 73 | puts ("create failed"); |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | /* Wait until the rwlock is locked. */ |
| 78 | pthread_barrier_wait (&b); |
| 79 | |
| 80 | ts.tv_nsec = -1; |
| 81 | |
| 82 | int e = pthread_rwlock_timedrdlock (&r, &ts); |
| 83 | if (e == 0) |
| 84 | { |
| 85 | puts ("first rwlock_timedrdlock did not fail"); |
| 86 | result = 1; |
| 87 | } |
| 88 | else if (e != EINVAL) |
| 89 | { |
| 90 | puts ("first rwlock_timedrdlock did not return EINVAL"); |
| 91 | result = 1; |
| 92 | } |
| 93 | |
| 94 | e = pthread_rwlock_timedwrlock (&r, &ts); |
| 95 | if (e == 0) |
| 96 | { |
| 97 | puts ("first rwlock_timedwrlock did not fail"); |
| 98 | result = 1; |
| 99 | } |
| 100 | else if (e != EINVAL) |
| 101 | { |
| 102 | puts ("first rwlock_timedwrlock did not return EINVAL"); |
| 103 | result = 1; |
| 104 | } |
| 105 | |
| 106 | ts.tv_nsec = 1000000000; |
| 107 | |
| 108 | e = pthread_rwlock_timedrdlock (&r, &ts); |
| 109 | if (e == 0) |
| 110 | { |
| 111 | puts ("second rwlock_timedrdlock did not fail"); |
| 112 | result = 1; |
| 113 | } |
| 114 | else if (e != EINVAL) |
| 115 | { |
| 116 | puts ("second rwlock_timedrdlock did not return EINVAL"); |
| 117 | result = 1; |
| 118 | } |
| 119 | |
| 120 | e = pthread_rwlock_timedrdlock (&r, &ts); |
| 121 | if (e == 0) |
| 122 | { |
| 123 | puts ("second rwlock_timedrdlock did not fail"); |
| 124 | result = 1; |
| 125 | } |
| 126 | else if (e != EINVAL) |
| 127 | { |
| 128 | puts ("second rwlock_timedrdlock did not return EINVAL"); |
| 129 | result = 1; |
| 130 | } |
| 131 | |
| 132 | ts.tv_nsec = (__typeof (ts.tv_nsec)) 0x100001000LL; |
| 133 | if ((__typeof (ts.tv_nsec)) 0x100001000LL != 0x100001000LL) |
| 134 | ts.tv_nsec = 2000000000; |
| 135 | |
| 136 | e = pthread_rwlock_timedrdlock (&r, &ts); |
| 137 | if (e == 0) |
| 138 | { |
| 139 | puts ("third rwlock_timedrdlock did not fail"); |
| 140 | result = 1; |
| 141 | } |
| 142 | else if (e != EINVAL) |
| 143 | { |
| 144 | puts ("third rwlock_timedrdlock did not return EINVAL"); |
| 145 | result = 1; |
| 146 | } |
| 147 | |
| 148 | e = pthread_rwlock_timedrdlock (&r, &ts); |
| 149 | if (e == 0) |
| 150 | { |
| 151 | puts ("third rwlock_timedrdlock did not fail"); |
| 152 | result = 1; |
| 153 | } |
| 154 | else if (e != EINVAL) |
| 155 | { |
| 156 | puts ("third rwlock_timedrdlock did not return EINVAL"); |
| 157 | result = 1; |
| 158 | } |
| 159 | |
| 160 | if (result == 0) |
| 161 | puts ("no bugs"); |
| 162 | |
| 163 | return result; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | #define TEST_FUNCTION do_test () |
| 168 | #include "../test-skeleton.c" |