1 /** Test interception of the various pthread_timed*lock() and pthread_try*lock()
2  *  functions. If any of these are not intercepted, an error message will be
3  *  printed at unlock time.
4  */
5 
6 
7 /* Needed for older glibc's (2.3 and older, at least) who don't
8    otherwise "know" about pthread_rwlock_anything or about
9    PTHREAD_MUTEX_RECURSIVE (amongst things). */
10 
11 #define _GNU_SOURCE 1
12 
13 #include "../../config.h"
14 #include <stdio.h>
15 #include <assert.h>
16 #include <pthread.h>
17 
18 
19 int main(int argc, char** argv)
20 {
21   int r;
22   pthread_mutex_t mutex;
23   pthread_rwlock_t rwlock;
24   struct timespec abs_timeout;
25 
26   time(&abs_timeout.tv_sec);
27   abs_timeout.tv_nsec = 0;
28   abs_timeout.tv_sec += 10;
29 
30   r = pthread_rwlock_init(&rwlock, NULL); assert(r == 0);
31   fprintf(stderr, "Locking rwlock via pthread_rwlock_wrlock().\n");
32   r = pthread_rwlock_wrlock(&rwlock); assert(r == 0);
33   r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
34   fprintf(stderr, "Locking rwlock via pthread_rwlock_trywrlock().\n");
35   r = pthread_rwlock_trywrlock(&rwlock); assert(r == 0);
36   r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
37   fprintf(stderr, "Locking rwlock via pthread_rwlock_timedwrlock().\n");
38 #ifdef HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
39   r = pthread_rwlock_timedwrlock(&rwlock, &abs_timeout); assert(r == 0);
40   r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
41 #endif
42   fprintf(stderr, "Locking rwlock via pthread_rwlock_rdlock().\n");
43   r = pthread_rwlock_rdlock(&rwlock); assert(r == 0);
44   r = pthread_rwlock_rdlock(&rwlock); assert(r == 0);
45   r = pthread_rwlock_rdlock(&rwlock); assert(r == 0);
46   r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
47   r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
48   r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
49   fprintf(stderr, "Locking rwlock via pthread_rwlock_tryrdlock().\n");
50   r = pthread_rwlock_tryrdlock(&rwlock); assert(r == 0);
51   r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
52   fprintf(stderr, "Locking rwlock via pthread_rwlock_timedrdlock().\n");
53 #ifdef HAVE_PTHREAD_RWLOCK_TIMEDRDLOCK
54   r = pthread_rwlock_timedrdlock(&rwlock, &abs_timeout); assert(r == 0);
55   r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
56 #endif
57   fprintf(stderr, "Attempt to lock for writing recursively (not allowed).\n");
58   r = pthread_rwlock_wrlock(&rwlock); assert(r == 0);
59   r = pthread_rwlock_wrlock(&rwlock); assert(r != 0);
60   r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
61   r = pthread_rwlock_destroy(&rwlock); assert(r == 0);
62 
63   r = pthread_mutex_init(&mutex, NULL); assert(r == 0);
64   fprintf(stderr, "Locking mutex via pthread_mutex_trylock().\n");
65   r = pthread_mutex_trylock(&mutex); assert(r == 0);
66   r = pthread_mutex_unlock(&mutex); assert(r == 0);
67   fprintf(stderr, "Locking mutex via pthread_mutex_lock().\n");
68   r = pthread_mutex_lock(&mutex); assert(r == 0);
69   r = pthread_mutex_unlock(&mutex); assert(r == 0);
70   fprintf(stderr, "Locking mutex via pthread_mutex_timedlock().\n");
71 #ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK
72   r = pthread_mutex_timedlock(&mutex, &abs_timeout); assert(r == 0);
73   r = pthread_mutex_unlock(&mutex); assert(r == 0);
74 #endif
75   r = pthread_mutex_destroy(&mutex);
76 
77   return 0;
78 }
79