| Index: tests/threads/rwlock_test.c
|
| diff --git a/tests/threads/rwlock_test.c b/tests/threads/rwlock_test.c
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..0d34ea8a58b4386da97b22437cfa947f7375fff0
|
| --- /dev/null
|
| +++ b/tests/threads/rwlock_test.c
|
| @@ -0,0 +1,147 @@
|
| +/*
|
| + * Copyright (c) 2014 The Native Client Authors. All rights reserved.
|
| + * Use of this source code is governed by a BSD-style license that can be
|
| + * found in the LICENSE file.
|
| + */
|
| +
|
| +#include <errno.h>
|
| +#include <pthread.h>
|
| +#include <stdio.h>
|
| +#include <time.h>
|
| +
|
| +#include "native_client/src/include/nacl_assert.h"
|
| +
|
| +static pthread_rwlock_t rwlock;
|
| +volatile int thread_has_lock = 0;
|
| +volatile int thread_should_acquire_lock = 0;
|
| +volatile int thread_should_release_lock = 0;
|
| +
|
| +void *locking_thread(void *unused) {
|
| + for (;;) {
|
| + while (!thread_should_acquire_lock) { /* Spin. */ }
|
| +
|
| + ASSERT_EQ(thread_has_lock, 0);
|
| + int rc = pthread_rwlock_rdlock(&rwlock);
|
| + ASSERT_EQ(rc, 0);
|
| + __sync_fetch_and_add(&thread_has_lock, 1);
|
| +
|
| + while (!thread_should_release_lock) { /* Spin. */ }
|
| +
|
| + ASSERT_EQ(thread_has_lock, 1);
|
| + rc = pthread_rwlock_unlock(&rwlock);
|
| + ASSERT_EQ(rc, 0);
|
| + __sync_fetch_and_sub(&thread_has_lock, 1);
|
| + }
|
| +
|
| + return NULL;
|
| +}
|
| +
|
| +void tell_thread_to_acquire_lock(void) {
|
| + fprintf(stderr, "Thread acquiring lock.\n");
|
| +
|
| + ASSERT_EQ(thread_has_lock, 0);
|
| + ASSERT_EQ(thread_should_acquire_lock, 0);
|
| + __sync_fetch_and_add(&thread_should_acquire_lock, 1);
|
| +
|
| + while (!thread_has_lock) { /* Spin. */ }
|
| +
|
| + __sync_fetch_and_sub(&thread_should_acquire_lock, 1);
|
| + ASSERT_EQ(thread_should_acquire_lock, 0);
|
| +
|
| + fprintf(stderr, "Thread acquired lock.\n");
|
| +}
|
| +
|
| +void tell_thread_to_release_lock(void) {
|
| + fprintf(stderr, "Thread releasing lock.\n");
|
| +
|
| + ASSERT_EQ(thread_has_lock, 1);
|
| + ASSERT_EQ(thread_should_release_lock, 0);
|
| + __sync_fetch_and_add(&thread_should_release_lock, 1);
|
| +
|
| + while (thread_has_lock) { /* Spin. */ }
|
| +
|
| + __sync_fetch_and_sub(&thread_should_release_lock, 1);
|
| + ASSERT_EQ(thread_should_release_lock, 0);
|
| +
|
| + fprintf(stderr, "Thread released lock.\n");
|
| +}
|
| +
|
| +void test_multiple_readers(void) {
|
| + fprintf(stderr, "test_multiple_readers\n");
|
| + tell_thread_to_acquire_lock();
|
| +
|
| + /*
|
| + * Now attempt to acquire the lock on the main thread.
|
| + * Since they are both readers this should succeed.
|
| + * Try with tryrdlock, rdlock and timedrdlock.
|
| + */
|
| + int rc = pthread_rwlock_tryrdlock(&rwlock);
|
| + ASSERT_EQ(rc, 0);
|
| + rc = pthread_rwlock_unlock(&rwlock);
|
| + ASSERT_EQ(rc, 0);
|
| +
|
| + rc = pthread_rwlock_timedrdlock(&rwlock, NULL);
|
| + ASSERT_EQ(rc, 0);
|
| + rc = pthread_rwlock_unlock(&rwlock);
|
| + ASSERT_EQ(rc, 0);
|
| +
|
| + rc = pthread_rwlock_rdlock(&rwlock);
|
| + ASSERT_EQ(rc, 0);
|
| + rc = pthread_rwlock_unlock(&rwlock);
|
| + ASSERT_EQ(rc, 0);
|
| +
|
| + tell_thread_to_release_lock();
|
| +}
|
| +
|
| +void test_reader_plus_writer(void) {
|
| + fprintf(stderr, "test_reader_plus_writer\n");
|
| + tell_thread_to_acquire_lock();
|
| +
|
| + /*
|
| + * Now attempt to acquire the write lock on the main thread.
|
| + * This should fail.
|
| + */
|
| + int rc = pthread_rwlock_trywrlock(&rwlock);
|
| + ASSERT_EQ(rc, EBUSY);
|
| +
|
| + tell_thread_to_release_lock();
|
| +}
|
| +
|
| +void test_unlocked_with_zero_timestamp(void) {
|
| + fprintf(stderr, "test_unlocked_with_zero_timestamp\n");
|
| + int rc;
|
| + struct timespec abstime = { 0, 0 };
|
| + ASSERT_EQ(thread_has_lock, 0);
|
| + fprintf(stderr, "Trying to lock the unlocked rwlock with a valid "
|
| + "zero absolute timestamp. "
|
| + "Expected to succeed instantly since the lock is free.\n");
|
| + rc = pthread_rwlock_timedrdlock(&rwlock, &abstime);
|
| + ASSERT_EQ(rc, 0);
|
| + rc = pthread_rwlock_unlock(&rwlock);
|
| + ASSERT_EQ(rc, 0);
|
| +}
|
| +
|
| +int main(int argc, char **argv) {
|
| + int rc;
|
| + fprintf(stderr, "Running...\n");
|
| +
|
| + pthread_rwlockattr_t attrs;
|
| + rc = pthread_rwlockattr_init(&attrs);
|
| + ASSERT_EQ(rc, 0);
|
| + rc = pthread_rwlock_init(&rwlock, &attrs);
|
| + ASSERT_EQ(rc, 0);
|
| + rc = pthread_rwlockattr_destroy(&attrs);
|
| + ASSERT_EQ(rc, 0);
|
| +
|
| + pthread_t thread;
|
| + rc = pthread_create(&thread, NULL, locking_thread, NULL);
|
| + ASSERT_EQ(rc, 0);
|
| + fprintf(stderr, "Thread started.\n");
|
| +
|
| + test_unlocked_with_zero_timestamp();
|
| + test_multiple_readers();
|
| + test_reader_plus_writer();
|
| +
|
| + fprintf(stderr, "Done.\n");
|
| + return 0;
|
| +}
|
|
|