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

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

Issue 623863002: Implement pthread_rwlock functions for NaCl newlib. (Closed) Base URL: svn://svn.chromium.org/native_client/trunk/src/native_client
Patch Set: Created 5 years, 10 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
OLDNEW
(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_recursive_reader(void) {
116 /*
117 * Test that the an rdlock can be recursively aquired
Mark Seaborn 2015/02/02 18:29:13 "acquired" Remove "the"
Sam Clegg 2015/02/02 19:25:09 Done.
118 * even when there is a waiting writer.
119 */
120 ASSERT_EQ(0, pthread_rwlock_rdlock(&rwlock));
121
122 /*
123 * Tell the locking thread to attempt to aquire the write lock.
Mark Seaborn 2015/02/02 18:29:14 "acquire". Same below.
Sam Clegg 2015/02/02 19:25:09 Done.
124 * This should fail and block until all readers are unlocked.
125 */
126 ASSERT_EQ(thread_has_lock, 0);
127 ASSERT_EQ(thread_should_acquire_lock, 0);
128 __sync_fetch_and_add(&thread_should_acquire_lock, WRITE_LOCK);
129
130 /*
131 * Sleep for 10ms
132 */
133 usleep(10 * 1000);
Mark Seaborn 2015/02/02 18:29:14 Nit: check return value
Sam Clegg 2015/02/02 19:25:09 Done.
134 ASSERT_EQ(thread_has_lock, 0);
135
136 /*
137 * Now make sure the waiting writer doesn't block the recursive acquisition
138 * of the rdlock (using both tryrdlock and rdlock).
139 */
140 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&rwlock));
141 ASSERT_EQ(0, pthread_rwlock_unlock(&rwlock));
142 ASSERT_EQ(thread_has_lock, 0);
143 ASSERT_EQ(0, pthread_rwlock_rdlock(&rwlock));
144 ASSERT_EQ(0, pthread_rwlock_unlock(&rwlock));
145 ASSERT_EQ(thread_has_lock, 0);
146
147 /*
148 * Finally unlock the rdlock which should allow the secondary thread
149 * to aquire the wrlock
150 */
151 ASSERT_EQ(0, pthread_rwlock_unlock(&rwlock));
152 while (!thread_has_lock) { /* Spin. */ }
153 __sync_fetch_and_sub(&thread_should_acquire_lock, WRITE_LOCK);
154 ASSERT_EQ(thread_should_acquire_lock, 0);
155
156 tell_thread_to_release_lock();
157 }
158
159 void test_multiple_readers(void) {
160 fprintf(stderr, "test_multiple_readers\n");
161 tell_thread_to_acquire_lock(READ_LOCK);
162
163 /*
164 * Now attempt to acquire the lock on the main thread.
165 * Since they are both readers this should succeed.
166 * Try with tryrdlock, rdlock and timedrdlock.
167 */
168 int rc = pthread_rwlock_tryrdlock(&rwlock);
169 ASSERT_EQ(rc, 0);
170 rc = pthread_rwlock_unlock(&rwlock);
171 ASSERT_EQ(rc, 0);
172
173 struct timespec t = { 0, 0 };
174 rc = pthread_rwlock_timedrdlock(&rwlock, &t);
175 ASSERT_EQ(rc, 0);
176 rc = pthread_rwlock_unlock(&rwlock);
177 ASSERT_EQ(rc, 0);
178
179 rc = pthread_rwlock_rdlock(&rwlock);
180 ASSERT_EQ(rc, 0);
181 rc = pthread_rwlock_unlock(&rwlock);
182 ASSERT_EQ(rc, 0);
183
184 tell_thread_to_release_lock();
185 }
186
187 void test_reader_plus_writer(void) {
188 fprintf(stderr, "test_reader_plus_writer\n");
189 tell_thread_to_acquire_lock(READ_LOCK);
190
191 /*
192 * Now attempt to acquire the write lock on the main thread.
193 * This should fail.
194 */
195 int rc = pthread_rwlock_trywrlock(&rwlock);
196 ASSERT_EQ(rc, EBUSY);
197
198 tell_thread_to_release_lock();
199 }
200
201 void test_writer_plus_reader(void) {
202 fprintf(stderr, "test_writer_plus_reader\n");
203
204 /*
205 * First get the write lock.
206 */
207 int rc = pthread_rwlock_wrlock(&rwlock);
208 ASSERT_EQ(rc, 0);
209
210 /*
211 * Attempt to acquire read lock should now fail
212 */
213 rc = pthread_rwlock_tryrdlock(&rwlock);
214 ASSERT_EQ(rc, EBUSY);
215
216 rc = pthread_rwlock_unlock(&rwlock);
217 ASSERT_EQ(rc, 0);
218 }
219
220 void test_unlocked_with_zero_timestamp(void) {
221 fprintf(stderr, "test_unlocked_with_zero_timestamp\n");
222 int rc;
223 struct timespec abstime = { 0, 0 };
224 ASSERT_EQ(thread_has_lock, 0);
225 fprintf(stderr, "Trying to lock the unlocked rwlock with a valid "
226 "zero absolute timestamp. "
227 "Expected to succeed instantly since the lock is free.\n");
228 rc = pthread_rwlock_timedrdlock(&rwlock, &abstime);
229 ASSERT_EQ(rc, 0);
230 rc = pthread_rwlock_unlock(&rwlock);
231 ASSERT_EQ(rc, 0);
232 }
233
234 int main(int argc, char **argv) {
235 int rc;
236 fprintf(stderr, "Running...\n");
237
238 pthread_rwlockattr_t attrs;
239 rc = pthread_rwlockattr_init(&attrs);
240 ASSERT_EQ(rc, 0);
241 int shared = -1;
242 rc = pthread_rwlockattr_getpshared(&attrs, &shared);
243 ASSERT_EQ(rc, 0);
244 ASSERT_EQ(shared, PTHREAD_PROCESS_PRIVATE);
245 rc = pthread_rwlockattr_setpshared(&attrs, PTHREAD_PROCESS_SHARED);
246 ASSERT_EQ(rc, 0);
247 rc = pthread_rwlockattr_setpshared(&attrs, PTHREAD_PROCESS_PRIVATE);
248 ASSERT_EQ(rc, 0);
249 rc = pthread_rwlock_init(&rwlock, &attrs);
250 ASSERT_EQ(rc, 0);
251 rc = pthread_rwlockattr_destroy(&attrs);
252 ASSERT_EQ(rc, 0);
253
254 pthread_t thread;
255 rc = pthread_create(&thread, NULL, locking_thread, NULL);
256 ASSERT_EQ(rc, 0);
257 fprintf(stderr, "Thread started.\n");
258
259 test_unlocked_with_zero_timestamp();
260 test_multiple_readers();
261 test_multiple_writers();
262 test_reader_plus_writer();
263 test_writer_plus_reader();
264 test_reader_timedwait();
265 test_writer_timedwait();
266 test_recursive_reader();
267
268 rc = pthread_rwlock_destroy(&rwlock);
269 ASSERT_EQ(rc, 0);
270 fprintf(stderr, "Done.\n");
271 return 0;
272 }
OLDNEW
« src/untrusted/pthread/pthread_types.h ('K') | « tests/threads/nacl.scons ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698