m68k: update libm test ULPs
[glibc.git] / nptl / tst-rwlock15.c
blobb292701e215cf4dd93ebf2c421b26443d6da9c53
1 /* Copyright (C) 2015 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. */
22 #include <errno.h>
23 #include <pthread.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <time.h>
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;
31 static void *
32 writer (void *arg)
34 struct timespec ts;
35 if (clock_gettime (CLOCK_REALTIME, &ts) != 0)
37 puts ("clock_gettime failed");
38 exit (EXIT_FAILURE);
40 ts.tv_sec += 1;
41 int e = pthread_rwlock_timedwrlock (&r, &ts);
42 if (e != ETIMEDOUT)
44 puts ("timedwrlock did not time out");
45 exit (EXIT_FAILURE);
47 return NULL;
50 static void *
51 reader (void *arg)
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)
60 if (e != 0)
61 exit (EXIT_FAILURE);
62 pthread_rwlock_unlock (&r);
64 e = pthread_rwlock_rdlock (&r);
65 if (e != 0)
67 puts ("reader rdlock failed");
68 exit (EXIT_FAILURE);
70 pthread_rwlock_unlock (&r);
71 return NULL;
75 static int
76 do_test (void)
78 /* Grab a rdlock, then create a writer and a reader, and wait until they
79 finished. */
81 if (pthread_rwlock_rdlock (&r) != 0)
83 puts ("initial rdlock failed");
84 return 1;
87 pthread_t thw;
88 if (pthread_create (&thw, NULL, writer, NULL) != 0)
90 puts ("create failed");
91 return 1;
93 pthread_t thr;
94 if (pthread_create (&thr, NULL, reader, NULL) != 0)
96 puts ("create failed");
97 return 1;
100 if (pthread_join (thw, NULL) != 0)
102 puts ("writer join failed");
103 return 1;
105 if (pthread_join (thr, NULL) != 0)
107 puts ("reader join failed");
108 return 1;
111 return 0;
115 #define TEST_FUNCTION do_test ()
116 #include "../test-skeleton.c"