OLD | NEW |
(Empty) | |
| 1 /* |
| 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 |
| 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 pthread_rwlock_t g_rwlock; |
| 15 volatile int g_thread_has_lock = 0; |
| 16 volatile int g_thread_should_acquire_lock = 0; |
| 17 volatile int g_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 (!g_thread_should_acquire_lock) { /* Spin. */ } |
| 28 |
| 29 ASSERT_EQ(g_thread_has_lock, 0); |
| 30 if (g_thread_should_acquire_lock == WRITE_LOCK) |
| 31 rc = pthread_rwlock_wrlock(&g_rwlock); |
| 32 else |
| 33 rc = pthread_rwlock_rdlock(&g_rwlock); |
| 34 ASSERT_EQ(rc, 0); |
| 35 __sync_fetch_and_add(&g_thread_has_lock, 1); |
| 36 |
| 37 while (!g_thread_should_release_lock) { /* Spin. */ } |
| 38 |
| 39 ASSERT_EQ(g_thread_has_lock, 1); |
| 40 rc = pthread_rwlock_unlock(&g_rwlock); |
| 41 ASSERT_EQ(rc, 0); |
| 42 __sync_fetch_and_sub(&g_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(g_thread_has_lock, 0); |
| 54 ASSERT_EQ(g_thread_should_acquire_lock, 0); |
| 55 __sync_fetch_and_add(&g_thread_should_acquire_lock, lock_type); |
| 56 |
| 57 while (!g_thread_has_lock) { /* Spin. */ } |
| 58 |
| 59 __sync_fetch_and_sub(&g_thread_should_acquire_lock, lock_type); |
| 60 ASSERT_EQ(g_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(g_thread_has_lock, 1); |
| 69 ASSERT_EQ(g_thread_should_release_lock, 0); |
| 70 __sync_fetch_and_add(&g_thread_should_release_lock, 1); |
| 71 |
| 72 while (g_thread_has_lock) { /* Spin. */ } |
| 73 |
| 74 __sync_fetch_and_sub(&g_thread_should_release_lock, 1); |
| 75 ASSERT_EQ(g_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(&g_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(&g_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(&g_rwlock); |
| 110 ASSERT_EQ(rc, EBUSY); |
| 111 |
| 112 tell_thread_to_release_lock(); |
| 113 } |
| 114 |
| 115 void test_recursive_reader(void) { |
| 116 /* |
| 117 * Test that an rdlock can be recursively acquired even when there |
| 118 * is a waiting writer. |
| 119 */ |
| 120 int rc = pthread_rwlock_rdlock(&g_rwlock); |
| 121 ASSERT_EQ(rc, 0); |
| 122 |
| 123 /* |
| 124 * Tell the locking thread to attempt to acquire the write lock. |
| 125 * This should fail and block until all readers are unlocked. |
| 126 */ |
| 127 ASSERT_EQ(g_thread_has_lock, 0); |
| 128 ASSERT_EQ(g_thread_should_acquire_lock, 0); |
| 129 __sync_fetch_and_add(&g_thread_should_acquire_lock, WRITE_LOCK); |
| 130 |
| 131 /* |
| 132 * Sleep for 10ms |
| 133 */ |
| 134 rc = usleep(10 * 1000); |
| 135 ASSERT_EQ(rc, 0); |
| 136 ASSERT_EQ(g_thread_has_lock, 0); |
| 137 |
| 138 /* |
| 139 * Now make sure the waiting writer doesn't block the recursive acquisition |
| 140 * of the rdlock (using both tryrdlock and rdlock). |
| 141 */ |
| 142 rc = pthread_rwlock_tryrdlock(&g_rwlock); |
| 143 ASSERT_EQ(rc, 0); |
| 144 rc = pthread_rwlock_unlock(&g_rwlock); |
| 145 ASSERT_EQ(rc, 0); |
| 146 ASSERT_EQ(g_thread_has_lock, 0); |
| 147 rc = pthread_rwlock_rdlock(&g_rwlock); |
| 148 ASSERT_EQ(rc, 0); |
| 149 rc = pthread_rwlock_unlock(&g_rwlock); |
| 150 ASSERT_EQ(rc, 0); |
| 151 ASSERT_EQ(g_thread_has_lock, 0); |
| 152 |
| 153 /* |
| 154 * Finally unlock the rdlock which should allow the secondary thread |
| 155 * to acquire the wrlock |
| 156 */ |
| 157 rc = pthread_rwlock_unlock(&g_rwlock); |
| 158 ASSERT_EQ(rc, 0); |
| 159 while (!g_thread_has_lock) { /* Spin. */ } |
| 160 __sync_fetch_and_sub(&g_thread_should_acquire_lock, WRITE_LOCK); |
| 161 ASSERT_EQ(g_thread_should_acquire_lock, 0); |
| 162 |
| 163 tell_thread_to_release_lock(); |
| 164 } |
| 165 |
| 166 void test_multiple_readers(void) { |
| 167 fprintf(stderr, "test_multiple_readers\n"); |
| 168 tell_thread_to_acquire_lock(READ_LOCK); |
| 169 |
| 170 /* |
| 171 * Now attempt to acquire the lock on the main thread. |
| 172 * Since they are both readers this should succeed. |
| 173 * Try with tryrdlock, rdlock and timedrdlock. |
| 174 */ |
| 175 int rc = pthread_rwlock_tryrdlock(&g_rwlock); |
| 176 ASSERT_EQ(rc, 0); |
| 177 rc = pthread_rwlock_unlock(&g_rwlock); |
| 178 ASSERT_EQ(rc, 0); |
| 179 |
| 180 struct timespec t = { 0, 0 }; |
| 181 rc = pthread_rwlock_timedrdlock(&g_rwlock, &t); |
| 182 ASSERT_EQ(rc, 0); |
| 183 rc = pthread_rwlock_unlock(&g_rwlock); |
| 184 ASSERT_EQ(rc, 0); |
| 185 |
| 186 rc = pthread_rwlock_rdlock(&g_rwlock); |
| 187 ASSERT_EQ(rc, 0); |
| 188 rc = pthread_rwlock_unlock(&g_rwlock); |
| 189 ASSERT_EQ(rc, 0); |
| 190 |
| 191 tell_thread_to_release_lock(); |
| 192 } |
| 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 * Number of milliseconds delay before we assume that newly created threads |
| 207 * (running read_lock_thread) are blocked on lock acquisition. |
| 208 */ |
| 209 #define THREAD_START_DELAY_MS 20 |
| 210 |
| 211 /* |
| 212 * Test that multiple readers are woken up when the write lock |
| 213 * is released. |
| 214 */ |
| 215 void test_multiple_reader_wakeup(void) { |
| 216 /* |
| 217 * First, acquire the write lock so that all readers |
| 218 * will be blocked. |
| 219 */ |
| 220 fprintf(stderr, "test_multiple_reader_wakeup\n"); |
| 221 int rc; |
| 222 rc = pthread_rwlock_wrlock(&g_rwlock); |
| 223 ASSERT_EQ(rc, 0); |
| 224 |
| 225 /* |
| 226 * Now start two new threads that will both wait on a read lock. |
| 227 */ |
| 228 pthread_t thread1; |
| 229 rc = pthread_create(&thread1, NULL, read_lock_thread, NULL); |
| 230 ASSERT_EQ(rc, 0); |
| 231 |
| 232 pthread_t thread2; |
| 233 rc = pthread_create(&thread2, NULL, read_lock_thread, NULL); |
| 234 ASSERT_EQ(rc, 0); |
| 235 |
| 236 /* |
| 237 * We now need to wait until both threads are waiting on the read lock. |
| 238 * However, we have no way to determine this as we do not record the number |
| 239 * of waiting readers. Sadly, we must resort to an arbitrary delay in the |
| 240 * test code, and hope that after this delay the two threads are both blocked. |
| 241 */ |
| 242 rc = usleep(THREAD_START_DELAY_MS * 1000); |
| 243 ASSERT_EQ(rc, 0); |
| 244 |
| 245 /* |
| 246 * Releasing the write lock should unblock both the readers and they |
| 247 * should then both be joinable. |
| 248 */ |
| 249 rc = pthread_rwlock_unlock(&g_rwlock); |
| 250 |
| 251 pthread_join(thread1, NULL); |
| 252 pthread_join(thread2, NULL); |
| 253 } |
| 254 |
| 255 void test_reader_plus_writer(void) { |
| 256 fprintf(stderr, "test_reader_plus_writer\n"); |
| 257 tell_thread_to_acquire_lock(READ_LOCK); |
| 258 |
| 259 /* |
| 260 * Now attempt to acquire the write lock on the main thread. |
| 261 * This should fail. |
| 262 */ |
| 263 int rc = pthread_rwlock_trywrlock(&g_rwlock); |
| 264 ASSERT_EQ(rc, EBUSY); |
| 265 |
| 266 tell_thread_to_release_lock(); |
| 267 } |
| 268 |
| 269 void test_writer_plus_reader(void) { |
| 270 fprintf(stderr, "test_writer_plus_reader\n"); |
| 271 |
| 272 /* |
| 273 * First get the write lock. |
| 274 */ |
| 275 int rc = pthread_rwlock_wrlock(&g_rwlock); |
| 276 ASSERT_EQ(rc, 0); |
| 277 |
| 278 /* |
| 279 * Attempt to acquire read lock should now fail |
| 280 */ |
| 281 rc = pthread_rwlock_tryrdlock(&g_rwlock); |
| 282 ASSERT_EQ(rc, EBUSY); |
| 283 |
| 284 rc = pthread_rwlock_unlock(&g_rwlock); |
| 285 ASSERT_EQ(rc, 0); |
| 286 } |
| 287 |
| 288 void test_unlocked_with_zero_timestamp(void) { |
| 289 fprintf(stderr, "test_unlocked_with_zero_timestamp\n"); |
| 290 int rc; |
| 291 struct timespec abstime = { 0, 0 }; |
| 292 ASSERT_EQ(g_thread_has_lock, 0); |
| 293 fprintf(stderr, "Trying to lock the unlocked rwlock with a valid " |
| 294 "zero absolute timestamp. " |
| 295 "Expected to succeed instantly since the lock is free.\n"); |
| 296 rc = pthread_rwlock_timedrdlock(&g_rwlock, &abstime); |
| 297 ASSERT_EQ(rc, 0); |
| 298 rc = pthread_rwlock_unlock(&g_rwlock); |
| 299 ASSERT_EQ(rc, 0); |
| 300 } |
| 301 |
| 302 int main(int argc, char **argv) { |
| 303 int rc; |
| 304 fprintf(stderr, "Running...\n"); |
| 305 |
| 306 pthread_rwlockattr_t attrs; |
| 307 rc = pthread_rwlockattr_init(&attrs); |
| 308 ASSERT_EQ(rc, 0); |
| 309 int shared = -1; |
| 310 rc = pthread_rwlockattr_getpshared(&attrs, &shared); |
| 311 ASSERT_EQ(rc, 0); |
| 312 ASSERT_EQ(shared, PTHREAD_PROCESS_PRIVATE); |
| 313 rc = pthread_rwlockattr_setpshared(&attrs, PTHREAD_PROCESS_SHARED); |
| 314 ASSERT_EQ(rc, 0); |
| 315 rc = pthread_rwlockattr_setpshared(&attrs, PTHREAD_PROCESS_PRIVATE); |
| 316 ASSERT_EQ(rc, 0); |
| 317 rc = pthread_rwlock_init(&g_rwlock, &attrs); |
| 318 ASSERT_EQ(rc, 0); |
| 319 rc = pthread_rwlockattr_destroy(&attrs); |
| 320 ASSERT_EQ(rc, 0); |
| 321 |
| 322 pthread_t thread; |
| 323 rc = pthread_create(&thread, NULL, locking_thread, NULL); |
| 324 ASSERT_EQ(rc, 0); |
| 325 fprintf(stderr, "Thread started.\n"); |
| 326 |
| 327 test_unlocked_with_zero_timestamp(); |
| 328 test_multiple_readers(); |
| 329 test_multiple_writers(); |
| 330 test_reader_plus_writer(); |
| 331 test_writer_plus_reader(); |
| 332 test_reader_timedwait(); |
| 333 test_writer_timedwait(); |
| 334 test_recursive_reader(); |
| 335 test_multiple_reader_wakeup(); |
| 336 |
| 337 rc = pthread_rwlock_destroy(&g_rwlock); |
| 338 ASSERT_EQ(rc, 0); |
| 339 fprintf(stderr, "Done.\n"); |
| 340 return 0; |
| 341 } |
OLD | NEW |