OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2014 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. |
| 5 */ |
| 6 |
| 7 #include <errno.h> |
| 8 #include <pthread.h> |
| 9 #include <stdio.h> |
| 10 #include <time.h> |
| 11 |
| 12 #include "native_client/src/include/nacl_assert.h" |
| 13 |
| 14 static pthread_rwlock_t rwlock; |
| 15 volatile int thread_has_lock = 0; |
| 16 volatile int thread_should_acquire_lock = 0; |
| 17 volatile int thread_should_release_lock = 0; |
| 18 |
| 19 typedef enum { |
| 20 READ_LOCK = 1, |
| 21 WRITE_LOCK = 2, |
| 22 } lock_type; |
| 23 |
| 24 void *locking_thread(void *unused) { |
| 25 int rc; |
| 26 for (;;) { |
| 27 while (!thread_should_acquire_lock) { /* Spin. */ } |
| 28 |
| 29 ASSERT_EQ(thread_has_lock, 0); |
| 30 if (thread_should_acquire_lock == WRITE_LOCK) |
| 31 rc = pthread_rwlock_wrlock(&rwlock); |
| 32 else |
| 33 rc = pthread_rwlock_rdlock(&rwlock); |
| 34 ASSERT_EQ(rc, 0); |
| 35 __sync_fetch_and_add(&thread_has_lock, 1); |
| 36 |
| 37 while (!thread_should_release_lock) { /* Spin. */ } |
| 38 |
| 39 ASSERT_EQ(thread_has_lock, 1); |
| 40 rc = pthread_rwlock_unlock(&rwlock); |
| 41 ASSERT_EQ(rc, 0); |
| 42 __sync_fetch_and_sub(&thread_has_lock, 1); |
| 43 } |
| 44 |
| 45 return NULL; |
| 46 } |
| 47 |
| 48 |
| 49 void tell_thread_to_acquire_lock(lock_type lock_type) { |
| 50 fprintf(stderr, "Thread acquiring lock: %s\n", |
| 51 lock_type == WRITE_LOCK ? "WRITE" : "READ" ); |
| 52 |
| 53 ASSERT_EQ(thread_has_lock, 0); |
| 54 ASSERT_EQ(thread_should_acquire_lock, 0); |
| 55 __sync_fetch_and_add(&thread_should_acquire_lock, lock_type); |
| 56 |
| 57 while (!thread_has_lock) { /* Spin. */ } |
| 58 |
| 59 __sync_fetch_and_sub(&thread_should_acquire_lock, lock_type); |
| 60 ASSERT_EQ(thread_should_acquire_lock, 0); |
| 61 |
| 62 fprintf(stderr, "Thread acquired lock.\n"); |
| 63 } |
| 64 |
| 65 void tell_thread_to_release_lock(void) { |
| 66 fprintf(stderr, "Thread releasing lock.\n"); |
| 67 |
| 68 ASSERT_EQ(thread_has_lock, 1); |
| 69 ASSERT_EQ(thread_should_release_lock, 0); |
| 70 __sync_fetch_and_add(&thread_should_release_lock, 1); |
| 71 |
| 72 while (thread_has_lock) { /* Spin. */ } |
| 73 |
| 74 __sync_fetch_and_sub(&thread_should_release_lock, 1); |
| 75 ASSERT_EQ(thread_should_release_lock, 0); |
| 76 |
| 77 fprintf(stderr, "Thread released lock.\n"); |
| 78 } |
| 79 |
| 80 void test_reader_timedwait(void) { |
| 81 fprintf(stderr, "test_reader_timedwait\n"); |
| 82 tell_thread_to_acquire_lock(WRITE_LOCK); |
| 83 |
| 84 struct timespec t = { 0, 0 }; |
| 85 int rc = pthread_rwlock_timedrdlock(&rwlock, &t); |
| 86 ASSERT_EQ(rc, ETIMEDOUT); |
| 87 |
| 88 tell_thread_to_release_lock(); |
| 89 } |
| 90 |
| 91 void test_writer_timedwait(void) { |
| 92 fprintf(stderr, "test_writer_timedwait\n"); |
| 93 tell_thread_to_acquire_lock(READ_LOCK); |
| 94 |
| 95 struct timespec t = { 0, 0 }; |
| 96 int rc = pthread_rwlock_timedwrlock(&rwlock, &t); |
| 97 ASSERT_EQ(rc, ETIMEDOUT); |
| 98 |
| 99 tell_thread_to_release_lock(); |
| 100 } |
| 101 |
| 102 void test_multiple_writers(void) { |
| 103 fprintf(stderr, "test_multiple_writers\n"); |
| 104 tell_thread_to_acquire_lock(WRITE_LOCK); |
| 105 |
| 106 /* |
| 107 * Attempt to acquire second write lock should fail. |
| 108 */ |
| 109 int rc = pthread_rwlock_trywrlock(&rwlock); |
| 110 ASSERT_EQ(rc, EBUSY); |
| 111 |
| 112 tell_thread_to_release_lock(); |
| 113 } |
| 114 |
| 115 void test_multiple_readers(void) { |
| 116 fprintf(stderr, "test_multiple_readers\n"); |
| 117 tell_thread_to_acquire_lock(READ_LOCK); |
| 118 |
| 119 /* |
| 120 * Now attempt to acquire the lock on the main thread. |
| 121 * Since they are both readers this should succeed. |
| 122 * Try with tryrdlock, rdlock and timedrdlock. |
| 123 */ |
| 124 int rc = pthread_rwlock_tryrdlock(&rwlock); |
| 125 ASSERT_EQ(rc, 0); |
| 126 rc = pthread_rwlock_unlock(&rwlock); |
| 127 ASSERT_EQ(rc, 0); |
| 128 |
| 129 struct timespec t = { 0, 0 }; |
| 130 rc = pthread_rwlock_timedrdlock(&rwlock, &t); |
| 131 ASSERT_EQ(rc, 0); |
| 132 rc = pthread_rwlock_unlock(&rwlock); |
| 133 ASSERT_EQ(rc, 0); |
| 134 |
| 135 rc = pthread_rwlock_rdlock(&rwlock); |
| 136 ASSERT_EQ(rc, 0); |
| 137 rc = pthread_rwlock_unlock(&rwlock); |
| 138 ASSERT_EQ(rc, 0); |
| 139 |
| 140 tell_thread_to_release_lock(); |
| 141 } |
| 142 |
| 143 void test_reader_plus_writer(void) { |
| 144 fprintf(stderr, "test_reader_plus_writer\n"); |
| 145 tell_thread_to_acquire_lock(READ_LOCK); |
| 146 |
| 147 /* |
| 148 * Now attempt to acquire the write lock on the main thread. |
| 149 * This should fail. |
| 150 */ |
| 151 int rc = pthread_rwlock_trywrlock(&rwlock); |
| 152 ASSERT_EQ(rc, EBUSY); |
| 153 |
| 154 tell_thread_to_release_lock(); |
| 155 } |
| 156 |
| 157 void test_writer_plus_reader(void) { |
| 158 fprintf(stderr, "test_writer_plus_reader\n"); |
| 159 |
| 160 /* |
| 161 * First get the write lock. |
| 162 */ |
| 163 int rc = pthread_rwlock_wrlock(&rwlock); |
| 164 ASSERT_EQ(rc, 0); |
| 165 |
| 166 /* |
| 167 * Attempt to acquire read lock should now fail |
| 168 */ |
| 169 rc = pthread_rwlock_tryrdlock(&rwlock); |
| 170 ASSERT_EQ(rc, EBUSY); |
| 171 |
| 172 rc = pthread_rwlock_unlock(&rwlock); |
| 173 ASSERT_EQ(rc, 0); |
| 174 } |
| 175 |
| 176 void test_unlocked_with_zero_timestamp(void) { |
| 177 fprintf(stderr, "test_unlocked_with_zero_timestamp\n"); |
| 178 int rc; |
| 179 struct timespec abstime = { 0, 0 }; |
| 180 ASSERT_EQ(thread_has_lock, 0); |
| 181 fprintf(stderr, "Trying to lock the unlocked rwlock with a valid " |
| 182 "zero absolute timestamp. " |
| 183 "Expected to succeed instantly since the lock is free.\n"); |
| 184 rc = pthread_rwlock_timedrdlock(&rwlock, &abstime); |
| 185 ASSERT_EQ(rc, 0); |
| 186 rc = pthread_rwlock_unlock(&rwlock); |
| 187 ASSERT_EQ(rc, 0); |
| 188 } |
| 189 |
| 190 int main(int argc, char **argv) { |
| 191 int rc; |
| 192 fprintf(stderr, "Running...\n"); |
| 193 |
| 194 pthread_rwlockattr_t attrs; |
| 195 rc = pthread_rwlockattr_init(&attrs); |
| 196 ASSERT_EQ(rc, 0); |
| 197 int shared = -1; |
| 198 rc = pthread_rwlockattr_getpshared(&attrs, &shared); |
| 199 ASSERT_EQ(rc, 0); |
| 200 ASSERT_EQ(shared, PTHREAD_PROCESS_PRIVATE); |
| 201 rc = pthread_rwlockattr_setpshared(&attrs, PTHREAD_PROCESS_SHARED); |
| 202 ASSERT_EQ(rc, 0); |
| 203 rc = pthread_rwlockattr_setpshared(&attrs, PTHREAD_PROCESS_PRIVATE); |
| 204 ASSERT_EQ(rc, 0); |
| 205 rc = pthread_rwlock_init(&rwlock, &attrs); |
| 206 ASSERT_EQ(rc, 0); |
| 207 rc = pthread_rwlockattr_destroy(&attrs); |
| 208 ASSERT_EQ(rc, 0); |
| 209 |
| 210 pthread_t thread; |
| 211 rc = pthread_create(&thread, NULL, locking_thread, NULL); |
| 212 ASSERT_EQ(rc, 0); |
| 213 fprintf(stderr, "Thread started.\n"); |
| 214 |
| 215 test_unlocked_with_zero_timestamp(); |
| 216 test_multiple_readers(); |
| 217 test_multiple_writers(); |
| 218 test_reader_plus_writer(); |
| 219 test_writer_plus_reader(); |
| 220 test_reader_timedwait(); |
| 221 test_writer_timedwait(); |
| 222 |
| 223 rc = pthread_rwlock_destroy(&rwlock); |
| 224 ASSERT_EQ(rc, 0); |
| 225 fprintf(stderr, "Done.\n"); |
| 226 return 0; |
| 227 } |
OLD | NEW |