1 /* Copyright (C) 2015-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>. */
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. */
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
;
35 if (clock_gettime (CLOCK_REALTIME
, &ts
) != 0)
37 puts ("clock_gettime failed");
41 int e
= pthread_rwlock_timedwrlock (&r
, &ts
);
44 puts ("timedwrlock did not time out");
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. */
58 while ((e
= pthread_rwlock_tryrdlock (&r
)) != EBUSY
)
62 pthread_rwlock_unlock (&r
);
64 e
= pthread_rwlock_rdlock (&r
);
67 puts ("reader rdlock failed");
70 pthread_rwlock_unlock (&r
);
78 /* Grab a rdlock, then create a writer and a reader, and wait until they
81 if (pthread_rwlock_rdlock (&r
) != 0)
83 puts ("initial rdlock failed");
88 if (pthread_create (&thw
, NULL
, writer
, NULL
) != 0)
90 puts ("create failed");
94 if (pthread_create (&thr
, NULL
, reader
, NULL
) != 0)
96 puts ("create failed");
100 if (pthread_join (thw
, NULL
) != 0)
102 puts ("writer join failed");
105 if (pthread_join (thr
, NULL
) != 0)
107 puts ("reader join failed");
115 #define TEST_FUNCTION do_test ()
116 #include "../test-skeleton.c"