Home | History | Annotate | Download | only in pi-tests
      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-5.c
     21  *
     22  * DESCRIPTION
     23  *      This testcase verifies if a thread can lock the priority inheritance
     24  *      mutex multiple 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 pthread_mutex_t child_mutex;
     44 
     45 void *child_thread(void *arg)
     46 {
     47 	int ret;
     48 
     49 	ret = pthread_mutex_lock(&child_mutex);
     50 	if (ret != 0)
     51 		printf("child thread: Failed to lock child_mutex: %d\n", ret);
     52 	else
     53 		printf("child_thread: got lock\n");
     54 
     55 	sleep(2);
     56 
     57 	printf("child_thread: Trying to get lock 2nd time\n");
     58 	ret = pthread_mutex_lock(&child_mutex);
     59 	if (ret != 0)
     60 		printf("child thread: Failed to lock child_mutex: %d\n", ret);
     61 	else
     62 		printf("child_thread: got lock 2nd time !!\n");
     63 
     64 	return NULL;
     65 }
     66 
     67 int do_test(int argc, char **argv)
     68 {
     69 	pthread_mutexattr_t mutexattr;
     70 	int retc, protocol;
     71 
     72 #if HAS_PRIORITY_INHERIT
     73 
     74 	if (pthread_mutexattr_init(&mutexattr) != 0)
     75 		printf("Failed to init mutexattr\n");
     76 
     77 	if (pthread_mutexattr_setprotocol(&mutexattr,
     78 					  PTHREAD_PRIO_INHERIT) != 0)
     79 		printf("Can't set protocol prio inherit\n");
     80 
     81 	if (pthread_mutexattr_getprotocol(&mutexattr, &protocol) != 0)
     82 		printf("Can't get mutexattr protocol\n");
     83 	else
     84 		printf("protocol in mutexattr is %d\n", protocol);
     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 #else
     95 	return 1;
     96 #endif
     97 }
     98 
     99 #include "test-skeleton.c"
    100