1
2/* Needed for older glibcs (2.3 and older, at least) who don't
3   otherwise "know" about pthread_rwlock_anything or about
4   PTHREAD_MUTEX_RECURSIVE (amongst things). */
5#define _GNU_SOURCE 1
6
7#include <stdio.h>
8#include "safe-pthread.h"
9#include <assert.h>
10
11/* Do trivial stuff with a reader-writer lock. */
12
13int main ( void )
14{
15  int r;
16  pthread_rwlock_t rwl;
17
18  r = pthread_rwlock_init( &rwl, NULL );  assert(r == 0);
19
20  r = pthread_rwlock_wrlock( &rwl );      assert(r == 0);
21  r = pthread_rwlock_unlock( &rwl );      assert(r == 0);
22
23  r = pthread_rwlock_rdlock( &rwl );      assert(r == 0);
24  r = pthread_rwlock_rdlock( &rwl );      assert(r == 0);
25  r = pthread_rwlock_unlock( &rwl );      assert(r == 0);
26  r = pthread_rwlock_unlock( &rwl );      assert(r == 0);
27
28  /* this should fail - lock is unowned now */
29  r = pthread_rwlock_unlock( &rwl );
30#if defined(VGO_darwin) || defined(VGO_solaris)
31  assert(r != 0);
32#else
33  assert(r == 0);
34#endif
35
36  r = pthread_rwlock_destroy( &rwl );     assert(r == 0);
37
38  return 0;
39}
40