memcheck/tests/sh-mem-random.c: Set huge_addr to 240GB
[valgrind.git] / drd / tests / trylock.c
blob1aca703408244845cd75ecfbf2295d96d1ab1307
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 */
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). */
11 #define _GNU_SOURCE 1
13 #include "../../config.h"
14 #include <errno.h>
15 #include <stdio.h>
16 #include <assert.h>
17 #include <pthread.h>
20 int main(int argc, char** argv)
22 int r;
23 pthread_mutex_t mutex;
24 pthread_rwlock_t rwlock;
25 struct timespec abs_timeout;
27 time(&abs_timeout.tv_sec);
28 abs_timeout.tv_nsec = 0;
29 abs_timeout.tv_sec += 10;
31 r = pthread_rwlock_init(&rwlock, NULL); assert(r == 0);
32 fprintf(stderr, "Locking rwlock via pthread_rwlock_wrlock().\n");
33 r = pthread_rwlock_wrlock(&rwlock); assert(r == 0);
34 r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
35 fprintf(stderr, "Locking rwlock via pthread_rwlock_trywrlock().\n");
36 r = pthread_rwlock_trywrlock(&rwlock); assert(r == 0);
37 r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
38 fprintf(stderr, "Locking rwlock via pthread_rwlock_timedwrlock().\n");
39 #ifdef HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
40 r = pthread_rwlock_timedwrlock(&rwlock, &abs_timeout); assert(r == 0);
41 r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
42 #endif
43 fprintf(stderr, "Locking rwlock via pthread_rwlock_rdlock().\n");
44 r = pthread_rwlock_rdlock(&rwlock); assert(r == 0);
45 r = pthread_rwlock_rdlock(&rwlock); assert(r == 0);
46 r = pthread_rwlock_rdlock(&rwlock); assert(r == 0);
47 r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
48 r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
49 r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
50 fprintf(stderr, "Locking rwlock via pthread_rwlock_tryrdlock().\n");
51 r = pthread_rwlock_tryrdlock(&rwlock); assert(r == 0);
52 r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
53 fprintf(stderr, "Locking rwlock via pthread_rwlock_timedrdlock().\n");
54 #ifdef HAVE_PTHREAD_RWLOCK_TIMEDRDLOCK
55 r = pthread_rwlock_timedrdlock(&rwlock, &abs_timeout); assert(r == 0);
56 r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
57 #endif
58 fprintf(stderr, "Attempt to lock for writing recursively (not allowed).\n");
59 r = pthread_rwlock_wrlock(&rwlock); assert(r == 0);
60 r = pthread_rwlock_trywrlock(&rwlock); assert(r == EBUSY);
61 r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
62 r = pthread_rwlock_destroy(&rwlock); assert(r == 0);
64 r = pthread_mutex_init(&mutex, NULL); assert(r == 0);
65 fprintf(stderr, "Locking mutex via pthread_mutex_trylock().\n");
66 r = pthread_mutex_trylock(&mutex); assert(r == 0);
67 r = pthread_mutex_unlock(&mutex); assert(r == 0);
68 fprintf(stderr, "Locking mutex via pthread_mutex_lock().\n");
69 r = pthread_mutex_lock(&mutex); assert(r == 0);
70 r = pthread_mutex_unlock(&mutex); assert(r == 0);
71 fprintf(stderr, "Locking mutex via pthread_mutex_timedlock().\n");
72 #ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK
73 r = pthread_mutex_timedlock(&mutex, &abs_timeout); assert(r == 0);
74 r = pthread_mutex_unlock(&mutex); assert(r == 0);
75 #endif
76 r = pthread_mutex_destroy(&mutex);
78 return 0;