| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Copyright (C) 2002-2016 Free Software Foundation, Inc. | 
|  | 2 | This file is part of the GNU C Library. | 
|  | 3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. | 
|  | 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 |  | 
|  | 24 |  | 
|  | 25 | static pthread_mutex_t m; | 
|  | 26 | static pthread_barrier_t b; | 
|  | 27 |  | 
|  | 28 |  | 
|  | 29 | static void * | 
|  | 30 | tf (void *arg) | 
|  | 31 | { | 
|  | 32 | int e = pthread_mutex_unlock (&m); | 
|  | 33 | if (e == 0) | 
|  | 34 | { | 
|  | 35 | puts ("1st mutex_unlock in child succeeded"); | 
|  | 36 | exit (1); | 
|  | 37 | } | 
|  | 38 | if (e != EPERM) | 
|  | 39 | { | 
|  | 40 | puts ("1st mutex_unlock in child didn't return EPERM"); | 
|  | 41 | exit (1); | 
|  | 42 | } | 
|  | 43 |  | 
|  | 44 | e = pthread_mutex_trylock (&m); | 
|  | 45 | if (e == 0) | 
|  | 46 | { | 
|  | 47 | puts ("mutex_trylock in second thread succeeded"); | 
|  | 48 | exit (1); | 
|  | 49 | } | 
|  | 50 | if (e != EBUSY) | 
|  | 51 | { | 
|  | 52 | puts ("mutex_trylock returned wrong value"); | 
|  | 53 | exit (1); | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | e = pthread_barrier_wait (&b); | 
|  | 57 | if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) | 
|  | 58 | { | 
|  | 59 | puts ("barrier_wait failed"); | 
|  | 60 | exit (1); | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | e = pthread_barrier_wait (&b); | 
|  | 64 | if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) | 
|  | 65 | { | 
|  | 66 | puts ("barrier_wait failed"); | 
|  | 67 | exit (1); | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | e = pthread_mutex_unlock (&m); | 
|  | 71 | if (e == 0) | 
|  | 72 | { | 
|  | 73 | puts ("2nd mutex_unlock in child succeeded"); | 
|  | 74 | exit (1); | 
|  | 75 | } | 
|  | 76 | if (e != EPERM) | 
|  | 77 | { | 
|  | 78 | puts ("2nd mutex_unlock in child didn't return EPERM"); | 
|  | 79 | exit (1); | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | if (pthread_mutex_trylock (&m) != 0) | 
|  | 83 | { | 
|  | 84 | puts ("2nd mutex_trylock in second thread failed"); | 
|  | 85 | exit (1); | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | if (pthread_mutex_unlock (&m) != 0) | 
|  | 89 | { | 
|  | 90 | puts ("3rd mutex_unlock in second thread failed"); | 
|  | 91 | exit (1); | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | return NULL; | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 |  | 
|  | 98 | static int | 
|  | 99 | do_test (void) | 
|  | 100 | { | 
|  | 101 | pthread_mutexattr_t a; | 
|  | 102 |  | 
|  | 103 | if (pthread_mutexattr_init (&a) != 0) | 
|  | 104 | { | 
|  | 105 | puts ("mutexattr_init failed"); | 
|  | 106 | return 1; | 
|  | 107 | } | 
|  | 108 |  | 
|  | 109 | if (pthread_mutexattr_settype (&a, PTHREAD_MUTEX_RECURSIVE) != 0) | 
|  | 110 | { | 
|  | 111 | puts ("mutexattr_settype failed"); | 
|  | 112 | return 1; | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | #ifdef ENABLE_PI | 
|  | 116 | if (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT) != 0) | 
|  | 117 | { | 
|  | 118 | puts ("pthread_mutexattr_setprotocol failed"); | 
|  | 119 | return 1; | 
|  | 120 | } | 
|  | 121 | #endif | 
|  | 122 |  | 
|  | 123 | int e; | 
|  | 124 | e = pthread_mutex_init (&m, &a); | 
|  | 125 | if (e != 0) | 
|  | 126 | { | 
|  | 127 | #ifdef ENABLE_PI | 
|  | 128 | if (e == ENOTSUP) | 
|  | 129 | { | 
|  | 130 | puts ("PI mutexes unsupported"); | 
|  | 131 | return 0; | 
|  | 132 | } | 
|  | 133 | #endif | 
|  | 134 | puts ("mutex_init failed"); | 
|  | 135 | return 1; | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | if (pthread_barrier_init (&b, NULL, 2) != 0) | 
|  | 139 | { | 
|  | 140 | puts ("barrier_init failed"); | 
|  | 141 | return 1; | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | if (pthread_mutex_lock (&m) != 0) | 
|  | 145 | { | 
|  | 146 | puts ("mutex_lock failed"); | 
|  | 147 | return 1; | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | if (pthread_mutex_lock (&m) != 0) | 
|  | 151 | { | 
|  | 152 | puts ("2nd mutex_lock failed"); | 
|  | 153 | return 1; | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | if (pthread_mutex_trylock (&m) != 0) | 
|  | 157 | { | 
|  | 158 | puts ("1st trylock failed"); | 
|  | 159 | return 1; | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | if (pthread_mutex_unlock (&m) != 0) | 
|  | 163 | { | 
|  | 164 | puts ("mutex_unlock failed"); | 
|  | 165 | return 1; | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | if (pthread_mutex_unlock (&m) != 0) | 
|  | 169 | { | 
|  | 170 | puts ("2nd mutex_unlock failed"); | 
|  | 171 | return 1; | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | pthread_t th; | 
|  | 175 | if (pthread_create (&th, NULL, tf, NULL) != 0) | 
|  | 176 | { | 
|  | 177 | puts ("create failed"); | 
|  | 178 | return 1; | 
|  | 179 | } | 
|  | 180 |  | 
|  | 181 | e = pthread_barrier_wait (&b); | 
|  | 182 | if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) | 
|  | 183 | { | 
|  | 184 | puts ("barrier_wait failed"); | 
|  | 185 | return 1; | 
|  | 186 | } | 
|  | 187 |  | 
|  | 188 | if (pthread_mutex_unlock (&m) != 0) | 
|  | 189 | { | 
|  | 190 | puts ("3rd mutex_unlock failed"); | 
|  | 191 | return 1; | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | e = pthread_mutex_unlock (&m); | 
|  | 195 | if (e == 0) | 
|  | 196 | { | 
|  | 197 | puts ("4th mutex_unlock succeeded"); | 
|  | 198 | return 1; | 
|  | 199 | } | 
|  | 200 | if (e != EPERM) | 
|  | 201 | { | 
|  | 202 | puts ("4th mutex_unlock didn't return EPERM"); | 
|  | 203 | return 1; | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | e = pthread_barrier_wait (&b); | 
|  | 207 | if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) | 
|  | 208 | { | 
|  | 209 | puts ("barrier_wait failed"); | 
|  | 210 | return 1; | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | if (pthread_join (th, NULL) != 0) | 
|  | 214 | { | 
|  | 215 | puts ("join failed"); | 
|  | 216 | return 1; | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | if (pthread_barrier_destroy (&b) != 0) | 
|  | 220 | { | 
|  | 221 | puts ("barrier_destroy failed"); | 
|  | 222 | return 1; | 
|  | 223 | } | 
|  | 224 |  | 
|  | 225 | if (pthread_mutex_destroy (&m) != 0) | 
|  | 226 | { | 
|  | 227 | puts ("mutex_destroy failed"); | 
|  | 228 | return 1; | 
|  | 229 | } | 
|  | 230 |  | 
|  | 231 | if (pthread_mutexattr_destroy (&a) != 0) | 
|  | 232 | { | 
|  | 233 | puts ("mutexattr_destroy failed"); | 
|  | 234 | return 1; | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | return 0; | 
|  | 238 | } | 
|  | 239 |  | 
|  | 240 | #define TEST_FUNCTION do_test () | 
|  | 241 | #include "../test-skeleton.c" |