• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 pthread_mutex_t child_mutex;
45 
child_thread(void * arg)46 void *child_thread(void *arg)
47 {
48 	int ret;
49 
50 	ret = pthread_mutex_lock(&child_mutex);
51 	if (ret != 0)
52 		printf("child thread: Failed to lock child_mutex: %d\n", ret);
53 	else
54 		printf("child_thread: got lock\n");
55 
56 	sleep(2);
57 
58 	printf("child_thread: Trying to get lock 2nd time\n");
59 	ret = pthread_mutex_lock(&child_mutex);
60 	if (ret != 0)
61 		printf("child thread: Failed to lock child_mutex: %d\n", ret);
62 	else
63 		printf("child_thread: got lock 2nd time !!\n");
64 
65 	return NULL;
66 }
67 
do_test(int argc,char ** argv)68 int do_test(int argc, char **argv)
69 {
70 	pthread_mutexattr_t mutexattr;
71 	int retc, robust;
72 
73 	if (pthread_mutexattr_init(&mutexattr) != 0)
74 		printf("Failed to init mutexattr\n");
75 
76 	if (pthread_mutexattr_setrobust(&mutexattr,
77 					   PTHREAD_MUTEX_ROBUST) != 0)
78 		printf("Can't set robust mutex\n");
79 
80 	if (pthread_mutexattr_getrobust(&mutexattr, &robust) != 0)
81 		printf("Can't get mutexattr protocol\n");
82 	else
83 		printf("robust in mutexattr is %d\n", robust);
84 
85 	retc = pthread_mutex_init(&child_mutex, &mutexattr);
86 	if (retc != 0)
87 		printf("Failed to init mutex: %d\n", retc);
88 
89 	create_other_thread(child_thread, NULL);
90 	join_threads();
91 
92 	return 0;
93 }
94 
95 #include "test-skeleton.c"
96