1 | /* |
---|
2 | * This test exercises the POSIX RWLock manager. |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-2006. |
---|
5 | * On-Line Applications Research Corporation (OAR). |
---|
6 | * |
---|
7 | * The license and distribution terms for this file may be |
---|
8 | * found in the file LICENSE in this distribution or at |
---|
9 | * http://www.rtems.com/license/LICENSE. |
---|
10 | * |
---|
11 | * $Id$ |
---|
12 | */ |
---|
13 | |
---|
14 | #include <assert.h> |
---|
15 | #include <stdio.h> |
---|
16 | #include <errno.h> |
---|
17 | #include <stdlib.h> |
---|
18 | |
---|
19 | /* #define __USE_XOPEN2K XXX already defined on GNU/Linux */ |
---|
20 | #include <pthread.h> |
---|
21 | |
---|
22 | #define NUMBER_THREADS 2 |
---|
23 | pthread_t ThreadIds[NUMBER_THREADS]; |
---|
24 | pthread_rwlock_t RWLock; |
---|
25 | |
---|
26 | /* |
---|
27 | * Test thread to block for read lock and unlock it |
---|
28 | */ |
---|
29 | void *ReadLockThread(void *arg) |
---|
30 | { |
---|
31 | int status; |
---|
32 | |
---|
33 | puts( "ReadThread - pthread_rwlock_rdlock(RWLock) blocking -- OK" ); |
---|
34 | status = pthread_rwlock_rdlock(&RWLock); |
---|
35 | assert( !status ); |
---|
36 | puts( "ReadThread - pthread_rwlock_rdlock(RWLock) unblocked -- OK" ); |
---|
37 | |
---|
38 | status = pthread_rwlock_unlock(&RWLock); |
---|
39 | assert( !status ); |
---|
40 | return NULL; |
---|
41 | } |
---|
42 | |
---|
43 | /* |
---|
44 | * Test thread to block for write lock and unlock it |
---|
45 | */ |
---|
46 | void *WriteLockThread(void *arg) |
---|
47 | { |
---|
48 | int status; |
---|
49 | |
---|
50 | puts( "WriteThread - pthread_rwlock_wrlock(RWLock) blocking -- OK" ); |
---|
51 | status = pthread_rwlock_wrlock(&RWLock); |
---|
52 | assert( !status ); |
---|
53 | puts( "WriteThread - pthread_rwlock_wrlock(RWLock) unblocked -- OK" ); |
---|
54 | |
---|
55 | sleep( 1 ); |
---|
56 | |
---|
57 | puts( "WriteThread - pthread_rwlock_unlock(RWLock) -- OK" ); |
---|
58 | status = pthread_rwlock_unlock(&RWLock); |
---|
59 | assert( !status ); |
---|
60 | return NULL; |
---|
61 | } |
---|
62 | |
---|
63 | /* |
---|
64 | * main entry point to the test |
---|
65 | */ |
---|
66 | |
---|
67 | #if defined(__rtems__) |
---|
68 | int test_main(void) |
---|
69 | #else |
---|
70 | int main( |
---|
71 | int argc, |
---|
72 | char **argv |
---|
73 | ) |
---|
74 | #endif |
---|
75 | { |
---|
76 | pthread_rwlock_t rwlock; |
---|
77 | pthread_rwlockattr_t attr; |
---|
78 | int status; |
---|
79 | int p; |
---|
80 | pthread_t thread_id; |
---|
81 | int i; |
---|
82 | struct timespec abstime; |
---|
83 | |
---|
84 | puts( "\n\n*** POSIX RWLOCK TEST 01 ***" ); |
---|
85 | |
---|
86 | /*************** NULL POINTER CHECKS *****************/ |
---|
87 | puts( "pthread_rwlockattr_init( NULL ) -- EINVAL" ); |
---|
88 | status = pthread_rwlockattr_init( NULL ); |
---|
89 | assert( status == EINVAL ); |
---|
90 | |
---|
91 | puts( "pthread_rwlockattr_setpshared( NULL, private ) -- EINVAL" ); |
---|
92 | status = pthread_rwlockattr_setpshared( NULL, PTHREAD_PROCESS_PRIVATE ); |
---|
93 | assert( status == EINVAL ); |
---|
94 | |
---|
95 | puts( "pthread_rwlockattr_setpshared( NULL, shared ) -- EINVAL" ); |
---|
96 | status = pthread_rwlockattr_setpshared( NULL, PTHREAD_PROCESS_SHARED ); |
---|
97 | assert( status == EINVAL ); |
---|
98 | |
---|
99 | puts( "pthread_rwlockattr_getpshared( NULL, &p ) -- EINVAL" ); |
---|
100 | status = pthread_rwlockattr_getpshared( NULL, &p ); |
---|
101 | assert( status == EINVAL ); |
---|
102 | |
---|
103 | puts( "pthread_rwlockattr_destroy( NULL ) -- EINVAL" ); |
---|
104 | status = pthread_rwlockattr_destroy( NULL ); |
---|
105 | assert( status == EINVAL ); |
---|
106 | |
---|
107 | /*************** NOT INITIALIZED CHECKS *****************/ |
---|
108 | /* cheat visibility */ |
---|
109 | attr.is_initialized = 0; |
---|
110 | puts( "pthread_rwlockattr_setpshared( &attr, shared ) -- EINVAL" ); |
---|
111 | status = pthread_rwlockattr_setpshared( &attr, PTHREAD_PROCESS_SHARED ); |
---|
112 | assert( status == EINVAL ); |
---|
113 | |
---|
114 | puts( "pthread_rwlockattr_getpshared( &attr, NULL ) -- EINVAL" ); |
---|
115 | status = pthread_rwlockattr_getpshared( &attr, NULL ); |
---|
116 | assert( status == EINVAL ); |
---|
117 | |
---|
118 | puts( "pthread_rwlockattr_destroy( &attr ) -- EINVAL" ); |
---|
119 | status = pthread_rwlockattr_destroy( &attr ); |
---|
120 | assert( status == EINVAL ); |
---|
121 | |
---|
122 | /*************** BAD PSHARED CHECK *****************/ |
---|
123 | puts( "pthread_rwlockattr_setpshared( &attr, private ) -- EINVAL" ); |
---|
124 | status = pthread_rwlockattr_setpshared( &attr, ~PTHREAD_PROCESS_PRIVATE ); |
---|
125 | assert( status == EINVAL ); |
---|
126 | |
---|
127 | /*************** ACTUALLY WORK THIS TIME *****************/ |
---|
128 | puts( "pthread_rwlockattr_init( &attr ) -- OK" ); |
---|
129 | status = pthread_rwlockattr_init( &attr ); |
---|
130 | assert( status == 0 ); |
---|
131 | |
---|
132 | puts( "pthread_rwlockattr_setpshared( &attr, private ) -- OK" ); |
---|
133 | status = pthread_rwlockattr_setpshared( &attr, PTHREAD_PROCESS_PRIVATE ); |
---|
134 | assert( status == 0 ); |
---|
135 | |
---|
136 | puts( "pthread_rwlockattr_getpshared( &attr, &p ) -- OK" ); |
---|
137 | status = pthread_rwlockattr_getpshared( &attr, &p ); |
---|
138 | assert( status == 0 ); |
---|
139 | assert( p == PTHREAD_PROCESS_PRIVATE ); |
---|
140 | |
---|
141 | puts( "pthread_rwlockattr_setpshared( &attr, shared ) -- OK" ); |
---|
142 | status = pthread_rwlockattr_setpshared( &attr, PTHREAD_PROCESS_SHARED ); |
---|
143 | assert( status == 0 ); |
---|
144 | |
---|
145 | puts( "pthread_rwlockattr_getpshared( &attr, &p ) -- OK" ); |
---|
146 | status = pthread_rwlockattr_getpshared( &attr, &p ); |
---|
147 | assert( status == 0 ); |
---|
148 | assert( p == PTHREAD_PROCESS_SHARED ); |
---|
149 | |
---|
150 | /*************** DESTROY/REUSE CHECK *****************/ |
---|
151 | puts( "pthread_rwlockattr_destroy( &attr ) -- OK" ); |
---|
152 | status = pthread_rwlockattr_destroy( &attr ); |
---|
153 | assert( status == 0 ); |
---|
154 | |
---|
155 | puts( "pthread_rwlockattr_getpshared( &attr, &p ) destroyed -- EINVAL" ); |
---|
156 | status = pthread_rwlockattr_getpshared( &attr, &p ); |
---|
157 | assert( status == EINVAL ); |
---|
158 | |
---|
159 | /*************** NULL ARGUMENT CHECKS *****************/ |
---|
160 | abstime.tv_sec = 0; |
---|
161 | abstime.tv_nsec = 0; |
---|
162 | |
---|
163 | puts( "pthread_rwlock_init(NULL, &attr) -- EINVAL" ); |
---|
164 | status = pthread_rwlock_init(NULL, &attr); |
---|
165 | assert( status == EINVAL ); |
---|
166 | |
---|
167 | puts( "pthread_rwlock_destroy(NULL) -- EINVAL" ); |
---|
168 | status = pthread_rwlock_destroy(NULL); |
---|
169 | assert( status == EINVAL ); |
---|
170 | |
---|
171 | puts( "pthread_rwlock_rdlock(NULL) -- EINVAL" ); |
---|
172 | status = pthread_rwlock_rdlock(NULL); |
---|
173 | assert( status == EINVAL ); |
---|
174 | |
---|
175 | puts( "pthread_rwlock_timedrdlock( NULL, &abstime) -- EINVAL" ); |
---|
176 | status = pthread_rwlock_timedrdlock( NULL, &abstime); |
---|
177 | assert( status == EINVAL ); |
---|
178 | |
---|
179 | puts( "pthread_rwlock_timedrdlock( &rwlock, NULL) -- EINVAL" ); |
---|
180 | status = pthread_rwlock_timedrdlock( &rwlock, NULL); |
---|
181 | assert( status == EINVAL ); |
---|
182 | |
---|
183 | puts( "pthread_rwlock_tryrdlock(NULL) -- EINVAL" ); |
---|
184 | status = pthread_rwlock_tryrdlock(NULL); |
---|
185 | assert( status == EINVAL ); |
---|
186 | |
---|
187 | puts( "pthread_rwlock_wrlock(NULL) -- EINVAL" ); |
---|
188 | status = pthread_rwlock_wrlock(NULL); |
---|
189 | assert( status == EINVAL ); |
---|
190 | |
---|
191 | puts( "pthread_rwlock_timedwrlock( NULL, &abstime) -- EINVAL" ); |
---|
192 | status = pthread_rwlock_timedwrlock( NULL, &abstime ); |
---|
193 | assert( status == EINVAL ); |
---|
194 | |
---|
195 | puts( "pthread_rwlock_timedwrlock( &rwlock, NULL) -- EINVAL" ); |
---|
196 | status = pthread_rwlock_timedwrlock( &rwlock, NULL); |
---|
197 | assert( status == EINVAL ); |
---|
198 | |
---|
199 | puts( "pthread_rwlock_trywrlock(NULL) -- EINVAL" ); |
---|
200 | status = pthread_rwlock_trywrlock(NULL); |
---|
201 | assert( status == EINVAL ); |
---|
202 | |
---|
203 | puts( "pthread_rwlock_unlock(NULL) -- EINVAL" ); |
---|
204 | status = pthread_rwlock_unlock(NULL); |
---|
205 | assert( status == EINVAL ); |
---|
206 | |
---|
207 | /*************** BAD ID CHECK *****************/ |
---|
208 | rwlock = 1; |
---|
209 | /* make a valid abstime */ |
---|
210 | puts( "clock_gettime(CLOCK_REALTIME, &abstime) -- OK" ); |
---|
211 | status = clock_gettime( CLOCK_REALTIME, &abstime ); |
---|
212 | assert( !status ); |
---|
213 | abstime.tv_sec += 5; |
---|
214 | |
---|
215 | puts( "pthread_rwlock_destroy(BadId) -- EINVAL" ); |
---|
216 | status = pthread_rwlock_destroy(&rwlock); |
---|
217 | assert( status == EINVAL ); |
---|
218 | |
---|
219 | puts( "pthread_rwlock_rdlock(BadId) -- EINVAL" ); |
---|
220 | status = pthread_rwlock_rdlock(&rwlock); |
---|
221 | assert( status == EINVAL ); |
---|
222 | |
---|
223 | puts( "pthread_rwlock_timedrdlock(BadId, &abstime) -- EINVAL" ); |
---|
224 | status = pthread_rwlock_timedrdlock( &rwlock, &abstime); |
---|
225 | assert( status == EINVAL ); |
---|
226 | |
---|
227 | puts( "pthread_rwlock_tryrdlock(BadId) -- EINVAL" ); |
---|
228 | status = pthread_rwlock_tryrdlock(&rwlock); |
---|
229 | assert( status == EINVAL ); |
---|
230 | |
---|
231 | puts( "pthread_rwlock_wrlock(BadId) -- EINVAL" ); |
---|
232 | status = pthread_rwlock_wrlock(&rwlock); |
---|
233 | assert( status == EINVAL ); |
---|
234 | |
---|
235 | puts( "pthread_rwlock_timedwrlock(BadId, &abstime) -- EINVAL" ); |
---|
236 | status = pthread_rwlock_timedwrlock( &rwlock, &abstime ); |
---|
237 | assert( status == EINVAL ); |
---|
238 | |
---|
239 | puts( "pthread_rwlock_trywrlock(BadId) -- EINVAL" ); |
---|
240 | status = pthread_rwlock_trywrlock(&rwlock); |
---|
241 | assert( status == EINVAL ); |
---|
242 | |
---|
243 | puts( "pthread_rwlock_unlock(BadId) -- EINVAL" ); |
---|
244 | status = pthread_rwlock_unlock(&rwlock); |
---|
245 | assert( status == EINVAL ); |
---|
246 | |
---|
247 | /*************** BAD ABSTIME CHECK *****************/ |
---|
248 | |
---|
249 | /* in the past */ |
---|
250 | abstime.tv_sec = 0; |
---|
251 | abstime.tv_nsec = 0; |
---|
252 | |
---|
253 | /* invalid tv_nsec */ |
---|
254 | abstime.tv_sec = 0; |
---|
255 | abstime.tv_nsec = 0x7fffffffL; |
---|
256 | |
---|
257 | /* XXX do we need bad time check? */ |
---|
258 | |
---|
259 | /*************** ACTUALLY CREATE ONE CHECK *****************/ |
---|
260 | puts( "pthread_rwlockattr_init( &attr ) -- OK" ); |
---|
261 | status = pthread_rwlockattr_init( &attr ); |
---|
262 | assert( status == 0 ); |
---|
263 | |
---|
264 | puts( "pthread_rwlock_init( &rwlock, &attr ) -- OK" ); |
---|
265 | status = pthread_rwlock_init( &rwlock, &attr ); |
---|
266 | assert( status == 0 ); |
---|
267 | assert( rwlock != 0 ); |
---|
268 | |
---|
269 | puts( "pthread_rwlock_init( &rwlock, &attr ) -- EAGAIN" ); |
---|
270 | status = pthread_rwlock_init( &rwlock, &attr ); |
---|
271 | assert( status == EAGAIN ); |
---|
272 | |
---|
273 | puts( "pthread_rwlock_destroy( &rwlock ) -- OK" ); |
---|
274 | status = pthread_rwlock_destroy( &rwlock ); |
---|
275 | assert( status == 0 ); |
---|
276 | |
---|
277 | /********* CREATE RWLOCK WITH DEFAULT ATTRIBUTES AND DESTROY IT *********/ |
---|
278 | puts( "pthread_rwlock_init( &rwlock, NULL ) -- OK" ); |
---|
279 | status = pthread_rwlock_init( &rwlock, NULL ); |
---|
280 | assert( status == 0 ); |
---|
281 | |
---|
282 | puts( "pthread_rwlock_destroy( &rwlock ) -- OK" ); |
---|
283 | status = pthread_rwlock_destroy( &rwlock ); |
---|
284 | assert( status == 0 ); |
---|
285 | |
---|
286 | /*************** CREATE THREADS AND LET THEM OBTAIN READLOCK ***************/ |
---|
287 | puts( "pthread_rwlock_init( &RWLock, &attr ) -- OK" ); |
---|
288 | status = pthread_rwlock_init( &RWLock, &attr ); |
---|
289 | assert( status == 0 ); |
---|
290 | |
---|
291 | puts( "pthread_rwlock_tryrdlock(RWLock) -- OK" ); |
---|
292 | status = pthread_rwlock_tryrdlock(&RWLock); |
---|
293 | assert( !status ); |
---|
294 | |
---|
295 | for (i=0 ; i<NUMBER_THREADS ; i++ ) { |
---|
296 | printf( "Init: pthread_create - thread %d OK\n", i+1 ); |
---|
297 | status = pthread_create(&ThreadIds[i], NULL, ReadLockThread, &ThreadIds[i]); |
---|
298 | assert( !status ); |
---|
299 | |
---|
300 | sleep(1); |
---|
301 | } |
---|
302 | |
---|
303 | puts( "pthread_rwlock_unlock(RWLock) -- OK" ); |
---|
304 | status = pthread_rwlock_unlock(&RWLock); |
---|
305 | assert( !status ); |
---|
306 | |
---|
307 | sleep(1); |
---|
308 | |
---|
309 | /*************** CREATE THREADS AND LET THEM OBTAIN READLOCK ***************/ |
---|
310 | puts( "pthread_rwlock_trywrlock(RWLock) -- OK" ); |
---|
311 | status = pthread_rwlock_trywrlock(&RWLock); |
---|
312 | assert( !status ); |
---|
313 | |
---|
314 | puts( "pthread_rwlock_tryrdlock(&RWLock) -- EBUSY" ); |
---|
315 | status = pthread_rwlock_tryrdlock(&RWLock); |
---|
316 | assert( status == EBUSY ); |
---|
317 | |
---|
318 | for (i=0 ; i<NUMBER_THREADS ; i++ ) { |
---|
319 | printf( "Init: pthread_create - thread %d OK\n", i+1 ); |
---|
320 | status = pthread_create(&ThreadIds[i], NULL, ReadLockThread, &ThreadIds[i]); |
---|
321 | assert( !status ); |
---|
322 | |
---|
323 | sleep(1); |
---|
324 | } |
---|
325 | |
---|
326 | /* Attempt delete while threads are blocked */ |
---|
327 | puts( "pthread_rwlock_destroy( &RWLock ) -- EBUSY" ); |
---|
328 | status = pthread_rwlock_destroy( &RWLock ); |
---|
329 | assert( status == EBUSY ); |
---|
330 | |
---|
331 | puts( "pthread_rwlock_unlock(RWLock) -- OK" ); |
---|
332 | status = pthread_rwlock_unlock(&RWLock); |
---|
333 | assert( !status ); |
---|
334 | |
---|
335 | sleep(2); |
---|
336 | |
---|
337 | /*************** CREATE THREADS AND LET THEM OBTAIN WRITE LOCK *************/ |
---|
338 | puts( "pthread_rwlock_trywrlock(RWLock) -- OK" ); |
---|
339 | status = pthread_rwlock_trywrlock(&RWLock); |
---|
340 | assert( !status ); |
---|
341 | |
---|
342 | puts( "pthread_rwlock_trywrlock(&RWLock) -- EBUSY" ); |
---|
343 | status = pthread_rwlock_trywrlock(&RWLock); |
---|
344 | assert( status == EBUSY ); |
---|
345 | |
---|
346 | for (i=0 ; i<NUMBER_THREADS ; i++ ) { |
---|
347 | printf( "Init: pthread_create - thread %d OK\n", i+1 ); |
---|
348 | status = |
---|
349 | pthread_create(&ThreadIds[i], NULL, WriteLockThread, &ThreadIds[i]); |
---|
350 | assert( !status ); |
---|
351 | |
---|
352 | sleep(1); |
---|
353 | } |
---|
354 | |
---|
355 | puts( "pthread_rwlock_unlock(RWLock) -- OK" ); |
---|
356 | status = pthread_rwlock_unlock(&RWLock); |
---|
357 | assert( !status ); |
---|
358 | |
---|
359 | sleep(2); |
---|
360 | |
---|
361 | /*************** TIMEOUT ON RWLOCK ***************/ |
---|
362 | puts( "clock_gettime(CLOCK_REALTIME, &abstime) -- OK" ); |
---|
363 | status = clock_gettime( CLOCK_REALTIME, &abstime ); |
---|
364 | assert( !status ); |
---|
365 | |
---|
366 | abstime.tv_sec += 1; |
---|
367 | puts( "pthread_rwlock_timedwrlock( &RWLock, &abstime) -- OK" ); |
---|
368 | status = pthread_rwlock_timedwrlock( &RWLock, &abstime ); |
---|
369 | assert( status == 0 ); |
---|
370 | |
---|
371 | abstime.tv_sec += 1; |
---|
372 | puts( "pthread_rwlock_timedrdlock( &RWLock, &abstime) -- OK" ); |
---|
373 | status = pthread_rwlock_timedrdlock( &RWLock, &abstime ); |
---|
374 | assert( status == ETIMEDOUT ); |
---|
375 | |
---|
376 | /*************** DESTROY RWLOCK ***************/ |
---|
377 | puts( "pthread_rwlock_destroy( &RWLock ) -- OK" ); |
---|
378 | status = pthread_rwlock_destroy( &RWLock ); |
---|
379 | assert( status == 0 ); |
---|
380 | |
---|
381 | |
---|
382 | /*************** END OF TEST *****************/ |
---|
383 | puts( "*** END OF POSIX RWLOCK TEST 01 ***" ); |
---|
384 | exit(0); |
---|
385 | } |
---|