source: rtems/testsuites/psxtests/psxrwlock01/test.c @ cd06fd58

4.104.115
Last change on this file since cd06fd58 was cd06fd58, checked in by Ralf Corsepius <ralf.corsepius@…>, on 10/25/09 at 06:51:02

2009-10-25 Ralf Corsépius <ralf.corsepius@…>

  • configure.ac: Check for pthread_rwlock_unlock in pthread.h.
  • psxrwlock01/test.c: Conditionally add local prototype for pthread_rwlock_unlock() if pthread.h doesn't supply it.
  • Property mode set to 100644
File size: 13.3 KB
Line 
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#if !HAVE_DECL_PTHREAD_RWLOCK_UNLOCK
23/* FIXME: Newlib should provide the decl. */
24extern int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
25#endif
26
27#define NUMBER_THREADS 2
28pthread_t ThreadIds[NUMBER_THREADS];
29pthread_rwlock_t RWLock;
30
31/*
32 * Test thread to block for read lock and unlock it
33 */
34void *ReadLockThread(void *arg)
35{
36  int status;
37
38  puts( "ReadThread - pthread_rwlock_rdlock(RWLock) blocking -- OK" );
39  status = pthread_rwlock_rdlock(&RWLock);
40  assert( !status );
41  puts( "ReadThread - pthread_rwlock_rdlock(RWLock) unblocked -- OK" );
42
43  status = pthread_rwlock_unlock(&RWLock);
44  assert( !status );
45  return NULL;
46}
47
48/*
49 * Test thread to block for write lock and unlock it
50 */
51void *WriteLockThread(void *arg)
52{
53  int status;
54
55  puts( "WriteThread - pthread_rwlock_wrlock(RWLock) blocking -- OK" );
56  status = pthread_rwlock_wrlock(&RWLock);
57  assert( !status );
58  puts( "WriteThread - pthread_rwlock_wrlock(RWLock) unblocked -- OK" );
59
60  sleep( 1 );
61
62  puts( "WriteThread - pthread_rwlock_unlock(RWLock) -- OK" );
63  status = pthread_rwlock_unlock(&RWLock);
64  assert( !status );
65  return NULL;
66}
67
68/*
69 *  main entry point to the test
70 */
71
72#if defined(__rtems__)
73int test_main(void)
74#else
75int main(
76  int    argc,
77  char **argv
78)
79#endif
80{
81  pthread_rwlock_t     rwlock;
82  pthread_rwlockattr_t attr;
83  int                  status;
84  int                  p;
85  int                  i;
86  struct timespec      abstime;
87
88  puts( "\n\n*** POSIX RWLOCK TEST 01 ***" );
89
90  /*************** NULL POINTER CHECKS *****************/
91  puts( "pthread_rwlockattr_init( NULL ) -- EINVAL" );
92  status = pthread_rwlockattr_init( NULL );
93  assert( status == EINVAL );
94
95  puts( "pthread_rwlockattr_setpshared( NULL, private ) -- EINVAL" );
96  status = pthread_rwlockattr_setpshared( NULL, PTHREAD_PROCESS_PRIVATE );
97  assert( status == EINVAL );
98
99  puts( "pthread_rwlockattr_setpshared( NULL, shared ) -- EINVAL" );
100  status = pthread_rwlockattr_setpshared( NULL, PTHREAD_PROCESS_SHARED );
101  assert( status == EINVAL );
102
103  puts( "pthread_rwlockattr_getpshared( NULL, &p ) -- EINVAL" );
104  status = pthread_rwlockattr_getpshared( NULL, &p );
105  assert( status == EINVAL );
106
107  puts( "pthread_rwlockattr_destroy( NULL ) -- EINVAL" );
108  status = pthread_rwlockattr_destroy( NULL );
109  assert( status == EINVAL );
110
111  /*************** NOT INITIALIZED CHECKS *****************/
112  /* cheat visibility */
113  attr.is_initialized = 0;
114  puts( "pthread_rwlockattr_setpshared( &attr, shared ) -- EINVAL" );
115  status = pthread_rwlockattr_setpshared( &attr, PTHREAD_PROCESS_SHARED );
116  assert( status == EINVAL );
117
118  puts( "pthread_rwlockattr_getpshared( &attr, NULL ) -- EINVAL" );
119  status = pthread_rwlockattr_getpshared( &attr, NULL );
120  assert( status == EINVAL );
121
122  puts( "pthread_rwlockattr_destroy( &attr ) -- EINVAL" );
123  status = pthread_rwlockattr_destroy( &attr );
124  assert( status == EINVAL );
125
126  /*************** BAD PSHARED CHECK *****************/
127  puts( "pthread_rwlockattr_setpshared( &attr, private ) -- EINVAL" );
128  status = pthread_rwlockattr_setpshared( &attr, ~PTHREAD_PROCESS_PRIVATE );
129  assert( status == EINVAL );
130
131  /*************** ACTUALLY WORK THIS TIME *****************/
132  puts( "pthread_rwlockattr_init( &attr ) -- OK" );
133  status = pthread_rwlockattr_init( &attr );
134  assert( status == 0 );
135
136  puts( "pthread_rwlockattr_setpshared( &attr, private ) -- OK" );
137  status = pthread_rwlockattr_setpshared( &attr, PTHREAD_PROCESS_PRIVATE );
138  assert( status == 0 );
139
140  puts( "pthread_rwlockattr_getpshared( &attr, &p ) -- OK" );
141  status = pthread_rwlockattr_getpshared( &attr, &p );
142  assert( status == 0 );
143  assert( p == PTHREAD_PROCESS_PRIVATE );
144
145  puts( "pthread_rwlockattr_setpshared( &attr, shared ) -- OK" );
146  status = pthread_rwlockattr_setpshared( &attr, PTHREAD_PROCESS_SHARED );
147  assert( status == 0 );
148
149  puts( "pthread_rwlockattr_getpshared( &attr, &p ) -- OK" );
150  status = pthread_rwlockattr_getpshared( &attr, &p );
151  assert( status == 0 );
152  assert( p == PTHREAD_PROCESS_SHARED );
153
154  /*************** DESTROY/REUSE CHECK *****************/
155  puts( "pthread_rwlockattr_destroy( &attr ) -- OK" );
156  status = pthread_rwlockattr_destroy( &attr );
157  assert( status == 0 );
158
159  puts( "pthread_rwlockattr_getpshared( &attr, &p ) destroyed -- EINVAL" );
160  status = pthread_rwlockattr_getpshared( &attr, &p );
161  assert( status == EINVAL );
162
163  /*************** NULL ARGUMENT CHECKS *****************/
164  abstime.tv_sec = 0;
165  abstime.tv_nsec = 0;
166
167  puts( "pthread_rwlock_init(NULL, &attr) -- EINVAL" );
168  status = pthread_rwlock_init(NULL, &attr);
169  assert( status == EINVAL );
170
171  puts( "pthread_rwlock_destroy(NULL) -- EINVAL" );
172  status = pthread_rwlock_destroy(NULL);
173  assert( status == EINVAL );
174
175  puts( "pthread_rwlock_rdlock(NULL) -- EINVAL" );
176  status = pthread_rwlock_rdlock(NULL);
177  assert( status == EINVAL );
178
179  puts( "pthread_rwlock_timedrdlock( NULL, &abstime) -- EINVAL" );
180  status = pthread_rwlock_timedrdlock( NULL, &abstime);
181  assert( status == EINVAL );
182
183  puts( "pthread_rwlock_timedrdlock( &rwlock, NULL) -- EINVAL" );
184  status = pthread_rwlock_timedrdlock( &rwlock, NULL);
185  assert( status == EINVAL );
186
187  puts( "pthread_rwlock_tryrdlock(NULL) -- EINVAL" );
188  status = pthread_rwlock_tryrdlock(NULL);
189  assert( status == EINVAL );
190
191  puts( "pthread_rwlock_wrlock(NULL) -- EINVAL" );
192  status = pthread_rwlock_wrlock(NULL);
193  assert( status == EINVAL );
194
195  puts( "pthread_rwlock_timedwrlock( NULL, &abstime) -- EINVAL" );
196  status = pthread_rwlock_timedwrlock( NULL, &abstime );
197  assert( status == EINVAL );
198
199  puts( "pthread_rwlock_timedwrlock( &rwlock, NULL) -- EINVAL" );
200  status = pthread_rwlock_timedwrlock( &rwlock, NULL);
201  assert( status == EINVAL );
202
203  puts( "pthread_rwlock_trywrlock(NULL) -- EINVAL" );
204  status = pthread_rwlock_trywrlock(NULL);
205  assert( status == EINVAL );
206
207  puts( "pthread_rwlock_unlock(NULL) -- EINVAL" );
208  status = pthread_rwlock_unlock(NULL);
209  assert( status == EINVAL );
210
211  /*************** BAD ID CHECK *****************/
212  rwlock = 1;
213  /* make a valid abstime */
214  puts( "clock_gettime(CLOCK_REALTIME, &abstime) -- OK" );
215  status = clock_gettime( CLOCK_REALTIME, &abstime );
216  assert( !status );
217  abstime.tv_sec += 5;
218
219  puts( "pthread_rwlock_destroy(BadId) -- EINVAL" );
220  status = pthread_rwlock_destroy(&rwlock);
221  assert( status == EINVAL );
222
223  puts( "pthread_rwlock_rdlock(BadId) -- EINVAL" );
224  status = pthread_rwlock_rdlock(&rwlock);
225  assert( status == EINVAL );
226
227  puts( "pthread_rwlock_timedrdlock(BadId, &abstime) -- EINVAL" );
228  status = pthread_rwlock_timedrdlock( &rwlock, &abstime);
229  assert( status == EINVAL );
230
231  puts( "pthread_rwlock_tryrdlock(BadId) -- EINVAL" );
232  status = pthread_rwlock_tryrdlock(&rwlock);
233  assert( status == EINVAL );
234
235  puts( "pthread_rwlock_wrlock(BadId) -- EINVAL" );
236  status = pthread_rwlock_wrlock(&rwlock);
237  assert( status == EINVAL );
238
239  puts( "pthread_rwlock_timedwrlock(BadId, &abstime) -- EINVAL" );
240  status = pthread_rwlock_timedwrlock( &rwlock, &abstime );
241  assert( status == EINVAL );
242
243  puts( "pthread_rwlock_trywrlock(BadId) -- EINVAL" );
244  status = pthread_rwlock_trywrlock(&rwlock);
245  assert( status == EINVAL );
246
247  puts( "pthread_rwlock_unlock(BadId) -- EINVAL" );
248  status = pthread_rwlock_unlock(&rwlock);
249  assert( status == EINVAL );
250
251  /*************** BAD ABSTIME CHECK *****************/
252
253  /* in the past */
254  abstime.tv_sec = 0;
255  abstime.tv_nsec = 0;
256
257  /* invalid tv_nsec */
258  abstime.tv_sec = 0;
259  abstime.tv_nsec = 0x7fffffffL;
260
261  /* XXX do we need bad time check? */
262
263  /*************** ACTUALLY CREATE ONE CHECK *****************/
264  puts( "pthread_rwlockattr_init( &attr ) -- OK" );
265  status = pthread_rwlockattr_init( &attr );
266  assert( status == 0 );
267
268  puts( "pthread_rwlock_init( &rwlock, &attr ) -- OK" );
269  status = pthread_rwlock_init( &rwlock, &attr );
270  assert( status == 0 );
271  assert( rwlock != 0 );
272
273  puts( "pthread_rwlock_init( &rwlock, &attr ) -- EAGAIN" );
274  status = pthread_rwlock_init( &rwlock, &attr );
275  assert( status == EAGAIN );
276
277  puts( "pthread_rwlock_destroy( &rwlock ) -- OK" );
278  status = pthread_rwlock_destroy( &rwlock );
279  assert( status == 0 );
280
281  /********* CREATE RWLOCK WITH DEFAULT ATTRIBUTES AND DESTROY IT *********/
282  puts( "pthread_rwlock_init( &rwlock, NULL ) -- OK" );
283  status = pthread_rwlock_init( &rwlock, NULL );
284  assert( status == 0 );
285
286  puts( "pthread_rwlock_destroy( &rwlock ) -- OK" );
287  status = pthread_rwlock_destroy( &rwlock );
288  assert( status == 0 );
289
290  /*************** CREATE THREADS AND LET THEM OBTAIN READLOCK ***************/
291  puts( "pthread_rwlock_init( &RWLock, &attr ) -- OK" );
292  status = pthread_rwlock_init( &RWLock, &attr );
293  assert( status == 0 );
294
295  puts( "pthread_rwlock_tryrdlock(RWLock) -- OK" );
296  status = pthread_rwlock_tryrdlock(&RWLock);
297  assert( !status );
298
299  for (i=0 ; i<NUMBER_THREADS ; i++ ) {
300    printf( "Init: pthread_create - thread %d OK\n", i+1 );
301    status = pthread_create(&ThreadIds[i], NULL, ReadLockThread, &ThreadIds[i]);
302    assert( !status );
303
304    sleep(1);
305  }
306
307  puts( "pthread_rwlock_unlock(RWLock) -- OK" );
308  status = pthread_rwlock_unlock(&RWLock);
309  assert( !status );
310
311  sleep(1);
312
313  /*************** CREATE THREADS AND LET THEM OBTAIN READLOCK ***************/
314  puts( "pthread_rwlock_trywrlock(RWLock) -- OK" );
315  status = pthread_rwlock_trywrlock(&RWLock);
316  assert( !status );
317
318  puts( "pthread_rwlock_tryrdlock(&RWLock) -- EBUSY" );
319  status = pthread_rwlock_tryrdlock(&RWLock);
320  assert( status == EBUSY );
321
322  for (i=0 ; i<NUMBER_THREADS ; i++ ) {
323    printf( "Init: pthread_create - thread %d OK\n", i+1 );
324    status = pthread_create(&ThreadIds[i], NULL, ReadLockThread, &ThreadIds[i]);
325    assert( !status );
326
327    sleep(1);
328  }
329
330  /* Attempt delete while threads are blocked */
331  puts( "pthread_rwlock_destroy( &RWLock ) -- EBUSY" );
332  status = pthread_rwlock_destroy( &RWLock );
333  assert( status == EBUSY );
334
335  /* now unlock it so the threads can continue */
336  puts( "pthread_rwlock_unlock(RWLock) -- OK" );
337  status = pthread_rwlock_unlock(&RWLock);
338  assert( !status );
339
340  sleep(2);
341 
342  /*************** CREATE THREADS AND LET THEM OBTAIN WRITE LOCK *************/
343  puts( "pthread_rwlock_trywrlock(RWLock) -- OK" );
344  status = pthread_rwlock_trywrlock(&RWLock);
345  assert( !status );
346
347  puts( "pthread_rwlock_trywrlock(&RWLock) -- EBUSY" );
348  status = pthread_rwlock_trywrlock(&RWLock);
349  assert( status == EBUSY );
350
351  for (i=0 ; i<NUMBER_THREADS ; i++ ) {
352    printf( "Init: pthread_create - thread %d OK\n", i+1 );
353    status =
354      pthread_create(&ThreadIds[i], NULL, WriteLockThread, &ThreadIds[i]);
355    assert( !status );
356
357    sleep(1);
358  }
359
360  puts( "pthread_rwlock_unlock(RWLock) -- OK" );
361  status = pthread_rwlock_unlock(&RWLock);
362  assert( !status );
363
364  sleep(2);
365
366  /*************** TIMEOUT ON RWLOCK ***************/
367  puts( "clock_gettime(CLOCK_REALTIME, &abstime) -- OK" );
368  status = clock_gettime( CLOCK_REALTIME, &abstime );
369  assert( !status );
370 
371  abstime.tv_sec += 1;
372  puts( "pthread_rwlock_timedwrlock( &RWLock, &abstime) -- OK" );
373  status = pthread_rwlock_timedwrlock( &RWLock, &abstime );
374  assert( status == 0 );
375
376  abstime.tv_sec += 1;
377  puts( "pthread_rwlock_timedrdlock( &RWLock, &abstime) -- ETIMEDOUT" );
378  status = pthread_rwlock_timedrdlock( &RWLock, &abstime );
379  assert( status == ETIMEDOUT );
380
381  abstime.tv_sec -= 1;
382  puts( "pthread_rwlock_timedrdlock( &RWLock, &abstime) -- ETIMEDOUT" );
383  status = pthread_rwlock_timedrdlock( &RWLock, &abstime );
384  assert( status == ETIMEDOUT );
385
386  abstime.tv_sec -= 1;
387  puts( "pthread_rwlock_timedwrlock( &RWLock, &abstime) -- ETIMEDOUT" );
388  status = pthread_rwlock_timedwrlock( &RWLock, &abstime );
389  assert( status == ETIMEDOUT );
390
391  /*************** OBTAIN RWLOCK WITH ABSTIME IN PAST ***************/
392  status = pthread_rwlock_unlock(&RWLock);
393  assert( !status );
394
395  abstime.tv_sec -= 1;
396  puts( "pthread_rwlock_timedrdlock( &RWLock, &abstime) -- in past -- OK" );
397  status = pthread_rwlock_timedrdlock( &RWLock, &abstime );
398  assert( status == 0 );
399
400  /*************** OBTAIN RWLOCK FOR WRITE WITH ABSTIME IN PAST ***************/
401  status = pthread_rwlock_unlock(&RWLock);
402  assert( !status );
403
404  abstime.tv_sec -= 1;
405  puts( "pthread_rwlock_timedwrlock( &RWLock, &abstime) -- in past -- OK" );
406  status = pthread_rwlock_timedwrlock( &RWLock, &abstime );
407  assert( status == 0 );
408
409  /*************** DESTROY RWLOCK ***************/
410  puts( "pthread_rwlock_destroy( &RWLock ) -- OK" );
411  status = pthread_rwlock_destroy( &RWLock );
412  assert( status == 0 );
413
414  /*************** OBTAIN A LOCK AND THEN RELEASE IT TWICE ***************/
415
416  puts( "pthread_rwlock_init( &rwlock, NULL ) -- OK" );
417  status = pthread_rwlock_init( &rwlock, NULL );
418  assert( status == 0 );
419  assert( rwlock != 0 );
420
421  puts( "pthread_rwlock_unlock ( &rwlock ) -- OK" );
422  status = pthread_rwlock_unlock( &rwlock );
423  assert( status == 0 );
424 
425  puts( "pthread_rwlock_unlock ( &rwlock ) -- OK" );
426  status = pthread_rwlock_unlock( &rwlock );
427  assert( status == 0 );
428
429  /*************** END OF TEST *****************/
430  puts( "*** END OF POSIX RWLOCK TEST 01 ***" );
431  exit(0);
432}
Note: See TracBrowser for help on using the repository browser.