Remove powerpc, sparc fdim inlines (bug 22987).
[glibc.git] / nptl / tst-rwlock16.c
blobf11a55fc18a69766f6ff3a8f0235a3f3fbfa5ae3
1 /* Copyright (C) 2015-2018 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 with a reader-preferring rwlock, all readers are woken if
19 one reader "steals" lock ownership from a blocked writer. */
21 #include <errno.h>
22 #include <pthread.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <semaphore.h>
26 #include <unistd.h>
28 /* If we strictly prefer writers over readers, a program must not expect
29 that, in the presence of concurrent writers, one reader will also acquire
30 the lock when another reader has already done so. Thus, use the
31 default rwlock type that does not strictly prefer writers. */
32 static pthread_rwlock_t r = PTHREAD_RWLOCK_INITIALIZER;
34 static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
35 static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
37 /* Avoid using glibc-internal atomic operations. */
38 static sem_t stop;
39 static int consumer_stop = 0;
41 static void *
42 writer (void *arg)
44 int s;
48 if (pthread_rwlock_wrlock (&r) != 0)
50 puts ("wrlock failed");
51 exit (EXIT_FAILURE);
53 if (pthread_rwlock_unlock (&r) != 0)
55 puts ("unlock failed");
56 exit (EXIT_FAILURE);
58 sem_getvalue (&stop, &s);
60 while (s == 0);
61 return NULL;
64 static void *
65 reader_producer (void *arg)
67 int s;
71 if (pthread_rwlock_rdlock (&r) != 0)
73 puts ("rdlock reader failed");
74 exit (EXIT_FAILURE);
77 sem_getvalue (&stop, &s);
79 pthread_mutex_lock (&m);
80 if (s != 0)
81 consumer_stop = 1;
82 pthread_cond_signal (&cv);
83 pthread_mutex_unlock (&m);
85 if (pthread_rwlock_unlock (&r) != 0)
87 puts ("unlock reader failed");
88 exit (EXIT_FAILURE);
91 while (s == 0);
92 puts ("producer finished");
93 return NULL;
96 static void *
97 reader_consumer (void *arg)
99 int s;
103 if (pthread_rwlock_rdlock (&r) != 0)
105 puts ("rdlock reader failed");
106 exit (EXIT_FAILURE);
109 pthread_mutex_lock (&m);
110 s = consumer_stop;
111 if (s == 0)
112 pthread_cond_wait (&cv, &m);
113 pthread_mutex_unlock (&m);
115 if (pthread_rwlock_unlock (&r) != 0)
117 puts ("unlock reader failed");
118 exit (EXIT_FAILURE);
121 while (s == 0);
122 puts ("consumer finished");
123 return NULL;
127 static int
128 do_test (void)
130 pthread_t w1, w2, rp, rc;
132 if (pthread_create (&w1, NULL, writer, NULL) != 0)
134 puts ("create failed");
135 return 1;
137 if (pthread_create (&w2, NULL, writer, NULL) != 0)
139 puts ("create failed");
140 return 1;
142 if (pthread_create (&rc, NULL, reader_consumer, NULL) != 0)
144 puts ("create failed");
145 return 1;
147 if (pthread_create (&rp, NULL, reader_producer, NULL) != 0)
149 puts ("create failed");
150 return 1;
153 sleep (2);
154 sem_post (&stop);
156 if (pthread_join (w1, NULL) != 0)
158 puts ("w1 join failed");
159 return 1;
161 if (pthread_join (w2, NULL) != 0)
163 puts ("w2 join failed");
164 return 1;
166 if (pthread_join (rp, NULL) != 0)
168 puts ("reader_producer join failed");
169 return 1;
171 if (pthread_join (rc, NULL) != 0)
173 puts ("reader_consumer join failed");
174 return 1;
177 return 0;
181 #define TIMEOUT 3
182 #define TEST_FUNCTION do_test ()
183 #include "../test-skeleton.c"