Index: tests/threads/rwlock_test.c |
diff --git a/tests/threads/rwlock_test.c b/tests/threads/rwlock_test.c |
index fe763abc4a4a4da111f8576727c64b339f0ef467..e3f6b79f09e13af040429d364e203859f0e552e8 100644 |
--- a/tests/threads/rwlock_test.c |
+++ b/tests/threads/rwlock_test.c |
@@ -191,6 +191,59 @@ void test_multiple_readers(void) { |
tell_thread_to_release_lock(); |
} |
+void *read_lock_thread(void *unused) { |
+ fprintf(stderr, "%lx: waiting for read lock\n", (long)pthread_self()); |
+ int rc = pthread_rwlock_rdlock(&g_rwlock); |
+ fprintf(stderr, "%lx: read lock acquired\n", (long)pthread_self()); |
+ ASSERT_EQ(rc, 0); |
+ rc = pthread_rwlock_unlock(&g_rwlock); |
+ ASSERT_EQ(rc, 0); |
+ fprintf(stderr, "%lx: thread done\n", (long)pthread_self()); |
+ return NULL; |
+} |
+ |
+/* |
+ * Test that multiple readers are woken up when the write lock |
+ * is released. |
+ */ |
+void test_multiple_reader_wakeup(void) { |
+ /* |
+ * 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.
|
+ * will be blocked. |
+ */ |
+ fprintf(stderr, "test_multiple_reader_wakeup\n"); |
+ int rc; |
+ rc = pthread_rwlock_wrlock(&g_rwlock); |
+ ASSERT_EQ(rc, 0); |
+ |
+ /* |
+ * 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.
|
+ * wait on a read lock. |
+ */ |
+ pthread_t thread1; |
+ rc = pthread_create(&thread1, NULL, read_lock_thread, NULL); |
+ ASSERT_EQ(rc, 0); |
+ |
+ pthread_t thread2; |
+ rc = pthread_create(&thread2, NULL, read_lock_thread, NULL); |
+ ASSERT_EQ(rc, 0); |
+ |
+ /* |
+ * 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.
|
+ */ |
+ rc = usleep(20 * 1000); |
+ ASSERT_EQ(rc, 0); |
+ |
+ /* |
+ * Releasing the write lock should unblock both the readers and they |
+ * should then both be joinable. |
+ */ |
+ rc = pthread_rwlock_unlock(&g_rwlock); |
+ |
+ pthread_join(thread1, NULL); |
+ pthread_join(thread2, NULL); |
+} |
+ |
void test_reader_plus_writer(void) { |
fprintf(stderr, "test_reader_plus_writer\n"); |
tell_thread_to_acquire_lock(READ_LOCK); |
@@ -271,6 +324,7 @@ int main(int argc, char **argv) { |
test_reader_timedwait(); |
test_writer_timedwait(); |
test_recursive_reader(); |
+ test_multiple_reader_wakeup(); |
rc = pthread_rwlock_destroy(&g_rwlock); |
ASSERT_EQ(rc, 0); |