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

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

Issue 943343004: Revert "Implement pthread_rwlock functions for NaCl newlib." (Closed) Base URL: https://chromium.googlesource.com/native_client/src/native_client.git@master
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
« 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 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 test_reader_plus_writer(void) {
195 fprintf(stderr, "test_reader_plus_writer\n");
196 tell_thread_to_acquire_lock(READ_LOCK);
197
198 /*
199 * Now attempt to acquire the write lock on the main thread.
200 * This should fail.
201 */
202 int rc = pthread_rwlock_trywrlock(&g_rwlock);
203 ASSERT_EQ(rc, EBUSY);
204
205 tell_thread_to_release_lock();
206 }
207
208 void test_writer_plus_reader(void) {
209 fprintf(stderr, "test_writer_plus_reader\n");
210
211 /*
212 * First get the write lock.
213 */
214 int rc = pthread_rwlock_wrlock(&g_rwlock);
215 ASSERT_EQ(rc, 0);
216
217 /*
218 * Attempt to acquire read lock should now fail
219 */
220 rc = pthread_rwlock_tryrdlock(&g_rwlock);
221 ASSERT_EQ(rc, EBUSY);
222
223 rc = pthread_rwlock_unlock(&g_rwlock);
224 ASSERT_EQ(rc, 0);
225 }
226
227 void test_unlocked_with_zero_timestamp(void) {
228 fprintf(stderr, "test_unlocked_with_zero_timestamp\n");
229 int rc;
230 struct timespec abstime = { 0, 0 };
231 ASSERT_EQ(g_thread_has_lock, 0);
232 fprintf(stderr, "Trying to lock the unlocked rwlock with a valid "
233 "zero absolute timestamp. "
234 "Expected to succeed instantly since the lock is free.\n");
235 rc = pthread_rwlock_timedrdlock(&g_rwlock, &abstime);
236 ASSERT_EQ(rc, 0);
237 rc = pthread_rwlock_unlock(&g_rwlock);
238 ASSERT_EQ(rc, 0);
239 }
240
241 int main(int argc, char **argv) {
242 int rc;
243 fprintf(stderr, "Running...\n");
244
245 pthread_rwlockattr_t attrs;
246 rc = pthread_rwlockattr_init(&attrs);
247 ASSERT_EQ(rc, 0);
248 int shared = -1;
249 rc = pthread_rwlockattr_getpshared(&attrs, &shared);
250 ASSERT_EQ(rc, 0);
251 ASSERT_EQ(shared, PTHREAD_PROCESS_PRIVATE);
252 rc = pthread_rwlockattr_setpshared(&attrs, PTHREAD_PROCESS_SHARED);
253 ASSERT_EQ(rc, 0);
254 rc = pthread_rwlockattr_setpshared(&attrs, PTHREAD_PROCESS_PRIVATE);
255 ASSERT_EQ(rc, 0);
256 rc = pthread_rwlock_init(&g_rwlock, &attrs);
257 ASSERT_EQ(rc, 0);
258 rc = pthread_rwlockattr_destroy(&attrs);
259 ASSERT_EQ(rc, 0);
260
261 pthread_t thread;
262 rc = pthread_create(&thread, NULL, locking_thread, NULL);
263 ASSERT_EQ(rc, 0);
264 fprintf(stderr, "Thread started.\n");
265
266 test_unlocked_with_zero_timestamp();
267 test_multiple_readers();
268 test_multiple_writers();
269 test_reader_plus_writer();
270 test_writer_plus_reader();
271 test_reader_timedwait();
272 test_writer_timedwait();
273 test_recursive_reader();
274
275 rc = pthread_rwlock_destroy(&g_rwlock);
276 ASSERT_EQ(rc, 0);
277 fprintf(stderr, "Done.\n");
278 return 0;
279 }
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