source: rtems/testsuites/psxtests/psxrwlock01/test.c @ 89fc9345

5
Last change on this file since 89fc9345 was 89fc9345, checked in by Sebastian Huber <sebastian.huber@…>, on 09/21/17 at 13:42:45

posix: Implement self-contained POSIX rwlocks

POSIX rwlocks are now available in all configurations and no longer
depend on --enable-posix.

Update #2514.
Update #3115.

  • Property mode set to 100644
File size: 20.5 KB
Line 
1/**
2 *  @file
3 *
4 *  This test exercises the POSIX RWLock manager.
5 */
6
7/*
8 *  COPYRIGHT (c) 1989-2012.
9 *  On-Line Applications Research Corporation (OAR).
10 *
11 *  The license and distribution terms for this file may be
12 *  found in the file LICENSE in this distribution or at
13 *  http://www.rtems.org/license/LICENSE.
14 */
15
16#ifdef HAVE_CONFIG_H
17#include "config.h"
18#endif
19
20#include "tmacros.h"
21#include <stdio.h>
22#include <errno.h>
23#include <stdlib.h>
24
25/* #define __USE_XOPEN2K XXX already defined on GNU/Linux */
26#include <pthread.h>
27
28const char rtems_test_name[] = "PSXRWLOCK 1";
29
30/* forward declarations to avoid warnings */
31void *ReadLockThread(void *arg);
32void *WriteLockThread(void *arg);
33int test_main(void);
34
35#if !HAVE_DECL_PTHREAD_RWLOCK_UNLOCK
36/* FIXME: Newlib should provide the decl. */
37extern int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
38#endif
39
40#define NUMBER_THREADS 2
41pthread_t ThreadIds[NUMBER_THREADS];
42pthread_rwlock_t RWLock;
43
44/*
45 * Test thread to block for read lock and unlock it
46 */
47void *ReadLockThread(void *arg)
48{
49  int status;
50
51  /*
52   * Detach ourselves so we don't wait for a join that won't happen.
53   */
54  pthread_detach( pthread_self() );
55
56  puts( "ReadThread - pthread_rwlock_rdlock(RWLock) blocking -- OK" );
57  status = pthread_rwlock_rdlock(&RWLock);
58  rtems_test_assert( !status );
59  puts( "ReadThread - pthread_rwlock_rdlock(RWLock) unblocked -- OK" );
60
61  status = pthread_rwlock_unlock(&RWLock);
62  rtems_test_assert( !status );
63  return NULL;
64}
65
66/*
67 * Test thread to block for write lock and unlock it
68 */
69void *WriteLockThread(void *arg)
70{
71  int status;
72
73  /*
74   * Detach ourselves so we don't wait for a join that won't happen.
75   */
76  pthread_detach( pthread_self() );
77
78  puts( "WriteThread - pthread_rwlock_wrlock(RWLock) blocking -- OK" );
79  status = pthread_rwlock_wrlock(&RWLock);
80  rtems_test_assert( !status );
81  puts( "WriteThread - pthread_rwlock_wrlock(RWLock) unblocked -- OK" );
82
83  sleep( 2 );
84
85  puts( "WriteThread - pthread_rwlock_unlock(RWLock) -- OK" );
86  status = pthread_rwlock_unlock(&RWLock);
87  if ( status )
88   printf( "status=%s\n", strerror(status) );
89  rtems_test_assert( !status );
90  return NULL;
91}
92
93static void test_rwlock_pshared_init(void)
94{
95  pthread_rwlock_t rwlock;
96  pthread_rwlockattr_t attr;
97  int eno;
98
99  eno = pthread_rwlockattr_init(&attr);
100  rtems_test_assert(eno == 0);
101
102  eno = pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
103  rtems_test_assert(eno == 0);
104
105  eno = pthread_rwlock_init(&rwlock, &attr);
106  rtems_test_assert(eno == 0);
107
108  eno = pthread_rwlock_destroy(&rwlock);
109  rtems_test_assert(eno == 0);
110
111  eno = pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
112  rtems_test_assert(eno == 0);
113
114  eno = pthread_rwlock_init(&rwlock, &attr);
115  rtems_test_assert(eno == 0);
116
117  eno = pthread_rwlock_destroy(&rwlock);
118  rtems_test_assert(eno == 0);
119
120  attr.process_shared = -1;
121
122  eno = pthread_rwlock_init(&rwlock, &attr);
123  rtems_test_assert(eno == EINVAL);
124
125  eno = pthread_rwlockattr_destroy(&attr);
126  rtems_test_assert(eno == 0);
127}
128
129static void test_rwlock_null( void )
130{
131  struct timespec to;
132  int eno;
133
134  eno = pthread_rwlock_destroy( NULL );
135  rtems_test_assert( eno == EINVAL );
136
137  eno = pthread_rwlock_init( NULL, NULL );
138  rtems_test_assert( eno == EINVAL );
139
140  eno = pthread_rwlock_rdlock( NULL );
141  rtems_test_assert( eno == EINVAL );
142
143  to.tv_sec = 1;
144  to.tv_nsec = 1;
145  eno = pthread_rwlock_timedrdlock( NULL, &to );
146  rtems_test_assert( eno == EINVAL );
147
148  to.tv_sec = 1;
149  to.tv_nsec = 1;
150  eno = pthread_rwlock_timedwrlock( NULL, &to );
151  rtems_test_assert( eno == EINVAL );
152
153  eno = pthread_rwlock_tryrdlock( NULL );
154  rtems_test_assert( eno == EINVAL );
155
156  eno = pthread_rwlock_trywrlock( NULL );
157  rtems_test_assert( eno == EINVAL );
158
159  eno = pthread_rwlock_unlock( NULL );
160  rtems_test_assert( eno == EINVAL );
161
162  eno = pthread_rwlock_wrlock( NULL );
163  rtems_test_assert( eno == EINVAL );
164}
165
166static void test_rwlock_not_initialized( void )
167{
168  pthread_rwlock_t rw;
169  struct timespec to;
170  int eno;
171
172  memset( &rw, 0xff, sizeof( rw ) );
173
174  eno = pthread_rwlock_destroy( &rw );
175  rtems_test_assert( eno == EINVAL );
176
177  eno = pthread_rwlock_rdlock( &rw );
178  rtems_test_assert( eno == EINVAL );
179
180  to.tv_sec = 1;
181  to.tv_nsec = 1;
182  eno = pthread_rwlock_timedrdlock( &rw, &to );
183  rtems_test_assert( eno == EINVAL );
184
185  to.tv_sec = 1;
186  to.tv_nsec = 1;
187  eno = pthread_rwlock_timedwrlock( &rw, &to );
188  rtems_test_assert( eno == EINVAL );
189
190  eno = pthread_rwlock_tryrdlock( &rw );
191  rtems_test_assert( eno == EINVAL );
192
193  eno = pthread_rwlock_trywrlock( &rw );
194  rtems_test_assert( eno == EINVAL );
195
196  eno = pthread_rwlock_unlock( &rw );
197  rtems_test_assert( eno == EINVAL );
198
199  eno = pthread_rwlock_wrlock( &rw );
200  rtems_test_assert( eno == EINVAL );
201}
202
203static void test_rwlock_invalid_copy( void )
204{
205  pthread_rwlock_t rw;
206  pthread_rwlock_t rw2;
207  struct timespec to;
208  int eno;
209
210  eno = pthread_rwlock_init( &rw, NULL );
211  rtems_test_assert( eno == 0 );
212
213  memcpy( &rw2, &rw, sizeof( rw2 ) );
214
215  eno = pthread_rwlock_destroy( &rw2 );
216  rtems_test_assert( eno == EINVAL );
217
218  eno = pthread_rwlock_rdlock( &rw2 );
219  rtems_test_assert( eno == EINVAL );
220
221  to.tv_sec = 1;
222  to.tv_nsec = 1;
223  eno = pthread_rwlock_timedrdlock( &rw2, &to );
224  rtems_test_assert( eno == EINVAL );
225
226  to.tv_sec = 1;
227  to.tv_nsec = 1;
228  eno = pthread_rwlock_timedwrlock( &rw2, &to );
229  rtems_test_assert( eno == EINVAL );
230
231  eno = pthread_rwlock_tryrdlock( &rw2 );
232  rtems_test_assert( eno == EINVAL );
233
234  eno = pthread_rwlock_trywrlock( &rw2 );
235  rtems_test_assert( eno == EINVAL );
236
237  eno = pthread_rwlock_unlock( &rw2 );
238  rtems_test_assert( eno == EINVAL );
239
240  eno = pthread_rwlock_wrlock( &rw2 );
241  rtems_test_assert( eno == EINVAL );
242
243  eno = pthread_rwlock_destroy( &rw );
244  rtems_test_assert( eno == 0 );
245}
246
247static void test_rwlock_auto_initialization( void )
248{
249  struct timespec to;
250  int eno;
251
252  {
253    static pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
254
255    eno = pthread_rwlock_destroy( &rw );
256    rtems_test_assert( eno == 0 );
257
258    eno = pthread_rwlock_destroy( &rw );
259    rtems_test_assert( eno == EINVAL );
260  }
261
262  {
263    static pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
264
265    eno = pthread_rwlock_rdlock( &rw );
266    rtems_test_assert( eno == 0 );
267  }
268
269  {
270    static pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
271
272    to.tv_sec = 1;
273    to.tv_nsec = 1;
274    eno = pthread_rwlock_timedrdlock( &rw, &to );
275    rtems_test_assert( eno == 0 );
276  }
277
278  {
279    static pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
280
281    to.tv_sec = 1;
282    to.tv_nsec = 1;
283    eno = pthread_rwlock_timedwrlock( &rw, &to );
284    rtems_test_assert( eno == 0 );
285  }
286
287  {
288    static pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
289
290    eno = pthread_rwlock_tryrdlock( &rw );
291    rtems_test_assert( eno == 0 );
292  }
293
294  {
295    static pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
296
297    eno = pthread_rwlock_trywrlock( &rw );
298    rtems_test_assert( eno == 0 );
299  }
300
301  {
302    static pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
303
304    eno = pthread_rwlock_unlock( &rw );
305    rtems_test_assert( eno == 0 );
306  }
307
308  {
309    static pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
310
311    eno = pthread_rwlock_wrlock( &rw );
312    rtems_test_assert( eno == 0 );
313  }
314}
315
316/*
317 *  main entry point to the test
318 */
319
320#if defined(__rtems__)
321int test_main(void)
322#else
323int main(
324  int    argc,
325  char **argv
326)
327#endif
328{
329  pthread_rwlock_t     rwlock;
330  pthread_rwlockattr_t attr;
331  int                  status;
332  int                  p;
333  int                  i;
334  struct timespec      abstime;
335
336  TEST_BEGIN();
337
338  test_rwlock_pshared_init();
339  test_rwlock_null();
340  test_rwlock_not_initialized();
341  test_rwlock_invalid_copy();
342  test_rwlock_auto_initialization();
343
344  /*************** NULL POINTER CHECKS *****************/
345  puts( "pthread_rwlockattr_init( NULL ) -- EINVAL" );
346  status = pthread_rwlockattr_init( NULL );
347  rtems_test_assert( status == EINVAL );
348
349  puts( "pthread_rwlockattr_setpshared( NULL, private ) -- EINVAL" );
350  status = pthread_rwlockattr_setpshared( NULL, PTHREAD_PROCESS_PRIVATE );
351  rtems_test_assert( status == EINVAL );
352
353  puts( "pthread_rwlockattr_setpshared( NULL, shared ) -- EINVAL" );
354  status = pthread_rwlockattr_setpshared( NULL, PTHREAD_PROCESS_SHARED );
355  rtems_test_assert( status == EINVAL );
356
357  puts( "pthread_rwlockattr_getpshared( NULL, &p ) -- EINVAL" );
358  status = pthread_rwlockattr_getpshared( NULL, &p );
359  rtems_test_assert( status == EINVAL );
360
361  puts( "pthread_rwlockattr_destroy( NULL ) -- EINVAL" );
362  status = pthread_rwlockattr_destroy( NULL );
363  rtems_test_assert( status == EINVAL );
364
365  /*************** NOT INITIALIZED CHECKS *****************/
366  /* cheat visibility */
367  attr.is_initialized = 0;
368  puts( "pthread_rwlockattr_setpshared( &attr, shared ) -- EINVAL" );
369  status = pthread_rwlockattr_setpshared( &attr, PTHREAD_PROCESS_SHARED );
370  rtems_test_assert( status == EINVAL );
371
372  puts( "pthread_rwlockattr_getpshared( &attr, NULL ) -- EINVAL" );
373  status = pthread_rwlockattr_getpshared( &attr, NULL );
374  rtems_test_assert( status == EINVAL );
375
376  puts( "pthread_rwlockattr_destroy( &attr ) -- EINVAL" );
377  status = pthread_rwlockattr_destroy( &attr );
378  rtems_test_assert( status == EINVAL );
379
380  /*************** BAD PSHARED CHECK *****************/
381  puts( "pthread_rwlockattr_setpshared( &attr, private ) -- EINVAL" );
382  status = pthread_rwlockattr_setpshared( &attr, ~PTHREAD_PROCESS_PRIVATE );
383  rtems_test_assert( status == EINVAL );
384
385  /*************** ACTUALLY WORK THIS TIME *****************/
386  puts( "pthread_rwlockattr_init( &attr ) -- OK" );
387  status = pthread_rwlockattr_init( &attr );
388  rtems_test_assert( status == 0 );
389
390  puts( "pthread_rwlockattr_setpshared( &attr, private ) -- OK" );
391  status = pthread_rwlockattr_setpshared( &attr, PTHREAD_PROCESS_PRIVATE );
392  rtems_test_assert( status == 0 );
393
394  puts( "pthread_rwlockattr_getpshared( &attr, &p ) -- OK" );
395  status = pthread_rwlockattr_getpshared( &attr, &p );
396  rtems_test_assert( status == 0 );
397  rtems_test_assert( p == PTHREAD_PROCESS_PRIVATE );
398
399  puts( "pthread_rwlockattr_setpshared( &attr, shared ) -- OK" );
400  status = pthread_rwlockattr_setpshared( &attr, PTHREAD_PROCESS_SHARED );
401  rtems_test_assert( status == 0 );
402
403  puts( "pthread_rwlockattr_getpshared( &attr, &p ) -- OK" );
404  status = pthread_rwlockattr_getpshared( &attr, &p );
405  rtems_test_assert( status == 0 );
406  rtems_test_assert( p == PTHREAD_PROCESS_SHARED );
407
408  /*************** DESTROY/REUSE CHECK *****************/
409  puts( "pthread_rwlockattr_destroy( &attr ) -- OK" );
410  status = pthread_rwlockattr_destroy( &attr );
411  rtems_test_assert( status == 0 );
412
413  puts( "pthread_rwlockattr_getpshared( &attr, &p ) destroyed -- EINVAL" );
414  status = pthread_rwlockattr_getpshared( &attr, &p );
415  rtems_test_assert( status == EINVAL );
416
417  /*************** NULL ARGUMENT CHECKS *****************/
418  abstime.tv_sec = 0;
419  abstime.tv_nsec = 0;
420
421  puts( "pthread_rwlock_init(NULL, &attr) -- EINVAL" );
422  status = pthread_rwlock_init(NULL, &attr);
423  rtems_test_assert( status == EINVAL );
424
425  puts( "pthread_rwlock_destroy(NULL) -- EINVAL" );
426  status = pthread_rwlock_destroy(NULL);
427  rtems_test_assert( status == EINVAL );
428
429  puts( "pthread_rwlock_rdlock(NULL) -- EINVAL" );
430  status = pthread_rwlock_rdlock(NULL);
431  rtems_test_assert( status == EINVAL );
432
433  puts( "pthread_rwlock_timedrdlock( NULL, &abstime) -- EINVAL" );
434  status = pthread_rwlock_timedrdlock( NULL, &abstime);
435  rtems_test_assert( status == EINVAL );
436
437  puts( "pthread_rwlock_timedrdlock( &rwlock, NULL) -- EINVAL" );
438  status = pthread_rwlock_timedrdlock( &rwlock, NULL);
439  rtems_test_assert( status == EINVAL );
440
441  puts( "pthread_rwlock_tryrdlock(NULL) -- EINVAL" );
442  status = pthread_rwlock_tryrdlock(NULL);
443  rtems_test_assert( status == EINVAL );
444
445  puts( "pthread_rwlock_wrlock(NULL) -- EINVAL" );
446  status = pthread_rwlock_wrlock(NULL);
447  rtems_test_assert( status == EINVAL );
448
449  puts( "pthread_rwlock_timedwrlock( NULL, &abstime) -- EINVAL" );
450  status = pthread_rwlock_timedwrlock( NULL, &abstime );
451  rtems_test_assert( status == EINVAL );
452
453  puts( "pthread_rwlock_timedwrlock( &rwlock, NULL) -- EINVAL" );
454  status = pthread_rwlock_timedwrlock( &rwlock, NULL);
455  rtems_test_assert( status == EINVAL );
456
457  puts( "pthread_rwlock_trywrlock(NULL) -- EINVAL" );
458  status = pthread_rwlock_trywrlock(NULL);
459  rtems_test_assert( status == EINVAL );
460
461  puts( "pthread_rwlock_unlock(NULL) -- EINVAL" );
462  status = pthread_rwlock_unlock(NULL);
463  rtems_test_assert( status == EINVAL );
464
465  /*************** BAD ID CHECK *****************/
466  /* make a valid abstime */
467  puts( "clock_gettime(CLOCK_REALTIME, &abstime) -- OK" );
468  status = clock_gettime( CLOCK_REALTIME, &abstime );
469  rtems_test_assert( !status );
470  abstime.tv_sec += 5;
471
472  puts( "pthread_rwlock_destroy(BadId) -- EINVAL" );
473  status = pthread_rwlock_destroy(NULL);
474  rtems_test_assert( status == EINVAL );
475
476  puts( "pthread_rwlock_rdlock(BadId) -- EINVAL" );
477  status = pthread_rwlock_rdlock(NULL);
478  rtems_test_assert( status == EINVAL );
479
480  puts( "pthread_rwlock_timedrdlock(BadId, &abstime) -- EINVAL" );
481  status = pthread_rwlock_timedrdlock( NULL, &abstime);
482  rtems_test_assert( status == EINVAL );
483
484  puts( "pthread_rwlock_tryrdlock(BadId) -- EINVAL" );
485  status = pthread_rwlock_tryrdlock(NULL);
486  rtems_test_assert( status == EINVAL );
487
488  puts( "pthread_rwlock_wrlock(BadId) -- EINVAL" );
489  status = pthread_rwlock_wrlock(NULL);
490  rtems_test_assert( status == EINVAL );
491
492  puts( "pthread_rwlock_timedwrlock(BadId, &abstime) -- EINVAL" );
493  status = pthread_rwlock_timedwrlock( NULL, &abstime );
494  rtems_test_assert( status == EINVAL );
495
496  puts( "pthread_rwlock_trywrlock(BadId) -- EINVAL" );
497  status = pthread_rwlock_trywrlock(NULL);
498  rtems_test_assert( status == EINVAL );
499
500  puts( "pthread_rwlock_unlock(BadId) -- EINVAL" );
501  status = pthread_rwlock_unlock(NULL);
502  rtems_test_assert( status == EINVAL );
503
504  /*************** BAD ABSTIME CHECK *****************/
505
506  /* in the past */
507  abstime.tv_sec = 0;
508  abstime.tv_nsec = 0;
509
510  /* invalid tv_nsec */
511  abstime.tv_sec = 0;
512  abstime.tv_nsec = 0x7fffffffL;
513
514  /* XXX do we need bad time check? */
515
516  /*************** ACTUALLY CREATE ONE CHECK *****************/
517  puts( "pthread_rwlockattr_init( &attr ) -- OK" );
518  status = pthread_rwlockattr_init( &attr );
519  rtems_test_assert( status == 0 );
520
521  puts( "pthread_rwlock_init( &rwlock, &attr ) -- OK" );
522  status = pthread_rwlock_init( &rwlock, &attr );
523  rtems_test_assert( status == 0 );
524
525  puts( "pthread_rwlock_destroy( &rwlock ) -- OK" );
526  status = pthread_rwlock_destroy( &rwlock );
527  rtems_test_assert( status == 0 );
528
529  /********* CREATE RWLOCK WITH DEFAULT ATTRIBUTES AND DESTROY IT *********/
530  puts( "pthread_rwlock_init( &rwlock, NULL ) -- OK" );
531  status = pthread_rwlock_init( &rwlock, NULL );
532  rtems_test_assert( status == 0 );
533
534  puts( "pthread_rwlock_destroy( &rwlock ) -- OK" );
535  status = pthread_rwlock_destroy( &rwlock );
536  rtems_test_assert( status == 0 );
537
538  /*************** CREATE THREADS AND LET THEM OBTAIN READLOCK ***************/
539  puts( "pthread_rwlock_init( &RWLock, &attr ) -- OK" );
540  status = pthread_rwlock_init( &RWLock, &attr );
541  rtems_test_assert( status == 0 );
542
543  puts( "pthread_rwlock_tryrdlock(RWLock) -- OK" );
544  status = pthread_rwlock_tryrdlock(&RWLock);
545  rtems_test_assert( !status );
546
547  for (i=0 ; i<NUMBER_THREADS ; i++ ) {
548    printf( "Init: pthread_create - thread %d OK\n", i+1 );
549    status = pthread_create(&ThreadIds[i], NULL, ReadLockThread, &ThreadIds[i]);
550    rtems_test_assert( !status );
551
552    sleep(1);
553  }
554
555  puts( "pthread_rwlock_unlock(RWLock) -- OK" );
556  status = pthread_rwlock_unlock(&RWLock);
557  rtems_test_assert( !status );
558
559  sleep(1);
560
561  /*************** CREATE THREADS AND LET THEM OBTAIN READLOCK ***************/
562  puts( "pthread_rwlock_trywrlock(RWLock) -- OK" );
563  status = pthread_rwlock_trywrlock(&RWLock);
564  rtems_test_assert( !status );
565
566  puts( "pthread_rwlock_tryrdlock(&RWLock) -- EBUSY" );
567  status = pthread_rwlock_tryrdlock(&RWLock);
568  rtems_test_assert( status == EBUSY );
569
570  for (i=0 ; i<NUMBER_THREADS ; i++ ) {
571    printf( "Init: pthread_create - thread %d OK\n", i+1 );
572    status = pthread_create(&ThreadIds[i], NULL, ReadLockThread, &ThreadIds[i]);
573    rtems_test_assert( !status );
574
575    sleep(1);
576  }
577
578  /* Attempt delete while threads are blocked */
579  puts( "pthread_rwlock_destroy( &RWLock ) -- EBUSY" );
580  status = pthread_rwlock_destroy( &RWLock );
581  rtems_test_assert( status == EBUSY );
582
583  /* now unlock it so the threads can continue */
584  puts( "pthread_rwlock_unlock(RWLock) -- OK" );
585  status = pthread_rwlock_unlock(&RWLock);
586  rtems_test_assert( !status );
587
588  sleep(2);
589
590  /*************** CREATE THREADS AND LET THEM OBTAIN WRITE LOCK *************/
591  puts( "\npthread_rwlock_trywrlock(RWLock) -- OK" );
592  status = pthread_rwlock_trywrlock(&RWLock);
593  rtems_test_assert( !status );
594
595  puts( "pthread_rwlock_trywrlock(&RWLock) -- EBUSY" );
596  status = pthread_rwlock_trywrlock(&RWLock);
597  rtems_test_assert( status == EBUSY );
598
599  for (i=0 ; i<NUMBER_THREADS ; i++ ) {
600    printf( "Init: pthread_create - thread %d OK\n", i+1 );
601    status =
602      pthread_create(&ThreadIds[i], NULL, WriteLockThread, &ThreadIds[i]);
603    rtems_test_assert( !status );
604
605    sleep(2);
606  }
607
608  puts( "pthread_rwlock_unlock(RWLock) -- OK" );
609  status = pthread_rwlock_unlock(&RWLock);
610  rtems_test_assert( !status );
611
612  sleep(6);
613
614  /*************** CREATE THREADS AND LET THEM OBTAIN WRITE LOCK *************/
615  /***************    THEN ATTEMPT TO OBTAIN A READLOCK          *************/
616 
617  puts( "\npthread_rwlock_tryrdlock(&RWLock) -- OK" );
618  status = pthread_rwlock_tryrdlock(&RWLock);
619  rtems_test_assert( !status );
620
621  printf( "Init: pthread_create - thread reader & writer OK\n" );
622  status = pthread_create(&ThreadIds[0], NULL, WriteLockThread, &ThreadIds[0]);
623  rtems_test_assert( !status );
624
625  sleep(1);
626  status = pthread_create(&ThreadIds[1], NULL, ReadLockThread, &ThreadIds[1]);
627  rtems_test_assert( !status );
628
629  sleep(1);
630
631  puts( "pthread_rwlock_tryrdlock(&RWLock) -- EBUSY" );
632  status = pthread_rwlock_tryrdlock(&RWLock);
633  rtems_test_assert( status == EBUSY );
634
635  puts( "pthread_rwlock_trywrlock(&RWLock) -- EBUSY" );
636  status = pthread_rwlock_trywrlock(&RWLock);
637  rtems_test_assert( status == EBUSY );
638
639  sleep( 5 );
640
641  puts( "pthread_rwlock_unlock(&RWLock) -- OK" );
642  status = pthread_rwlock_unlock(&RWLock);
643  rtems_test_assert( !status );
644
645  sleep( 5 );
646
647  /*************** TIMEOUT ON RWLOCK ***************/
648  puts( "clock_gettime(CLOCK_REALTIME, &abstime) -- OK" );
649  status = clock_gettime( CLOCK_REALTIME, &abstime );
650  rtems_test_assert( !status );
651
652  abstime.tv_sec += 1;
653  puts( "pthread_rwlock_timedwrlock( &RWLock, &abstime) -- OK" );
654  status = pthread_rwlock_timedwrlock( &RWLock, &abstime );
655  rtems_test_assert( status == 0 );
656
657  abstime.tv_sec += 1;
658  puts( "pthread_rwlock_timedrdlock( &RWLock, &abstime) -- ETIMEDOUT" );
659  status = pthread_rwlock_timedrdlock( &RWLock, &abstime );
660  rtems_test_assert( status == ETIMEDOUT );
661
662  abstime.tv_sec -= 1;
663  puts( "pthread_rwlock_timedrdlock( &RWLock, &abstime) -- ETIMEDOUT" );
664  status = pthread_rwlock_timedrdlock( &RWLock, &abstime );
665  rtems_test_assert( status == ETIMEDOUT );
666
667  abstime.tv_sec -= 1;
668  puts( "pthread_rwlock_timedwrlock( &RWLock, &abstime) -- ETIMEDOUT" );
669  status = pthread_rwlock_timedwrlock( &RWLock, &abstime );
670  rtems_test_assert( status == ETIMEDOUT );
671
672  /*************** OBTAIN RWLOCK WITH ABSTIME IN PAST ***************/
673  status = pthread_rwlock_unlock(&RWLock);
674  rtems_test_assert( !status );
675
676  abstime.tv_sec -= 1;
677  puts( "pthread_rwlock_timedrdlock( &RWLock, &abstime) -- in past -- OK" );
678  status = pthread_rwlock_timedrdlock( &RWLock, &abstime );
679  rtems_test_assert( status == 0 );
680
681  /*************** OBTAIN RWLOCK FOR WRITE WITH ABSTIME IN PAST ***************/
682  status = pthread_rwlock_unlock(&RWLock);
683  rtems_test_assert( !status );
684
685  abstime.tv_sec -= 1;
686  puts( "pthread_rwlock_timedwrlock( &RWLock, &abstime) -- in past -- OK" );
687  status = pthread_rwlock_timedwrlock( &RWLock, &abstime );
688  rtems_test_assert( status == 0 );
689
690  /*************** DESTROY RWLOCK ***************/
691  puts( "pthread_rwlock_destroy( &RWLock ) -- OK" );
692  status = pthread_rwlock_destroy( &RWLock );
693  rtems_test_assert( status == 0 );
694
695  /*************** OBTAIN A LOCK AND THEN RELEASE IT TWICE ***************/
696
697  puts( "pthread_rwlock_init( &rwlock, NULL ) -- OK" );
698  status = pthread_rwlock_init( &rwlock, NULL );
699  rtems_test_assert( status == 0 );
700
701  puts( "pthread_rwlock_unlock ( &rwlock ) -- OK" );
702  status = pthread_rwlock_unlock( &rwlock );
703  rtems_test_assert( status == 0 );
704
705  puts( "pthread_rwlock_unlock ( &rwlock ) -- OK" );
706  status = pthread_rwlock_unlock( &rwlock );
707  rtems_test_assert( status == 0 );
708
709  /*************** END OF TEST *****************/
710  TEST_END();
711  exit(0);
712}
Note: See TracBrowser for help on using the repository browser.