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