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