lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 2003 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2003. |
| 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 | #include <sys/time.h> |
| 26 | |
| 27 | |
| 28 | static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; |
| 29 | static pthread_mutex_t mut = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP; |
| 30 | |
| 31 | static pthread_barrier_t bar; |
| 32 | |
| 33 | |
| 34 | static void |
| 35 | ch (void *arg) |
| 36 | { |
| 37 | int e = pthread_mutex_lock (&mut); |
| 38 | if (e == 0) |
| 39 | { |
| 40 | puts ("mutex not locked at all by cond_wait"); |
| 41 | exit (1); |
| 42 | } |
| 43 | |
| 44 | if (e != EDEADLK) |
| 45 | { |
| 46 | puts ("no deadlock error signaled"); |
| 47 | exit (1); |
| 48 | } |
| 49 | |
| 50 | if (pthread_mutex_unlock (&mut) != 0) |
| 51 | { |
| 52 | puts ("ch: cannot unlock mutex"); |
| 53 | exit (1); |
| 54 | } |
| 55 | |
| 56 | puts ("ch done"); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | static void * |
| 61 | tf1 (void *p) |
| 62 | { |
| 63 | int err; |
| 64 | |
| 65 | if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0 |
| 66 | || pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, NULL) != 0) |
| 67 | { |
| 68 | puts ("cannot set cancellation options"); |
| 69 | exit (1); |
| 70 | } |
| 71 | |
| 72 | err = pthread_mutex_lock (&mut); |
| 73 | if (err != 0) |
| 74 | { |
| 75 | puts ("child: cannot get mutex"); |
| 76 | exit (1); |
| 77 | } |
| 78 | |
| 79 | err = pthread_barrier_wait (&bar); |
| 80 | if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD) |
| 81 | { |
| 82 | printf ("barrier_wait returned %d\n", err); |
| 83 | exit (1); |
| 84 | } |
| 85 | |
| 86 | puts ("child: got mutex; waiting"); |
| 87 | |
| 88 | pthread_cleanup_push (ch, NULL); |
| 89 | |
| 90 | pthread_cond_wait (&cond, &mut); |
| 91 | |
| 92 | pthread_cleanup_pop (0); |
| 93 | |
| 94 | puts ("child: cond_wait should not have returned"); |
| 95 | |
| 96 | return NULL; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | static void * |
| 101 | tf2 (void *p) |
| 102 | { |
| 103 | int err; |
| 104 | |
| 105 | if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0 |
| 106 | || pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, NULL) != 0) |
| 107 | { |
| 108 | puts ("cannot set cancellation options"); |
| 109 | exit (1); |
| 110 | } |
| 111 | |
| 112 | err = pthread_mutex_lock (&mut); |
| 113 | if (err != 0) |
| 114 | { |
| 115 | puts ("child: cannot get mutex"); |
| 116 | exit (1); |
| 117 | } |
| 118 | |
| 119 | err = pthread_barrier_wait (&bar); |
| 120 | if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD) |
| 121 | { |
| 122 | printf ("barrier_wait returned %d\n", err); |
| 123 | exit (1); |
| 124 | } |
| 125 | |
| 126 | puts ("child: got mutex; waiting"); |
| 127 | |
| 128 | pthread_cleanup_push (ch, NULL); |
| 129 | |
| 130 | /* Current time. */ |
| 131 | struct timeval tv; |
| 132 | (void) gettimeofday (&tv, NULL); |
| 133 | /* +1000 seconds in correct format. */ |
| 134 | struct timespec ts; |
| 135 | TIMEVAL_TO_TIMESPEC (&tv, &ts); |
| 136 | ts.tv_sec += 1000; |
| 137 | |
| 138 | pthread_cond_timedwait (&cond, &mut, &ts); |
| 139 | |
| 140 | pthread_cleanup_pop (0); |
| 141 | |
| 142 | puts ("child: cond_wait should not have returned"); |
| 143 | |
| 144 | return NULL; |
| 145 | } |
| 146 | |
| 147 | |
| 148 | static int |
| 149 | do_test (void) |
| 150 | { |
| 151 | pthread_t th; |
| 152 | int err; |
| 153 | |
| 154 | printf ("&cond = %p\n&mut = %p\n", &cond, &mut); |
| 155 | |
| 156 | puts ("parent: get mutex"); |
| 157 | |
| 158 | err = pthread_barrier_init (&bar, NULL, 2); |
| 159 | if (err != 0) |
| 160 | { |
| 161 | puts ("parent: cannot init barrier"); |
| 162 | exit (1); |
| 163 | } |
| 164 | |
| 165 | puts ("parent: create child"); |
| 166 | |
| 167 | err = pthread_create (&th, NULL, tf1, NULL); |
| 168 | if (err != 0) |
| 169 | { |
| 170 | puts ("parent: cannot create thread"); |
| 171 | exit (1); |
| 172 | } |
| 173 | |
| 174 | puts ("parent: wait for child to lock mutex"); |
| 175 | |
| 176 | err = pthread_barrier_wait (&bar); |
| 177 | if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD) |
| 178 | { |
| 179 | puts ("parent: cannot wait for barrier"); |
| 180 | exit (1); |
| 181 | } |
| 182 | |
| 183 | err = pthread_mutex_lock (&mut); |
| 184 | if (err != 0) |
| 185 | { |
| 186 | puts ("parent: mutex_lock failed"); |
| 187 | exit (1); |
| 188 | } |
| 189 | |
| 190 | err = pthread_mutex_unlock (&mut); |
| 191 | if (err != 0) |
| 192 | { |
| 193 | puts ("parent: mutex_unlock failed"); |
| 194 | exit (1); |
| 195 | } |
| 196 | |
| 197 | if (pthread_cancel (th) != 0) |
| 198 | { |
| 199 | puts ("cannot cancel thread"); |
| 200 | exit (1); |
| 201 | } |
| 202 | |
| 203 | void *r; |
| 204 | err = pthread_join (th, &r); |
| 205 | if (err != 0) |
| 206 | { |
| 207 | puts ("parent: failed to join"); |
| 208 | exit (1); |
| 209 | } |
| 210 | |
| 211 | if (r != PTHREAD_CANCELED) |
| 212 | { |
| 213 | puts ("child hasn't been canceled"); |
| 214 | exit (1); |
| 215 | } |
| 216 | |
| 217 | |
| 218 | |
| 219 | puts ("parent: create 2nd child"); |
| 220 | |
| 221 | err = pthread_create (&th, NULL, tf2, NULL); |
| 222 | if (err != 0) |
| 223 | { |
| 224 | puts ("parent: cannot create thread"); |
| 225 | exit (1); |
| 226 | } |
| 227 | |
| 228 | puts ("parent: wait for child to lock mutex"); |
| 229 | |
| 230 | err = pthread_barrier_wait (&bar); |
| 231 | if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD) |
| 232 | { |
| 233 | puts ("parent: cannot wait for barrier"); |
| 234 | exit (1); |
| 235 | } |
| 236 | |
| 237 | err = pthread_mutex_lock (&mut); |
| 238 | if (err != 0) |
| 239 | { |
| 240 | puts ("parent: mutex_lock failed"); |
| 241 | exit (1); |
| 242 | } |
| 243 | |
| 244 | err = pthread_mutex_unlock (&mut); |
| 245 | if (err != 0) |
| 246 | { |
| 247 | puts ("parent: mutex_unlock failed"); |
| 248 | exit (1); |
| 249 | } |
| 250 | |
| 251 | if (pthread_cancel (th) != 0) |
| 252 | { |
| 253 | puts ("cannot cancel thread"); |
| 254 | exit (1); |
| 255 | } |
| 256 | |
| 257 | err = pthread_join (th, &r); |
| 258 | if (err != 0) |
| 259 | { |
| 260 | puts ("parent: failed to join"); |
| 261 | exit (1); |
| 262 | } |
| 263 | |
| 264 | if (r != PTHREAD_CANCELED) |
| 265 | { |
| 266 | puts ("child hasn't been canceled"); |
| 267 | exit (1); |
| 268 | } |
| 269 | |
| 270 | puts ("done"); |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | |
| 276 | #define TEST_FUNCTION do_test () |
| 277 | #include "../test-skeleton.c" |