source: rtems/testsuites/psxtests/psxrwlock01/test.c @ 6ec7f21

5
Last change on this file since 6ec7f21 was 6ec7f21, checked in by Sebastian Huber <sebastian.huber@…>, on 07/16/18 at 06:31:05

posix: Fix rwlock auto initialization

Add more test cases.

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