Add DRD suppression patterns for races triggered by std::ostream
[valgrind.git] / drd / tests / recursive_mutex.c
blob7d8ff2d371caffe1546451c87c7d998edaebc681
1 /** Initialize several kinds of mutexes and lock each mutex twice.
2 * Note: locking a regular mutex twice causes a deadlock.
3 */
5 #define _GNU_SOURCE
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <pthread.h>
10 #include "../../config.h"
13 static void lock_twice(pthread_mutex_t* const p)
15 if (pthread_mutex_trylock(p) != 0)
16 fprintf(stderr, "first lock call failed !\n");
17 if (pthread_mutex_trylock(p) != 0)
18 fprintf(stderr, "second lock call failed !\n");
19 if (pthread_mutex_unlock(p) != 0)
20 fprintf(stderr, "first unlock call failed !\n");
21 if (pthread_mutex_unlock(p) != 0)
22 fprintf(stderr, "second unlock call failed !\n");
25 int main(int argc, char** argv)
27 #if defined(HAVE_PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
29 pthread_mutex_t m = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
31 fprintf(stderr, "Recursive mutex (statically initialized).\n");
32 lock_twice(&m);
33 pthread_mutex_destroy(&m);
35 #endif
36 #if defined(HAVE_PTHREAD_MUTEX_RECURSIVE_NP)
38 pthread_mutex_t m;
39 pthread_mutexattr_t attr;
41 fprintf(stderr, "\nRecursive mutex (initialized via mutex attributes).\n");
42 pthread_mutexattr_init(&attr);
43 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
44 pthread_mutex_init(&m, &attr);
45 pthread_mutexattr_destroy(&attr);
46 lock_twice(&m);
47 pthread_mutex_destroy(&m);
49 #endif
50 #if defined(HAVE_PTHREAD_MUTEX_ERRORCHECK_NP)
52 pthread_mutex_t m;
53 pthread_mutexattr_t attr;
55 fprintf(stderr, "\nError checking mutex.\n");
56 pthread_mutexattr_init(&attr);
57 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK_NP);
58 pthread_mutex_init(&m, &attr);
59 pthread_mutexattr_destroy(&attr);
60 lock_twice(&m);
61 pthread_mutex_destroy(&m);
63 #endif
66 pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
68 fprintf(stderr, "\nNon-recursive mutex.\n");
69 lock_twice(&m);
72 fprintf(stderr, "\nDone.\n");
74 return 0;