]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/valgrind/src/valgrind-3.6.0-svn/drd/tests/pth_cleanup_handler.c
Inital import
[l4.git] / l4 / pkg / valgrind / src / valgrind-3.6.0-svn / drd / tests / pth_cleanup_handler.c
1 /*
2  * Test program for verifying whether pthread cleanup handlers are invoked
3  * correctly.
4  */
5
6
7 #include <stdbool.h>
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <pthread.h>
11 #include <stdlib.h>
12
13
14 static pthread_mutex_t s_mutex;
15
16
17 static void cleanup_handler(void* param)
18 {
19   fprintf(stderr, "Cleanup handler has been called.\n");
20   pthread_mutex_unlock(&s_mutex);
21 }
22
23 static void* f(void *p)
24 {
25   if (pthread_mutex_lock(&s_mutex) != 0)
26   {
27     fprintf(stderr, "pthread_mutex_lock()\n");
28     exit(1);
29   }
30
31   pthread_cleanup_push(cleanup_handler, NULL);
32   pthread_exit(0);
33   pthread_cleanup_pop(true);
34 }
35
36
37 int main()
38 {
39   pthread_t pt1, pt2;
40
41   // Make sure the program exits in case a deadlock has been triggered.
42   alarm(2);
43
44   if (pthread_mutex_init(&s_mutex, NULL) != 0)
45   {
46     fprintf(stderr, "pthread_mutex_init()\n");
47     exit(1);
48   }
49   if (pthread_create(&pt1, NULL, f, NULL) != 0)
50   {
51     fprintf(stderr, "pthread_create()\n");
52     exit(1);
53   }
54   if (pthread_create(&pt2, NULL, f, NULL) != 0)
55   {
56     fprintf(stderr, "pthread_create()\n");
57     exit(1);
58   }
59
60   pthread_join(pt1, 0);
61   pthread_join(pt2, 0);
62
63   fprintf(stderr, "Test succeeded.\n");
64
65   return 0;
66 }