lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 2015 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | |
| 4 | The GNU C Library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | The GNU C Library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with the GNU C Library; if not, see |
| 16 | <http://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | /* This tests that a writer that is preferred -- but times out due to a |
| 19 | reader being present -- does not miss to wake other readers blocked on the |
| 20 | writer's pending lock acquisition. */ |
| 21 | |
| 22 | #include <errno.h> |
| 23 | #include <pthread.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <time.h> |
| 27 | |
| 28 | /* The bug existed in the code that strictly prefers writers over readers. */ |
| 29 | static pthread_rwlock_t r = PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP; |
| 30 | |
| 31 | static void * |
| 32 | writer (void *arg) |
| 33 | { |
| 34 | struct timespec ts; |
| 35 | if (clock_gettime (CLOCK_REALTIME, &ts) != 0) |
| 36 | { |
| 37 | puts ("clock_gettime failed"); |
| 38 | exit (EXIT_FAILURE); |
| 39 | } |
| 40 | ts.tv_sec += 1; |
| 41 | int e = pthread_rwlock_timedwrlock (&r, &ts); |
| 42 | if (e != ETIMEDOUT) |
| 43 | { |
| 44 | puts ("timedwrlock did not time out"); |
| 45 | exit (EXIT_FAILURE); |
| 46 | } |
| 47 | return NULL; |
| 48 | } |
| 49 | |
| 50 | static void * |
| 51 | reader (void *arg) |
| 52 | { |
| 53 | /* This isn't a reliable way to get the interleaving we need (because a |
| 54 | failed trylock doesn't synchronize with the writer, and because we could |
| 55 | try to lock after the writer has already timed out). However, both will |
| 56 | just lead to false positives. */ |
| 57 | int e; |
| 58 | while ((e = pthread_rwlock_tryrdlock (&r)) != EBUSY) |
| 59 | { |
| 60 | if (e != 0) |
| 61 | exit (EXIT_FAILURE); |
| 62 | pthread_rwlock_unlock (&r); |
| 63 | } |
| 64 | e = pthread_rwlock_rdlock (&r); |
| 65 | if (e != 0) |
| 66 | { |
| 67 | puts ("reader rdlock failed"); |
| 68 | exit (EXIT_FAILURE); |
| 69 | } |
| 70 | pthread_rwlock_unlock (&r); |
| 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | static int |
| 76 | do_test (void) |
| 77 | { |
| 78 | /* Grab a rdlock, then create a writer and a reader, and wait until they |
| 79 | finished. */ |
| 80 | |
| 81 | if (pthread_rwlock_rdlock (&r) != 0) |
| 82 | { |
| 83 | puts ("initial rdlock failed"); |
| 84 | return 1; |
| 85 | } |
| 86 | |
| 87 | pthread_t thw; |
| 88 | if (pthread_create (&thw, NULL, writer, NULL) != 0) |
| 89 | { |
| 90 | puts ("create failed"); |
| 91 | return 1; |
| 92 | } |
| 93 | pthread_t thr; |
| 94 | if (pthread_create (&thr, NULL, reader, NULL) != 0) |
| 95 | { |
| 96 | puts ("create failed"); |
| 97 | return 1; |
| 98 | } |
| 99 | |
| 100 | if (pthread_join (thw, NULL) != 0) |
| 101 | { |
| 102 | puts ("writer join failed"); |
| 103 | return 1; |
| 104 | } |
| 105 | if (pthread_join (thr, NULL) != 0) |
| 106 | { |
| 107 | puts ("reader join failed"); |
| 108 | return 1; |
| 109 | } |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | #define TEST_FUNCTION do_test () |
| 116 | #include "../test-skeleton.c" |