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