OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2015 The Native Client Authors. All rights reserved. | 2 * Copyright 2015 The Native Client Authors. All rights reserved. |
3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
5 */ | 5 */ |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <pthread.h> | 8 #include <pthread.h> |
9 #include <stdio.h> | 9 #include <stdio.h> |
10 #include <time.h> | 10 #include <time.h> |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
184 ASSERT_EQ(rc, 0); | 184 ASSERT_EQ(rc, 0); |
185 | 185 |
186 rc = pthread_rwlock_rdlock(&g_rwlock); | 186 rc = pthread_rwlock_rdlock(&g_rwlock); |
187 ASSERT_EQ(rc, 0); | 187 ASSERT_EQ(rc, 0); |
188 rc = pthread_rwlock_unlock(&g_rwlock); | 188 rc = pthread_rwlock_unlock(&g_rwlock); |
189 ASSERT_EQ(rc, 0); | 189 ASSERT_EQ(rc, 0); |
190 | 190 |
191 tell_thread_to_release_lock(); | 191 tell_thread_to_release_lock(); |
192 } | 192 } |
193 | 193 |
194 void *read_lock_thread(void *unused) { | |
195 fprintf(stderr, "%lx: waiting for read lock\n", (long)pthread_self()); | |
196 int rc = pthread_rwlock_rdlock(&g_rwlock); | |
197 fprintf(stderr, "%lx: read lock acquired\n", (long)pthread_self()); | |
198 ASSERT_EQ(rc, 0); | |
199 rc = pthread_rwlock_unlock(&g_rwlock); | |
200 ASSERT_EQ(rc, 0); | |
201 fprintf(stderr, "%lx: thread done\n", (long)pthread_self()); | |
202 return NULL; | |
203 } | |
204 | |
205 /* | |
206 * Test that multiple readers are woken up when the write lock | |
207 * is released. | |
208 */ | |
209 void test_multiple_reader_wakeup(void) { | |
210 /* | |
211 * First, aquire the write lock so that all readers | |
Roland McGrath
2015/03/23 20:34:07
typo: "acquire"
Sam Clegg
2015/03/23 20:55:21
Done.
| |
212 * will be blocked. | |
213 */ | |
214 fprintf(stderr, "test_multiple_reader_wakeup\n"); | |
215 int rc; | |
216 rc = pthread_rwlock_wrlock(&g_rwlock); | |
217 ASSERT_EQ(rc, 0); | |
218 | |
219 /* | |
220 * Now start two new threads that which will both | |
Roland McGrath
2015/03/23 20:34:07
s/that which/that/
Sam Clegg
2015/03/23 20:55:21
Done.
| |
221 * wait on a read lock. | |
222 */ | |
223 pthread_t thread1; | |
224 rc = pthread_create(&thread1, NULL, read_lock_thread, NULL); | |
225 ASSERT_EQ(rc, 0); | |
226 | |
227 pthread_t thread2; | |
228 rc = pthread_create(&thread2, NULL, read_lock_thread, NULL); | |
229 ASSERT_EQ(rc, 0); | |
230 | |
231 /* | |
232 * Sleep for 20ms to allow both readers to become blocked on rdlock() | |
Roland McGrath
2015/03/23 20:34:07
Put this duration in a macro so it's clear it can
Sam Clegg
2015/03/23 20:55:21
Done.
| |
233 */ | |
234 rc = usleep(20 * 1000); | |
235 ASSERT_EQ(rc, 0); | |
236 | |
237 /* | |
238 * Releasing the write lock should unblock both the readers and they | |
239 * should then both be joinable. | |
240 */ | |
241 rc = pthread_rwlock_unlock(&g_rwlock); | |
242 | |
243 pthread_join(thread1, NULL); | |
244 pthread_join(thread2, NULL); | |
245 } | |
246 | |
194 void test_reader_plus_writer(void) { | 247 void test_reader_plus_writer(void) { |
195 fprintf(stderr, "test_reader_plus_writer\n"); | 248 fprintf(stderr, "test_reader_plus_writer\n"); |
196 tell_thread_to_acquire_lock(READ_LOCK); | 249 tell_thread_to_acquire_lock(READ_LOCK); |
197 | 250 |
198 /* | 251 /* |
199 * Now attempt to acquire the write lock on the main thread. | 252 * Now attempt to acquire the write lock on the main thread. |
200 * This should fail. | 253 * This should fail. |
201 */ | 254 */ |
202 int rc = pthread_rwlock_trywrlock(&g_rwlock); | 255 int rc = pthread_rwlock_trywrlock(&g_rwlock); |
203 ASSERT_EQ(rc, EBUSY); | 256 ASSERT_EQ(rc, EBUSY); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
264 fprintf(stderr, "Thread started.\n"); | 317 fprintf(stderr, "Thread started.\n"); |
265 | 318 |
266 test_unlocked_with_zero_timestamp(); | 319 test_unlocked_with_zero_timestamp(); |
267 test_multiple_readers(); | 320 test_multiple_readers(); |
268 test_multiple_writers(); | 321 test_multiple_writers(); |
269 test_reader_plus_writer(); | 322 test_reader_plus_writer(); |
270 test_writer_plus_reader(); | 323 test_writer_plus_reader(); |
271 test_reader_timedwait(); | 324 test_reader_timedwait(); |
272 test_writer_timedwait(); | 325 test_writer_timedwait(); |
273 test_recursive_reader(); | 326 test_recursive_reader(); |
327 test_multiple_reader_wakeup(); | |
274 | 328 |
275 rc = pthread_rwlock_destroy(&g_rwlock); | 329 rc = pthread_rwlock_destroy(&g_rwlock); |
276 ASSERT_EQ(rc, 0); | 330 ASSERT_EQ(rc, 0); |
277 fprintf(stderr, "Done.\n"); | 331 fprintf(stderr, "Done.\n"); |
278 return 0; | 332 return 0; |
279 } | 333 } |
OLD | NEW |