lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 2002, 2003 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, 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 | |
| 25 | |
| 26 | static pthread_mutex_t m; |
| 27 | static pthread_barrier_t b; |
| 28 | |
| 29 | |
| 30 | static void * |
| 31 | tf (void *arg) |
| 32 | { |
| 33 | int e = pthread_mutex_unlock (&m); |
| 34 | if (e == 0) |
| 35 | { |
| 36 | puts ("child: 1st mutex_unlock succeeded"); |
| 37 | exit (1); |
| 38 | } |
| 39 | else if (e != EPERM) |
| 40 | { |
| 41 | puts ("child: 1st mutex_unlock error != EPERM"); |
| 42 | exit (1); |
| 43 | } |
| 44 | |
| 45 | e = pthread_mutex_trylock (&m); |
| 46 | if (e == 0) |
| 47 | { |
| 48 | puts ("child: 1st trylock suceeded"); |
| 49 | exit (1); |
| 50 | } |
| 51 | if (e != EBUSY) |
| 52 | { |
| 53 | puts ("child: 1st trylock didn't return EBUSY"); |
| 54 | exit (1); |
| 55 | } |
| 56 | |
| 57 | e = pthread_barrier_wait (&b); |
| 58 | if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) |
| 59 | { |
| 60 | puts ("child: 1st barrier_wait failed"); |
| 61 | exit (1); |
| 62 | } |
| 63 | |
| 64 | e = pthread_barrier_wait (&b); |
| 65 | if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) |
| 66 | { |
| 67 | puts ("child: 2nd barrier_wait failed"); |
| 68 | exit (1); |
| 69 | } |
| 70 | |
| 71 | e = pthread_mutex_unlock (&m); |
| 72 | if (e == 0) |
| 73 | { |
| 74 | puts ("child: 2nd mutex_unlock succeeded"); |
| 75 | exit (1); |
| 76 | } |
| 77 | else if (e != EPERM) |
| 78 | { |
| 79 | puts ("child: 2nd mutex_unlock error != EPERM"); |
| 80 | exit (1); |
| 81 | } |
| 82 | |
| 83 | if (pthread_mutex_trylock (&m) != 0) |
| 84 | { |
| 85 | puts ("child: 2nd trylock failed"); |
| 86 | exit (1); |
| 87 | } |
| 88 | |
| 89 | if (pthread_mutex_unlock (&m) != 0) |
| 90 | { |
| 91 | puts ("child: 3rd mutex_unlock failed"); |
| 92 | exit (1); |
| 93 | } |
| 94 | |
| 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | static int |
| 100 | do_test (void) |
| 101 | { |
| 102 | pthread_mutexattr_t a; |
| 103 | int e; |
| 104 | |
| 105 | if (pthread_mutexattr_init (&a) != 0) |
| 106 | { |
| 107 | puts ("mutexattr_init failed"); |
| 108 | exit (1); |
| 109 | } |
| 110 | |
| 111 | if (pthread_mutexattr_settype (&a, PTHREAD_MUTEX_ERRORCHECK) != 0) |
| 112 | { |
| 113 | puts ("mutexattr_settype failed"); |
| 114 | exit (1); |
| 115 | } |
| 116 | |
| 117 | if (pthread_mutex_init (&m, &a) != 0) |
| 118 | { |
| 119 | puts ("mutex_init failed"); |
| 120 | exit (1); |
| 121 | } |
| 122 | |
| 123 | if (pthread_barrier_init (&b, NULL, 2) != 0) |
| 124 | { |
| 125 | puts ("barrier_init failed"); |
| 126 | exit (1); |
| 127 | } |
| 128 | |
| 129 | if ((e = pthread_mutex_unlock (&m)) == 0) |
| 130 | { |
| 131 | puts ("1st mutex_unlock succeeded"); |
| 132 | exit (1); |
| 133 | } |
| 134 | else if (e != EPERM) |
| 135 | { |
| 136 | puts ("1st mutex_unlock error != EPERM"); |
| 137 | exit (1); |
| 138 | } |
| 139 | |
| 140 | if (pthread_mutex_lock (&m) != 0) |
| 141 | { |
| 142 | puts ("mutex_lock failed"); |
| 143 | exit (1); |
| 144 | } |
| 145 | |
| 146 | if ((e = pthread_mutex_lock (&m)) == 0) |
| 147 | { |
| 148 | puts ("2nd mutex_lock succeeded"); |
| 149 | exit (1); |
| 150 | } |
| 151 | else if (e != EDEADLK) |
| 152 | { |
| 153 | puts ("2nd mutex_lock error != EDEADLK"); |
| 154 | exit (1); |
| 155 | } |
| 156 | |
| 157 | pthread_t th; |
| 158 | if (pthread_create (&th, NULL, tf, NULL) != 0) |
| 159 | { |
| 160 | puts ("create failed"); |
| 161 | exit (1); |
| 162 | } |
| 163 | |
| 164 | e = pthread_barrier_wait (&b); |
| 165 | if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) |
| 166 | { |
| 167 | puts ("1st barrier_wait failed"); |
| 168 | exit (1); |
| 169 | } |
| 170 | |
| 171 | if (pthread_mutex_unlock (&m) != 0) |
| 172 | { |
| 173 | puts ("2nd mutex_unlock failed"); |
| 174 | exit (1); |
| 175 | } |
| 176 | |
| 177 | if ((e = pthread_mutex_unlock (&m)) == 0) |
| 178 | { |
| 179 | puts ("3rd mutex_unlock succeeded"); |
| 180 | exit (1); |
| 181 | } |
| 182 | else if (e != EPERM) |
| 183 | { |
| 184 | puts ("3rd mutex_unlock error != EPERM"); |
| 185 | exit (1); |
| 186 | } |
| 187 | |
| 188 | e = pthread_barrier_wait (&b); |
| 189 | if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) |
| 190 | { |
| 191 | puts ("2nd barrier_wait failed"); |
| 192 | exit (1); |
| 193 | } |
| 194 | |
| 195 | if (pthread_join (th, NULL) != 0) |
| 196 | { |
| 197 | puts ("join failed"); |
| 198 | exit (1); |
| 199 | } |
| 200 | |
| 201 | if (pthread_mutex_destroy (&m) != 0) |
| 202 | { |
| 203 | puts ("mutex_destroy failed"); |
| 204 | exit (1); |
| 205 | } |
| 206 | |
| 207 | if (pthread_barrier_destroy (&b) != 0) |
| 208 | { |
| 209 | puts ("barrier_destroy failed"); |
| 210 | exit (1); |
| 211 | } |
| 212 | |
| 213 | if (pthread_mutexattr_destroy (&a) != 0) |
| 214 | { |
| 215 | puts ("mutexattr_destroy failed"); |
| 216 | exit (1); |
| 217 | } |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | #define TEST_FUNCTION do_test () |
| 223 | #include "../test-skeleton.c" |