Add DRD suppression patterns for races triggered by std::ostream
[valgrind.git] / drd / tests / pth_cleanup_handler.c
blob0fb3d073d66839e457505bcf1dcdace531c62f45
1 /*
2 * Test program for verifying whether pthread cleanup handlers are invoked
3 * correctly.
4 */
7 #include <stdbool.h>
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <pthread.h>
11 #include <stdlib.h>
14 static pthread_mutex_t s_mutex;
17 static void cleanup_handler(void* param)
19 fprintf(stderr, "Cleanup handler has been called.\n");
20 pthread_mutex_unlock(&s_mutex);
23 static void* f(void *p)
25 if (pthread_mutex_lock(&s_mutex) != 0)
27 fprintf(stderr, "pthread_mutex_lock()\n");
28 exit(1);
31 pthread_cleanup_push(cleanup_handler, NULL);
32 pthread_exit(0);
33 pthread_cleanup_pop(true);
37 int main()
39 pthread_t pt1, pt2;
41 // Make sure the program exits in case a deadlock has been triggered.
42 alarm(20);
44 if (pthread_mutex_init(&s_mutex, NULL) != 0)
46 fprintf(stderr, "pthread_mutex_init()\n");
47 exit(1);
49 if (pthread_create(&pt1, NULL, f, NULL) != 0)
51 fprintf(stderr, "pthread_create()\n");
52 exit(1);
54 if (pthread_create(&pt2, NULL, f, NULL) != 0)
56 fprintf(stderr, "pthread_create()\n");
57 exit(1);
60 pthread_join(pt1, 0);
61 pthread_join(pt2, 0);
63 fprintf(stderr, "Test succeeded.\n");
65 return 0;