1 /******************************************************************************
2 *
3 * Copyright © International Business Machines Corp., 2008
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * NAME
20 * testpi-6.c
21 *
22 * DESCRIPTION
23 * This testcase verifies if a thread can lock the robust mutex multiple
24 * times.
25 *
26 * USAGE:
27 * Use run_auto.sh script in current directory to build and run test.
28 *
29 * AUTHOR
30 *
31 *
32 * HISTORY
33 * 2010-04-22 Code cleanup by Gowrishankar
34 *
35 *
36 *****************************************************************************/
37
38 #include <stdio.h>
39 #include <pthread.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <librttest.h>
43
44 #if HAS_PTHREAD_MUTEXTATTR_ROBUST_APIS
45 pthread_mutex_t child_mutex;
46
child_thread(void * arg)47 void *child_thread(void *arg)
48 {
49 int ret;
50
51 ret = pthread_mutex_lock(&child_mutex);
52 if (ret != 0)
53 printf("child thread: Failed to lock child_mutex: %d\n", ret);
54 else
55 printf("child_thread: got lock\n");
56
57 sleep(2);
58
59 printf("child_thread: Trying to get lock 2nd time\n");
60 ret = pthread_mutex_lock(&child_mutex);
61 if (ret != 0)
62 printf("child thread: Failed to lock child_mutex: %d\n", ret);
63 else
64 printf("child_thread: got lock 2nd time !!\n");
65
66 return NULL;
67 }
68
do_test(int argc,char ** argv)69 int do_test(int argc, char **argv)
70 {
71 pthread_mutexattr_t mutexattr;
72 int retc, robust;
73
74 if (pthread_mutexattr_init(&mutexattr) != 0)
75 printf("Failed to init mutexattr\n");
76
77 if (pthread_mutexattr_setrobust(&mutexattr,
78 PTHREAD_MUTEX_ROBUST) != 0)
79 printf("Can't set robust mutex\n");
80
81 if (pthread_mutexattr_getrobust(&mutexattr, &robust) != 0)
82 printf("Can't get mutexattr protocol\n");
83 else
84 printf("robust in mutexattr is %d\n", robust);
85
86 retc = pthread_mutex_init(&child_mutex, &mutexattr);
87 if (retc != 0)
88 printf("Failed to init mutex: %d\n", retc);
89
90 create_other_thread(child_thread, NULL);
91 join_threads();
92
93 return 0;
94 }
95 #else
do_test(int argc,char ** argv)96 int do_test(int argc, char **argv)
97 {
98 printf("Your system doesn't have robust pthread mutex support\n");
99 return 1;
100 }
101 #endif
102
103 #include "test-skeleton.c"
104