Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: tests/threads/rwlock_test.c

Issue 609363003: Add implementation of pthread_rwlock based on NaCl futexs (Closed) Base URL: svn://svn.chromium.org/native_client/trunk/src/native_client
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tests/threads/nacl.scons ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 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 void *locking_thread(void *unused) {
20 for (;;) {
21 while (!thread_should_acquire_lock) { /* Spin. */ }
22
23 ASSERT_EQ(thread_has_lock, 0);
24 int rc = pthread_rwlock_rdlock(&rwlock);
25 ASSERT_EQ(rc, 0);
26 __sync_fetch_and_add(&thread_has_lock, 1);
27
28 while (!thread_should_release_lock) { /* Spin. */ }
29
30 ASSERT_EQ(thread_has_lock, 1);
31 rc = pthread_rwlock_unlock(&rwlock);
32 ASSERT_EQ(rc, 0);
33 __sync_fetch_and_sub(&thread_has_lock, 1);
34 }
35
36 return NULL;
37 }
38
39 void tell_thread_to_acquire_lock(void) {
40 fprintf(stderr, "Thread acquiring lock.\n");
41
42 ASSERT_EQ(thread_has_lock, 0);
43 ASSERT_EQ(thread_should_acquire_lock, 0);
44 __sync_fetch_and_add(&thread_should_acquire_lock, 1);
45
46 while (!thread_has_lock) { /* Spin. */ }
47
48 __sync_fetch_and_sub(&thread_should_acquire_lock, 1);
49 ASSERT_EQ(thread_should_acquire_lock, 0);
50
51 fprintf(stderr, "Thread acquired lock.\n");
52 }
53
54 void tell_thread_to_release_lock(void) {
55 fprintf(stderr, "Thread releasing lock.\n");
56
57 ASSERT_EQ(thread_has_lock, 1);
58 ASSERT_EQ(thread_should_release_lock, 0);
59 __sync_fetch_and_add(&thread_should_release_lock, 1);
60
61 while (thread_has_lock) { /* Spin. */ }
62
63 __sync_fetch_and_sub(&thread_should_release_lock, 1);
64 ASSERT_EQ(thread_should_release_lock, 0);
65
66 fprintf(stderr, "Thread released lock.\n");
67 }
68
69 void test_multiple_readers(void) {
70 fprintf(stderr, "test_multiple_readers\n");
71 tell_thread_to_acquire_lock();
72
73 /*
74 * Now attempt to acquire the lock on the main thread.
75 * Since they are both readers this should succeed.
76 * Try with tryrdlock, rdlock and timedrdlock.
77 */
78 int rc = pthread_rwlock_tryrdlock(&rwlock);
79 ASSERT_EQ(rc, 0);
80 rc = pthread_rwlock_unlock(&rwlock);
81 ASSERT_EQ(rc, 0);
82
83 rc = pthread_rwlock_timedrdlock(&rwlock, NULL);
84 ASSERT_EQ(rc, 0);
85 rc = pthread_rwlock_unlock(&rwlock);
86 ASSERT_EQ(rc, 0);
87
88 rc = pthread_rwlock_rdlock(&rwlock);
89 ASSERT_EQ(rc, 0);
90 rc = pthread_rwlock_unlock(&rwlock);
91 ASSERT_EQ(rc, 0);
92
93 tell_thread_to_release_lock();
94 }
95
96 void test_reader_plus_writer(void) {
97 fprintf(stderr, "test_reader_plus_writer\n");
98 tell_thread_to_acquire_lock();
99
100 /*
101 * Now attempt to acquire the write lock on the main thread.
102 * This should fail.
103 */
104 int rc = pthread_rwlock_trywrlock(&rwlock);
105 ASSERT_EQ(rc, EBUSY);
106
107 tell_thread_to_release_lock();
108 }
109
110 void test_unlocked_with_zero_timestamp(void) {
111 fprintf(stderr, "test_unlocked_with_zero_timestamp\n");
112 int rc;
113 struct timespec abstime = { 0, 0 };
114 ASSERT_EQ(thread_has_lock, 0);
115 fprintf(stderr, "Trying to lock the unlocked rwlock with a valid "
116 "zero absolute timestamp. "
117 "Expected to succeed instantly since the lock is free.\n");
118 rc = pthread_rwlock_timedrdlock(&rwlock, &abstime);
119 ASSERT_EQ(rc, 0);
120 rc = pthread_rwlock_unlock(&rwlock);
121 ASSERT_EQ(rc, 0);
122 }
123
124 int main(int argc, char **argv) {
125 int rc;
126 fprintf(stderr, "Running...\n");
127
128 pthread_rwlockattr_t attrs;
129 rc = pthread_rwlockattr_init(&attrs);
130 ASSERT_EQ(rc, 0);
131 rc = pthread_rwlock_init(&rwlock, &attrs);
132 ASSERT_EQ(rc, 0);
133 rc = pthread_rwlockattr_destroy(&attrs);
134 ASSERT_EQ(rc, 0);
135
136 pthread_t thread;
137 rc = pthread_create(&thread, NULL, locking_thread, NULL);
138 ASSERT_EQ(rc, 0);
139 fprintf(stderr, "Thread started.\n");
140
141 test_unlocked_with_zero_timestamp();
142 test_multiple_readers();
143 test_reader_plus_writer();
144
145 fprintf(stderr, "Done.\n");
146 return 0;
147 }
OLDNEW
« no previous file with comments | « tests/threads/nacl.scons ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698