genericopenlibs/openenvcore/libpthread/test/testpthread/src/tpthreadblocks.cpp
changeset 31 ce057bb09d0b
child 45 4b03adbd26ca
equal deleted inserted replaced
30:e20de85af2ee 31:ce057bb09d0b
       
     1 /*
       
     2 * Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "tpthread.h"
       
    20 
       
    21 
       
    22 // Forward declarations of thread entry functions
       
    23 void* ThreadEntryPoint(void* aParam);
       
    24 void* SocketReadThreadEntryPoint( void* aParam );
       
    25 void* SocketWriteThreadEntryPoint( void* aParam );
       
    26 void* FileReadThreadEntryPoint( void* aParam );
       
    27 void* FileWriteThreadEntryPoint( void* aParam );
       
    28 void *ThreadEntryPointDummy(void* aParam ); 
       
    29 
       
    30 //static functions used for PThreadJoinTest case
       
    31 static void *first(void *arg); 
       
    32 static void *second(void *arg); 
       
    33 static void *third(void *arg);
       
    34 	
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // CTPThreadBasicApis::PThreadBasicApisTest
       
    38 // Description: This function does most of the basic pthread api testing
       
    39 // This test function can be called with below syntax from INI file:
       
    40 // PThreadBasicAPITest <API> <TestType> [<State> <StackSize>]
       
    41 // API is an integer which can be any of the below:
       
    42 // 		PTHREAD_SELF (1)
       
    43 // 		PTHREAD_CREATE (2)
       
    44 // 		PTHREAD_DETACH (3)
       
    45 // 		PTHREAD_JOIN (4)
       
    46 // 		PTHREAD_EXIT (5)
       
    47 // TestType is an integer which can be any of the below:
       
    48 // 		POSITIVE_TEST (1)
       
    49 // 		NEGATIVE_TEST (2)
       
    50 // State is an integer which can be any of the below:
       
    51 // 		JOIN_THREAD (0)
       
    52 // 		DETACH_THREAD (1)
       
    53 // StackSize is the size of the stack whic is an integer
       
    54 // PThreadBasicAPITest PTHREAD_SELF NEGATIVE_TEST
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 TInt CTestPThread::PThreadBasicApisTest()
       
    58     {
       
    59 
       
    60     INFO_PRINTF1(_L("In TPThreadBasicApis"));
       
    61 
       
    62     TInt retVal = KErrNone;
       
    63     TInt apiType = 1;
       
    64     
       
    65     _LIT( KParameter1, "Parameter1" );
       
    66 	TBool res = GetIntFromConfig(ConfigSection(), KParameter1, apiType);
       
    67 	
       
    68     TInt testType = 1;
       
    69     _LIT( KParameter2, "Parameter2" );
       
    70 	res = GetIntFromConfig(ConfigSection(), KParameter2, testType);
       
    71 	 
       
    72     switch(apiType)
       
    73     	{
       
    74     	case EPThreadSelf:
       
    75     		{
       
    76     		retVal = PThreadSelfApiTest();
       
    77     		break;
       
    78     		}
       
    79     	case EPThreadCreate:
       
    80     		{
       
    81     		retVal = PThreadCreateApiTest( (TTestType)testType );
       
    82     		break;
       
    83     		}
       
    84     	case EPThreadJoin:
       
    85     		{
       
    86     		retVal = PThreadJoinApiTest(  (TTestType)testType );
       
    87     		break;
       
    88     		}
       
    89     	case EPThreadDetach:
       
    90     		{
       
    91     		retVal = PThreadDetachApiTest(  (TTestType)testType );
       
    92     		break;
       
    93     		}
       
    94     	case EPThreadExit:
       
    95     		{
       
    96     		//Read Exit Reasson from INI file
       
    97     		TInt exitReason = 0;
       
    98     		_LIT( KParameter3, "Parameter3" );
       
    99 			res = GetIntFromConfig(ConfigSection(), KParameter3, exitReason);
       
   100 			if(!res)
       
   101 			{
       
   102 			 	_LIT(Kerr , "Failed to read parameter exit reason from ini file") ;
       
   103 			 	ERR_PRINTF1(Kerr) ;
       
   104 			 	return KErrGeneral ;
       
   105 			}
       
   106     		
       
   107     		retVal = PThreadExitApiTest( exitReason );
       
   108     		break;
       
   109     		}
       
   110     	default:
       
   111     		{
       
   112     		INFO_PRINTF2(_L("Invalid arguments from ini file : %d"), apiType);
       
   113     		retVal = KErrArgument;
       
   114     		break;
       
   115     		}
       
   116     	}
       
   117 
       
   118     return retVal;
       
   119     }
       
   120 // -----------------------------------------------------------------------------
       
   121 // CTPThreadBasicApis::PThreadSelfApiTest
       
   122 // Description: This function tests pthread_self API
       
   123 // returns KErrNone or error code depending on the test
       
   124 // -----------------------------------------------------------------------------
       
   125 //
       
   126 TInt CTestPThread::PThreadSelfApiTest()
       
   127 	{
       
   128 
       
   129 	TInt retVal = KErrNone;
       
   130 
       
   131     pthread_t selfID = pthread_self();
       
   132 
       
   133 	
       
   134     if( selfID == 0 )
       
   135     	{
       
   136 	    INFO_PRINTF2(_L("Wrong return Value from pthread_self() : %d"), selfID);
       
   137 		retVal = KErrPThreadSelf;
       
   138     	}
       
   139     else
       
   140     	{
       
   141    		pthread_t threadID = 0;
       
   142 
       
   143 		pthread_attr_t threadAttr;
       
   144 		pthread_attr_init( &threadAttr );
       
   145 
       
   146 		retVal = pthread_create( &threadID, &threadAttr, ThreadEntryPoint,
       
   147 			(void*)KErrNone );
       
   148 		if( KErrNone != retVal )
       
   149 			{
       
   150 			INFO_PRINTF2(_L("pthread_create FAILED with %d"), retVal );
       
   151 			retVal = KErrPThreadCreate;
       
   152 			}
       
   153 		else
       
   154 			{
       
   155 			//Testing pthread self aswell.....
       
   156 			if(pthread_equal(selfID,selfID) == 0)
       
   157 				{
       
   158 				retVal += KErrPThreadSelf;
       
   159 				}
       
   160 			if(pthread_equal(selfID,threadID))
       
   161 				{	
       
   162 				retVal += KErrPThreadSelf;
       
   163 				}
       
   164 			
       
   165 			//get the thread id again, if it does not match.. ERROR
       
   166 			pthread_t selfID1 = pthread_self();
       
   167 			if( selfID1 != selfID )
       
   168 				{
       
   169 				INFO_PRINTF1(_L("undefined behaviour from pthread_self!!"));
       
   170 				retVal += KErrPThreadSelf;
       
   171 				}
       
   172 			else
       
   173 				{
       
   174 		    	TBuf<20> apiName(KPThreadSelf);
       
   175 				INFO_PRINTF2(_L("API %S returned 1"),&apiName);
       
   176 				}
       
   177 			}
       
   178 		int threadRetVal = 0;
       
   179 		TInt ret = pthread_join(threadID, (void**)threadRetVal);
       
   180     	}
       
   181     return retVal;
       
   182 	}
       
   183 
       
   184 // -----------------------------------------------------------------------------
       
   185 // CTPThreadBasicApis::PThreadCreateApiTest
       
   186 // Description: This function tests pthread_create and
       
   187 // pthread attr related APIs
       
   188 // returns KErrNone or error code depending on the test
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 TInt CTestPThread::PThreadCreateApiTest(TTestType aTestType )
       
   192 	{
       
   193     TInt retVal = KErrNone;
       
   194 	pthread_t threadID = 0;
       
   195 	pthread_attr_t threadAttr;
       
   196 
       
   197 	pthread_t selfID = pthread_self();
       
   198 	INFO_PRINTF2(_L("Thread Id is : %d"), selfID);
       
   199 
       
   200 	if( aTestType == EPositive )
       
   201 		{
       
   202 		//Read Attributes like detatch state and stack size from INI file
       
   203 		TInt threadState = PTHREAD_CREATE_DETACHED;
       
   204 		TInt stackSize = KDefaultPThreadStackSize;
       
   205         TUint stkSize;
       
   206         
       
   207 		ReadThreadAttributes( threadState, stackSize, aTestType);
       
   208 
       
   209 		//Set all attributes as 0
       
   210 		memset(&threadAttr, 0, sizeof(threadAttr));
       
   211 		pthread_attr_init( &threadAttr );
       
   212 		pthread_attr_setdetachstate( &threadAttr, threadState );
       
   213 		if (pthread_attr_setstacksize( &threadAttr, stackSize ) == EINVAL)
       
   214             {
       
   215             // Test case passed
       
   216             return KErrNone;
       
   217             }
       
   218         //This has been added to cover getstack size test case
       
   219         if (pthread_attr_getstacksize( &threadAttr, &stkSize ) != 0)
       
   220             {
       
   221             return KErrPThreadCreate;
       
   222             }
       
   223         if (stackSize != stkSize)
       
   224             {
       
   225             return KErrPThreadCreate;
       
   226             }
       
   227 		}
       
   228 
       
   229     //Create a thread now
       
   230 	retVal = pthread_create( &threadID, &threadAttr, ThreadEntryPoint,
       
   231 			(void*)KErrNone );
       
   232 
       
   233 	selfID = pthread_self();
       
   234 	INFO_PRINTF2(_L("Thread Id is : %d"), selfID);
       
   235 
       
   236 	if( aTestType == ENegative && EINVAL != retVal )
       
   237 		{
       
   238 		// As we have sent some wrong parameters, this should return with EINVAL
       
   239 		INFO_PRINTF2(_L("Wrong return Value from pthread_create() : %d"), retVal);
       
   240 		retVal = KErrPThreadCreate;
       
   241 		}
       
   242 	else if( aTestType == EPositive && KErrNone != retVal )
       
   243 		{
       
   244 		// As we have sent some wrong parameters,
       
   245 		// this should return with EINVAL
       
   246 		INFO_PRINTF2(_L("pthread_create() Failed with : %d !!"), retVal);
       
   247 		retVal = KErrPThreadCreate;
       
   248 		}
       
   249 	else
       
   250 		{
       
   251     	TBuf<20> apiName(KPThreadCreate);
       
   252 		INFO_PRINTF3(_L("API Test %S of type %d passed"),&apiName,aTestType);
       
   253 		retVal = KErrNone;
       
   254 		}
       
   255 	pthread_attr_destroy( &threadAttr );
       
   256     return retVal;
       
   257 	}
       
   258 
       
   259 // -----------------------------------------------------------------------------
       
   260 // CTPThreadBasicApis::PThreadJoinApiTest
       
   261 // Description: This function tests pthread_join API
       
   262 // returns KErrNone or error code depending on the test
       
   263 // -----------------------------------------------------------------------------
       
   264 //
       
   265 TInt CTestPThread::PThreadJoinApiTest(TTestType aTestType )
       
   266 	{
       
   267     TInt retVal = KErrNone;
       
   268 	pthread_t threadID = 0;
       
   269     pthread_attr_t threadAttr;
       
   270 	TInt threadState = PTHREAD_CREATE_DETACHED;
       
   271 	TInt stackSize = KDefaultPThreadStackSize;
       
   272 	TInt moreFlag = 0;
       
   273 
       
   274     if( aTestType == EPositive )
       
   275     	{
       
   276 		//Read Attributes like detatch state and stack size from INI file
       
   277 		ReadThreadAttributes( threadState, stackSize, aTestType);
       
   278 	
       
   279 		_LIT( KParameter5, "Parameter5" );
       
   280 		TBool res = GetIntFromConfig(ConfigSection(), KParameter5, moreFlag);
       
   281 				
       
   282 		if( PTHREAD_CREATE_DETACHED == threadState )
       
   283 			{
       
   284 			aTestType = ENegative;
       
   285 			}
       
   286 
       
   287 		pthread_attr_init( &threadAttr );
       
   288 		pthread_attr_setdetachstate( &threadAttr, threadState );
       
   289 		pthread_attr_setstacksize( &threadAttr, stackSize );
       
   290 
       
   291 	    //Create a thread now
       
   292 		retVal = pthread_create( &threadID, &threadAttr, ThreadEntryPoint,
       
   293 			(void*)KErrNone );
       
   294     	}
       
   295 
       
   296 	if( KErrNone != retVal )
       
   297 		{
       
   298 		// pthread_Create Failed!!
       
   299 		INFO_PRINTF2(_L("pthread_create() Failed with : %d"), retVal);
       
   300 		retVal = KErrPThreadCreate;
       
   301 		}
       
   302 	else
       
   303 		{
       
   304 		int threadRetVal = 0;
       
   305 		TInt ret = pthread_join(threadID, (void**)threadRetVal);
       
   306 
       
   307 		if( ENegative == aTestType )
       
   308 			{
       
   309 			//If thread id is 0, then return should be ESRCH
       
   310 			if( 0 == threadID )
       
   311 				{
       
   312 				if(ESRCH != ret )
       
   313 					{
       
   314 					INFO_PRINTF2(_L("Unexpected return value from pthread_join: %d"), retVal);
       
   315 					INFO_PRINTF1(_L("Expected ESRCH as return error code"));
       
   316 					retVal = KErrPThreadJoin;
       
   317 					}
       
   318 				}
       
   319 			//If thread state is not joinable, then return should be EINVAL
       
   320 			else if ( PTHREAD_CREATE_JOINABLE == threadState )
       
   321 				{
       
   322 				if( EINVAL != ret )
       
   323 					{
       
   324 					INFO_PRINTF2(_L("Unexpected return value from pthread_join: %d"), retVal);
       
   325 					INFO_PRINTF1(_L("Expected EINVAL as return error code"));
       
   326 					retVal = KErrPThreadJoin;
       
   327 					}
       
   328 				}
       
   329 			}
       
   330 	    //If its positive test, then return should be 0
       
   331 	    else if( KErrNone != ret )
       
   332 	    	{
       
   333 	    	INFO_PRINTF2(_L("Unexpected return value from pthread_join: %d"), retVal);
       
   334 	    	INFO_PRINTF1(_L("Expected pthread_join to return successfully"));
       
   335 			retVal = KErrPThreadJoin;
       
   336 	    	}
       
   337 	    else if( moreFlag )
       
   338 	    	{
       
   339 	    	//issue pthread_join again on same thread which is terminated already
       
   340 	    	if( ESRCH != (ret = pthread_join(threadID, (void**)threadRetVal)) )
       
   341 	    		{
       
   342 				INFO_PRINTF2(_L("Unexpected return value from pthread_join: %d"), ret);
       
   343 				INFO_PRINTF1(_L("Expected ESRCH as return error code"));
       
   344 				retVal = KErrPThreadJoin;
       
   345 	    		}
       
   346 	    	}
       
   347 
       
   348 	    if( KErrNone == retVal )
       
   349 	    	{
       
   350 	    	TBuf<20> apiName(KPThreadJoin);
       
   351 			LogResult(aTestType, apiName);
       
   352 	    	}
       
   353 		}
       
   354 
       
   355     return retVal;
       
   356 	}
       
   357 
       
   358 // -----------------------------------------------------------------------------
       
   359 // CTPThreadBasicApis::PThreadDetachApiTest
       
   360 // Description: This function tests pthread_detach API
       
   361 // returns KErrNone or error code depending on the test
       
   362 // -----------------------------------------------------------------------------
       
   363 //
       
   364 TInt CTestPThread::PThreadDetachApiTest( TTestType aTestType )
       
   365 	{
       
   366     TInt retVal = KErrNone;
       
   367 	pthread_t threadID = 0;
       
   368     pthread_attr_t threadAttr;
       
   369 	TInt threadState = PTHREAD_CREATE_JOINABLE;
       
   370 	TInt thrState;
       
   371 	TInt stackSize = KDefaultPThreadStackSize;
       
   372 	TInt moreFlag = 0;
       
   373 
       
   374     if( aTestType == EPositive )
       
   375     	{
       
   376 		//Read Attributes like detatch state and stack size from INI file
       
   377 		ReadThreadAttributes( threadState, stackSize, aTestType);
       
   378 		
       
   379 		_LIT( KParameter5, "Parameter5" );
       
   380 		TBool res = GetIntFromConfig(ConfigSection(), KParameter5, moreFlag);
       
   381 		
       
   382 		
       
   383 		if( PTHREAD_CREATE_DETACHED == threadState )
       
   384 			{
       
   385 			aTestType = ENegative;
       
   386 			}
       
   387 
       
   388 		pthread_attr_init( &threadAttr );
       
   389 		pthread_attr_setdetachstate( &threadAttr, threadState );
       
   390 		//testing getdetachstate aswell....
       
   391 		if(pthread_attr_getdetachstate(&threadAttr, &thrState))
       
   392 			{
       
   393 		    retVal += KErrPThreadDetach;
       
   394 			}
       
   395 		if(threadState != thrState)
       
   396 			{
       
   397 		    retVal += KErrPThreadDetach;
       
   398 			}
       
   399 		pthread_attr_setstacksize( &threadAttr, stackSize );
       
   400 
       
   401 	    //Create a thread now
       
   402 		retVal += pthread_create( &threadID, &threadAttr, ThreadEntryPoint,
       
   403 			(void*)KErrNone );
       
   404     	}
       
   405 
       
   406 	if( KErrNone != retVal )
       
   407 		{
       
   408 		// pthread_Create Failed!!
       
   409 		INFO_PRINTF2(_L("pthread_create() Failed with : %d"), retVal);
       
   410 		retVal = KErrPThreadCreate;
       
   411 		}
       
   412 	else
       
   413 		{
       
   414 		TInt ret = pthread_detach( threadID );
       
   415 
       
   416 		if( ENegative == aTestType )
       
   417 			{
       
   418 			//If thread id is 0, then return should be ESRCH
       
   419 			if( 0 == threadID )
       
   420 				{
       
   421 				if( ESRCH != ret )
       
   422 					{
       
   423 					INFO_PRINTF2(_L("Unexpected return value from pthread_detach: %d"), retVal);
       
   424 					INFO_PRINTF1(_L("Expected ESRCH as return error code"));
       
   425 					retVal = KErrPThreadDetach;
       
   426 					}
       
   427 				}
       
   428 			//If thread state is not joinable, then return should be EINVAL
       
   429 			else if ( PTHREAD_CREATE_JOINABLE == threadState )
       
   430 				{
       
   431 				if( EINVAL != ret )
       
   432 					{
       
   433 					INFO_PRINTF2(_L("Unexpected return value from pthread_detach: %d"), retVal);
       
   434 					INFO_PRINTF1(_L("Expected EINVAL as return error code"));
       
   435 					retVal = KErrPThreadDetach;
       
   436 					}
       
   437 				}
       
   438 			}
       
   439 	    //If its positive test, and return should be 0
       
   440 	    else if( ret != KErrNone )
       
   441 	    	{
       
   442 	    	INFO_PRINTF2(_L("Unexpected return value from pthread_detach: %d"), retVal);
       
   443 	    	INFO_PRINTF1(_L("Expected pthread_detach to return successfully"));
       
   444 			retVal = KErrPThreadDetach;
       
   445 	    	}
       
   446 	    else if( moreFlag )
       
   447 	    	{
       
   448 	    	//issue pthread_detach again on same thread, which is no more joinable
       
   449 	    	//return should be EINVAL
       
   450 	    	ret = pthread_detach( threadID );
       
   451 	    	if( (EINVAL != ret ) &&  (ESRCH != ret))
       
   452 	    		{
       
   453 				INFO_PRINTF2(_L("Unexpected return value from pthread_detach: %d"), ret);
       
   454 				INFO_PRINTF1(_L("Expected ESRCH as return error code"));
       
   455 				retVal = KErrPThreadDetach;
       
   456 	    		}
       
   457 	    	}
       
   458 
       
   459 	    if( KErrNone == retVal )
       
   460 	    	{
       
   461 	    	TBuf<20> apiName(KPThreadDetach);
       
   462 			LogResult(aTestType, apiName);
       
   463 	    	}
       
   464 		}
       
   465 
       
   466     return retVal;
       
   467 	}
       
   468 
       
   469 // -----------------------------------------------------------------------------
       
   470 // CTPThreadBasicApis::PThreadExitApiTest
       
   471 // Description: This function tests pthread_exit API
       
   472 // returns KErrNone or error code depending on the test
       
   473 // -----------------------------------------------------------------------------
       
   474 //
       
   475 TInt CTestPThread::PThreadExitApiTest( TInt aExitReason )
       
   476 	{
       
   477     TInt retVal = KErrNone;
       
   478 	pthread_t threadID = 0;
       
   479 
       
   480 	pthread_attr_t threadAttr;
       
   481 	pthread_attr_init( &threadAttr );
       
   482 	pthread_attr_setdetachstate( &threadAttr, PTHREAD_CREATE_JOINABLE );
       
   483     //Create a thread now
       
   484 	retVal = pthread_create( &threadID, &threadAttr, ThreadEntryPoint,
       
   485 		(void*)aExitReason );
       
   486 
       
   487 	if( KErrNone != retVal )
       
   488 		{
       
   489 		// pthread_Create Failed!!
       
   490 		INFO_PRINTF2(_L("pthread_create() Failed with : %d"), retVal);
       
   491 		retVal = KErrPThreadCreate;
       
   492 		}
       
   493 	else
       
   494 		{
       
   495 		//Wait for the completion of thread
       
   496 		TInt exitReason = 0;
       
   497 		TInt ret = pthread_join( threadID, (void**) &exitReason );
       
   498 		if( KErrNone != ret )
       
   499 			{
       
   500 			INFO_PRINTF2(_L("pthread_join returned with %d!!"), ret);
       
   501 			retVal = KErrPThreadJoin;
       
   502 			}
       
   503 		else if( exitReason != aExitReason )
       
   504 			{
       
   505 			INFO_PRINTF2(_L("unexpected return with pthread_exit-> %d!!"), exitReason);
       
   506 			retVal = KErrPThreadExit;
       
   507 			}
       
   508 		}
       
   509 
       
   510     if( KErrNone == retVal )
       
   511     	{
       
   512     	TBuf<20> apiName(KPThreadExit);
       
   513 		LogResult(EPositive, apiName);
       
   514     	}
       
   515 
       
   516     return retVal;
       
   517 	}
       
   518 	
       
   519 // -----------------------------------------------------------------------------
       
   520 // CTestPThread::ReadThreadAttributes
       
   521 // Description: Reads Thread Attributes from INI file
       
   522 // -----------------------------------------------------------------------------
       
   523 //
       
   524 void CTestPThread::ReadThreadAttributes(TInt& aThreadState,
       
   525         		TInt& aStackSize, TTestType& aTestType)
       
   526 	{
       
   527 	
       
   528 	_LIT( KParameter3, "Parameter3" );
       
   529 	TBool res = GetIntFromConfig(ConfigSection(), KParameter3, aThreadState);
       
   530 	
       
   531 	
       
   532 	_LIT( KParameter4, "Parameter4" );
       
   533 	res = GetIntFromConfig(ConfigSection(), KParameter4, aStackSize);
       
   534 	
       
   535 
       
   536 	if( PTHREAD_STACK_MIN > aStackSize ||
       
   537 	    ( PTHREAD_CREATE_JOINABLE != aThreadState &&
       
   538 	      PTHREAD_CREATE_DETACHED != aThreadState ) )
       
   539 		{
       
   540 		INFO_PRINTF1(_L("Invalid arguments from INI file!!"));
       
   541 		aTestType = ENegative;
       
   542 		}
       
   543 	}
       
   544 // -----------------------------------------------------------------------------
       
   545 // CTPThreadBasicApis::LogResult
       
   546 // Description: Logs the test api and test result
       
   547 // -----------------------------------------------------------------------------
       
   548 //
       
   549 inline void CTestPThread::LogResult(TTestType aTestType, TPtrC aBuf)
       
   550 	{
       
   551 	TBuf<20> testBuf( KPositive );
       
   552 	if( aTestType == ENegative )
       
   553 		{
       
   554 		testBuf.Copy( KNegative );
       
   555 		}
       
   556 
       
   557 	INFO_PRINTF3(_L("[%S] %S Test Passed"), &aBuf, &testBuf);
       
   558 	}
       
   559 
       
   560 
       
   561 // -----------------------------------------------------------------------------
       
   562 // CTPThreadBasicApis::TestMultiplePThread
       
   563 // Description: This function tests pthread_create and pthread_join API
       
   564 // Two threads will be created and if needed, main thread will wait for both
       
   565 // threads
       
   566 // returns KErrNone or error code depending on the test
       
   567 // -----------------------------------------------------------------------------
       
   568 //
       
   569 TInt CTestPThread::TestMultiplePThread()
       
   570 	{
       
   571 	int retVal = 0;
       
   572 	TInt threadState = PTHREAD_CREATE_DETACHED;
       
   573 	//Read Thread State from INI file
       
   574 	_LIT( KParameter1, "Parameter1" );
       
   575 	TBool res = GetIntFromConfig(ConfigSection(), KParameter1, threadState);
       
   576 	
       
   577 	TInt waitFlag = 0;
       
   578 	
       
   579 	_LIT( KParameter2, "Parameter2" );
       
   580 	res = GetIntFromConfig(ConfigSection(), KParameter2, waitFlag);
       
   581 	
       
   582 	pthread_t threadID1 = 0;
       
   583 	pthread_t threadID2 = 0;
       
   584 	pthread_attr_t threadAttr;
       
   585 	pthread_attr_init( &threadAttr );
       
   586 	pthread_attr_setdetachstate( &threadAttr, threadState );
       
   587 
       
   588 	//Create a thread now
       
   589 	retVal = pthread_create( &threadID1, &threadAttr, ThreadEntryPoint, (void*)0);
       
   590 	INFO_PRINTF2(_L("First pthread_create() returned with %d"), retVal);
       
   591 	if( retVal == 0)
       
   592 		{
       
   593 		//Create one more thread
       
   594 		retVal = pthread_create( &threadID2, &threadAttr, ThreadEntryPoint, (void*)0);
       
   595 		INFO_PRINTF2(_L("Second pthread_create() returned with %d"), retVal);
       
   596 		//If its joinable thread and waitFlag id set
       
   597 		if( KErrNone == retVal && PTHREAD_CREATE_JOINABLE == threadState &&
       
   598 			waitFlag )
       
   599 			{
       
   600 			int ret = pthread_join(threadID1, (void**)NULL);
       
   601 			INFO_PRINTF2(_L("First pthread_join() returned with %d"), ret);
       
   602 			}
       
   603 		if( KErrNone == retVal && PTHREAD_CREATE_JOINABLE == threadState &&
       
   604 			waitFlag )
       
   605 			{
       
   606 			int ret = pthread_join(threadID2, (void**)NULL);
       
   607 			INFO_PRINTF2(_L("Second pthread_join() returned with %d"), ret);
       
   608 			}
       
   609 		}
       
   610 
       
   611 	return retVal;
       
   612 	}
       
   613 
       
   614 // -----------------------------------------------------------------------------
       
   615 // CTPThreadBasicApis::SocketReadAndWritePThread
       
   616 // Description: This function tests pthread_create and pthread_join API
       
   617 // Two threads will be created and if needed, main thread will wait for both
       
   618 // threads
       
   619 // returns KErrNone or error code depending on the test
       
   620 // -----------------------------------------------------------------------------
       
   621 //
       
   622 TInt CTestPThread::SocketReadAndWritePThread( )
       
   623 	{
       
   624 	int retVal = 0;
       
   625 	TInt dataCount = KCount;
       
   626 	//Read Port Number from INI file
       
   627 	_LIT( KParameter1, "Parameter1" );
       
   628 	TBool res = GetIntFromConfig(ConfigSection(), KParameter1, dataCount);
       
   629 	
       
   630 	
       
   631     TThreadParam threadParam;
       
   632 
       
   633 	if( sem_init( &threadParam.iSemaphore, 0, 0 ) != KErrNone )
       
   634     	{
       
   635     	INFO_PRINTF2(_L("Semaphore creation Failed with %d"), errno );
       
   636     	return KErrGeneral;
       
   637     	}
       
   638 
       
   639 	pthread_t threadID1 = 0;
       
   640 	pthread_t threadID2 = 0;
       
   641 	pthread_attr_t threadAttr;
       
   642 	pthread_attr_init( &threadAttr );
       
   643 	pthread_attr_setdetachstate( &threadAttr, PTHREAD_CREATE_JOINABLE );
       
   644 
       
   645 	//Create a Read thread now
       
   646 	retVal = pthread_create( &threadID1, &threadAttr, SocketReadThreadEntryPoint,
       
   647 		(void*)&threadParam );
       
   648 	INFO_PRINTF2(_L("ReadThread Creation returned with %d"), retVal);
       
   649 	if( retVal == 0)
       
   650 		{
       
   651 		//Create Write thread
       
   652 		retVal = pthread_create( &threadID2, &threadAttr,
       
   653 			SocketWriteThreadEntryPoint, (void*)&threadParam );
       
   654 		INFO_PRINTF2(_L("WriteThread Creation returned with %d"), retVal);
       
   655 		//If its joinable thread and waitFlag id set
       
   656 		if( KErrNone == retVal )
       
   657 			{
       
   658 			TInt exitReason = 0;
       
   659 			retVal = pthread_join(threadID1, (void**)&exitReason );
       
   660 			INFO_PRINTF3(_L("ReadThread join returned with %d and the exit reason is %d"), retVal, exitReason );
       
   661 			retVal = pthread_join(threadID2, (void**)&exitReason );
       
   662 			INFO_PRINTF3(_L("WriteThread join returned with %d and the exit reason is %d"), retVal, exitReason );
       
   663 			}
       
   664 		}
       
   665 
       
   666 	if( sem_destroy( &threadParam.iSemaphore ) != KErrNone )
       
   667 		{
       
   668 		INFO_PRINTF2(_L("sem_destroy Failed with %d"), errno );
       
   669 		retVal = KErrGeneral;
       
   670 		}
       
   671 
       
   672 	return retVal;
       
   673 	}
       
   674 	
       
   675 	
       
   676 void* SocketReadThreadEntryPoint(void* aParam)
       
   677 	{
       
   678 	TInt retVal = KErrNone;
       
   679 	TInt count = 5;
       
   680     TThreadParam* pThreadParam = ( TThreadParam* ) aParam;
       
   681 
       
   682 	TInt portNum = KPortNum + count;
       
   683 	int serverFd = 0;
       
   684 	int newFd = 0;
       
   685 	size_t addrSize;
       
   686 	struct sockaddr_in servAddr;
       
   687 	struct sockaddr_in sockAddr;
       
   688 	const char* buff = "Server --> Sending Data Item : ";
       
   689 	char sendBuff[50];
       
   690 	char recvBuff[100];
       
   691 	const unsigned int maxRecv = 100;
       
   692 
       
   693 	serverFd = socket(AF_INET, SOCK_STREAM, 0);
       
   694 	if( serverFd == -1 )
       
   695 		{
       
   696 		retVal = KErrGeneral;
       
   697 		}
       
   698 
       
   699 	(&servAddr)->sin_addr.s_addr  = 0x0100007F;
       
   700 	(&servAddr)->sin_family = AF_INET;
       
   701 
       
   702 	servAddr.sin_port = htons( (TUint16) portNum );
       
   703 	retVal |= bind( serverFd, (struct sockaddr*)&servAddr, sizeof(servAddr) );
       
   704 	if( retVal != 0 )
       
   705 		{
       
   706 		close( serverFd );
       
   707 		retVal = KErrGeneral;
       
   708 		}
       
   709 
       
   710 	retVal |= listen( serverFd, 1 );
       
   711 	if( retVal != 0 )
       
   712 		{
       
   713 		close( serverFd );
       
   714 		retVal = KErrGeneral;
       
   715 		}
       
   716 
       
   717 	addrSize = sizeof( sockAddr );
       
   718 
       
   719 	//Tell the other thread that data is ready for reading
       
   720 	retVal = sem_post(&pThreadParam->iSemaphore);
       
   721 	if( retVal != 0 )
       
   722 		{
       
   723 		close( serverFd );
       
   724 		retVal = KErrGeneral;
       
   725 		}
       
   726 
       
   727 	newFd = accept( serverFd, (struct sockaddr*)&sockAddr, &addrSize);
       
   728 	if( newFd == -1 )
       
   729 		{
       
   730 		close( serverFd );
       
   731 		retVal = KErrGeneral;
       
   732 	    return (void*)retVal;
       
   733 		}
       
   734 
       
   735 	if( KErrNone == retVal )
       
   736 		{
       
   737 		//Now Receive and Send some Data
       
   738 		for(TUint i=1; i<=count; i++)
       
   739 			{
       
   740 			TInt ret = recv(newFd, recvBuff, maxRecv, 0);
       
   741 			if( ret == -1 )
       
   742 				{
       
   743 				close( newFd );
       
   744 				close( serverFd );
       
   745 				retVal = KErrGeneral;
       
   746 				break;
       
   747 				}
       
   748 			sprintf(sendBuff, "%s %d", buff, i);
       
   749 			ret = send(newFd, sendBuff, sizeof(sendBuff), 0);
       
   750 			if( ret == -1 )
       
   751 				{
       
   752 				close( newFd );
       
   753 				close( serverFd );
       
   754 				retVal = KErrGeneral;
       
   755 				break;
       
   756 				}
       
   757 			}
       
   758 		}
       
   759 
       
   760 	if( KErrNone == retVal )
       
   761 		{
       
   762 		close( newFd );
       
   763 		close( serverFd );
       
   764 		}
       
   765 	return (void*)retVal;
       
   766 	}
       
   767 
       
   768 void* SocketWriteThreadEntryPoint(void* aParam)
       
   769 	{
       
   770 	TInt retVal = KErrNone;
       
   771 	TInt count = 5;
       
   772     TThreadParam* pThreadParam = ( TThreadParam* ) aParam;
       
   773 
       
   774 	TInt portNum = KPortNum + count;
       
   775 	int clientFd;
       
   776 	size_t addrSize;
       
   777 	struct sockaddr_in servAddr;
       
   778 	const char* buff = "Client --> Sending Data Item : ";
       
   779 	char sendBuff[50];
       
   780 	char recvBuff[100];
       
   781 	const unsigned int maxRecv = 100;
       
   782 
       
   783 	(&servAddr)->sin_addr.s_addr  = 0x0100007F;
       
   784 	(&servAddr)->sin_family = AF_INET;
       
   785 
       
   786 	servAddr.sin_port = htons( portNum );
       
   787 
       
   788 	clientFd = socket(AF_INET, SOCK_STREAM, 0);
       
   789 	if( clientFd == -1 )
       
   790 		{
       
   791 		retVal = KErrGeneral;
       
   792 		}
       
   793 
       
   794     //Wait for the other thread to write some data to file
       
   795     retVal = sem_wait(&pThreadParam->iSemaphore);
       
   796     if( retVal != 0 )
       
   797     	{
       
   798     	close(clientFd);
       
   799     	retVal = KErrGeneral;
       
   800 	    return (void*)retVal;
       
   801     	}
       
   802 
       
   803 	addrSize = sizeof(servAddr);
       
   804 	retVal = connect(clientFd, (struct sockaddr*)&servAddr, addrSize);
       
   805 	//This should Pass
       
   806 	if( retVal == -1 )
       
   807 		{
       
   808 		close(clientFd);
       
   809 		retVal = KErrGeneral;
       
   810 		}
       
   811 
       
   812 	if( KErrNone == retVal )
       
   813 		{
       
   814 		//Now Send and Receive some Data
       
   815 		for(TUint i=1; i<=count; i++)
       
   816 			{
       
   817 			sprintf(sendBuff, "%s %d", buff, i);
       
   818 			TInt ret = send(clientFd, sendBuff, sizeof(sendBuff), 0);
       
   819 			if(ret == -1)
       
   820 				{
       
   821 				close( clientFd );
       
   822 				retVal = KErrGeneral;
       
   823 				break;
       
   824 				}
       
   825 			ret = recv(clientFd, recvBuff, maxRecv, 0);
       
   826 			if(ret == -1)
       
   827 				{
       
   828 				close( clientFd );
       
   829 				retVal = KErrGeneral;
       
   830 				break;
       
   831 				}
       
   832 			}
       
   833 		}
       
   834 
       
   835 	if (KErrNone == retVal )
       
   836 		{
       
   837 		close( clientFd );
       
   838 		}
       
   839 
       
   840 	return (void*)retVal;
       
   841 	}
       
   842 
       
   843 void* FileReadThreadEntryPoint( void* aParam )
       
   844 	{
       
   845 	TInt retVal = KErrNone;
       
   846 	char string[80];
       
   847 	int count = 0;
       
   848 	int index = 0;
       
   849 	TThreadParam* pThreadParam = ( TThreadParam* ) aParam;
       
   850 	//Open the File in read mode
       
   851 	FILE* fp = fopen( KFileName, "r");
       
   852 
       
   853 
       
   854 	if( fp == NULL )
       
   855 		{
       
   856 		retVal = KErrGeneral;
       
   857 		}
       
   858 	else
       
   859 		{
       
   860 		for( ;index < pThreadParam->iDataCount; index++ )
       
   861 			{
       
   862 			//Wait for the other thread to write some data to file
       
   863 			retVal = sem_wait(&pThreadParam->iSemaphore);
       
   864 			if( retVal != 0 )
       
   865 				{
       
   866 				retVal = KErrGeneral;
       
   867 				break;
       
   868 				}
       
   869 			fscanf(fp, "%s%d", string, &count);
       
   870 			}
       
   871 		}
       
   872 
       
   873 	fclose( fp );
       
   874 	return (void*)retVal;
       
   875 	}
       
   876 
       
   877 void* FileWriteThreadEntryPoint( void* aParam )
       
   878 	{
       
   879 	TInt retVal = KErrNone;
       
   880 	char string[80] = "Hello World... ";
       
   881 	int index = 0;
       
   882 	TThreadParam* pThreadParam = ( TThreadParam* ) aParam;
       
   883 	//Open the File in write mode
       
   884 	FILE* fp = fopen( KFileName, "w");
       
   885 
       
   886 	if( fp == NULL )
       
   887 		{
       
   888 		retVal = KErrGeneral;
       
   889 		}
       
   890 	else
       
   891 		{
       
   892 		for( ;index < pThreadParam->iDataCount; index++ )
       
   893 			{
       
   894 			fprintf(fp, "%s%d", string, index);
       
   895 			//Tell the other thread that data is ready for reading
       
   896 			retVal = sem_post(&pThreadParam->iSemaphore);
       
   897 			if( retVal != 0 )
       
   898 				{
       
   899 				retVal = KErrGeneral;
       
   900 				break;
       
   901 				}
       
   902 			}
       
   903 		}
       
   904 
       
   905     fclose( fp );
       
   906 	return (void*)retVal;
       
   907 	}
       
   908 //General functions 
       
   909 void* threadFun(void *)
       
   910 	{
       
   911 	TInt* val = 0;
       
   912 
       
   913 	pthread_t selfID = pthread_self();
       
   914 	for(int i=0;i < 10;i++)
       
   915 		{
       
   916 		printf("The index i -> %d and Thread Id -> %d\n",i,selfID);
       
   917 		}
       
   918 	return (void*)val;
       
   919 	}
       
   920 	
       
   921 
       
   922 void* threadwrite(void *)
       
   923 	{
       
   924 	TInt* val = 0;
       
   925 
       
   926 	FILE *fp;
       
   927 	char buf[] = {"This is a test for 2 thread writing to same file"};
       
   928 	
       
   929 	pthread_t selfID = pthread_self();
       
   930 
       
   931 	fp=fopen("c:\\test1.txt","w");
       
   932 	if(fp==NULL)
       
   933 		{
       
   934 		printf("\nFIle creation failed");
       
   935 		return (void*)val;
       
   936 		}
       
   937 	fprintf(fp,"%s",buf);
       
   938 	fclose(fp);
       
   939 	return (void*)val;
       
   940 	}
       
   941 
       
   942 void* threadappend(void *)
       
   943 	{
       
   944 	TInt* val = 0;
       
   945 
       
   946 	FILE *fp;
       
   947 	char buf[] = {"This is a test for 2 thread appending to same file"};
       
   948 	
       
   949 	pthread_t selfID = pthread_self();
       
   950 
       
   951 	fp=fopen("c:\\testapp1.txt","a");
       
   952 	if(fp==NULL)
       
   953 		{
       
   954 		printf("\nFIle creation failed");
       
   955 		return (void*)val;
       
   956 		}
       
   957 	fprintf(fp,"%s",buf);
       
   958 	fclose(fp);
       
   959 	return (void*)val;
       
   960 	}
       
   961 	
       
   962 void* threadread(void *)
       
   963 	{
       
   964 	TInt* val = 0;
       
   965 
       
   966 	char a; 
       
   967 	FILE *fp;
       
   968 	
       
   969 	pthread_t selfID = pthread_self();
       
   970 
       
   971 	fp=fopen("c:\\test1.txt","r");
       
   972 	if(fp==NULL)
       
   973 		{ 
       
   974 		printf("\nFIle opening failed");
       
   975 		return (void*)val;
       
   976 		}
       
   977 	while(fread(&a, sizeof(char), 1, fp), !feof(fp) && !ferror(fp)) 
       
   978 		{
       
   979         printf("I read %c\t", a);
       
   980 	    }
       
   981 	return (void*)val;
       
   982 	}
       
   983 
       
   984 
       
   985 void* ThreadEntryPoint(void* aParam)
       
   986 	{
       
   987 	TInt retVal = KErrNone;
       
   988 	//Get the thread id
       
   989 	pthread_t selfID = pthread_self();
       
   990 	//Make sure that, its valid
       
   991 	if( selfID == 0 )
       
   992 		{
       
   993 		//Exit the thread with the reason
       
   994 		retVal = KErrPThreadSelf;
       
   995 		pthread_exit( (void*) retVal );
       
   996 		}
       
   997 	//Try to join with the same thread
       
   998 	else
       
   999 		{
       
  1000 		int ret = pthread_join( selfID, (void**)NULL );
       
  1001 		if( ret!= EDEADLK )
       
  1002 			{
       
  1003 			//Exit the thread with the reason
       
  1004 			retVal = KErrPThreadJoin;
       
  1005 			pthread_exit( (void*) retVal );
       
  1006 			}
       
  1007 		}
       
  1008 
       
  1009 	//If sent Parameter is not 0, then exit the thread
       
  1010 	if( (TInt)aParam != KErrNone )
       
  1011 		{
       
  1012 		//Exit the thread with the reason
       
  1013 		pthread_exit( aParam );
       
  1014 		}
       
  1015 
       
  1016 	return (void*)retVal;
       
  1017 	}
       
  1018 
       
  1019 // -----------------------------------------------------------------------------
       
  1020 // CTPThreadBasicApis::FileReadAndWritePThread
       
  1021 // Description: This function tests pthread_create and pthread_join API
       
  1022 // Two threads will be created and if needed, main thread will wait for both
       
  1023 // threads
       
  1024 // returns KErrNone or error code depending on the test
       
  1025 // -----------------------------------------------------------------------------
       
  1026 //
       
  1027 TInt CTestPThread::FileReadAndWritePThread( )
       
  1028 	{
       
  1029 	int retVal = 0;
       
  1030 	TInt dataCount = KCount;
       
  1031 	//Read Port Number from INI file
       
  1032 	_LIT( KParameter1, "Parameter1" );
       
  1033 	TBool res = GetIntFromConfig(ConfigSection(), KParameter1, dataCount);
       
  1034 	
       
  1035 	
       
  1036 	TThreadParam threadParam;
       
  1037 	threadParam.iDataCount = dataCount;
       
  1038 
       
  1039 	if( sem_init( &threadParam.iSemaphore, 0, 0 ) != KErrNone )
       
  1040 		{
       
  1041 		INFO_PRINTF2(_L("Semaphore creation Failed with %d"), errno );
       
  1042 		return KErrGeneral;
       
  1043 		}
       
  1044 
       
  1045 	pthread_t threadID1 = 0;
       
  1046 	pthread_t threadID2 = 0;
       
  1047 	pthread_attr_t threadAttr;
       
  1048 	pthread_attr_init( &threadAttr );
       
  1049 	pthread_attr_setdetachstate( &threadAttr, PTHREAD_CREATE_JOINABLE );
       
  1050 
       
  1051 	//Create a Read thread now
       
  1052 	retVal = pthread_create( &threadID1, &threadAttr, FileReadThreadEntryPoint,
       
  1053 		(void*)&threadParam );
       
  1054 	INFO_PRINTF2(_L("ReadThread Creation returned with %d"), retVal);
       
  1055 	if( retVal == 0)
       
  1056 		{
       
  1057 		//Create Write thread
       
  1058 		retVal = pthread_create( &threadID2, &threadAttr,
       
  1059 			FileWriteThreadEntryPoint, (void*)&threadParam );
       
  1060 		INFO_PRINTF2(_L("WriteThread Creation returned with %d"), retVal);
       
  1061 		//If its joinable thread and waitFlag id set
       
  1062 		if( KErrNone == retVal )
       
  1063 			{
       
  1064 			TInt exitReason = 0;
       
  1065 			retVal = pthread_join(threadID1, (void**)&exitReason );
       
  1066 			INFO_PRINTF3(_L("ReadThread join returned with %d and the exit reason is %d"), retVal, exitReason );
       
  1067 			retVal = pthread_join(threadID2, (void**)&exitReason );
       
  1068 			INFO_PRINTF3(_L("WriteThread join returned with %d and the exit reason is %d"), retVal, exitReason );
       
  1069 			}
       
  1070 		}
       
  1071 
       
  1072 	if( sem_destroy( &threadParam.iSemaphore ) != KErrNone )
       
  1073 		{
       
  1074 		INFO_PRINTF2(_L("sem_destroy Failed with %d"), errno );
       
  1075 		retVal = KErrGeneral;
       
  1076 		}
       
  1077 
       
  1078 	return retVal;
       
  1079 	}
       
  1080 
       
  1081 
       
  1082 // -----------------------------------------------------------------------------
       
  1083 // PThreadSchedTest::PThreadSchedTest
       
  1084 // Description: This function does most of the pthread scheduling attribute set/get  api testing
       
  1085 // This test function can be called with below syntax from INI file:
       
  1086 // PThreadSchedTest <API> <TestType> [<State> <StackSize>]
       
  1087 // API is an integer which can be any of the below:
       
  1088 // 		PTHREAD_ATTR_SETSCHEDPARAM(6)
       
  1089 //		PTHREAD_ATTR_GETSCHEDPARAM(7)
       
  1090 //		PTHREAD_ATTR_SETSCHEDPOLICY(8)
       
  1091 //		PTHREAD_ATTR_GETSCHEDPOLICY(9)
       
  1092 //		PTHREAD_SETSCHEDPARAM(10)
       
  1093 //		PTHREAD_GETSCHEDPARAM(11)
       
  1094 
       
  1095 // TestType is an integer which can be any of the below:
       
  1096 // 		POSITIVE_TEST (1)
       
  1097 // 		NEGATIVE_TEST (2)
       
  1098 // State is an integer which can be any of the below:
       
  1099 // 		JOIN_THREAD (0)
       
  1100 // 		DETACH_THREAD (1)
       
  1101 // StackSize is the size of the stack whic is an integer
       
  1102 // -----------------------------------------------------------------------------
       
  1103 TInt CTestPThread::PThreadSchedTest(  )
       
  1104     {
       
  1105     
       
  1106     INFO_PRINTF1(_L("In TPThreadSchedApis"));
       
  1107 
       
  1108     TInt retVal = KErrNone;
       
  1109     TInt apiType = 1;
       
  1110     _LIT( KParameter1, "Parameter1");
       
  1111 	TBool res = GetIntFromConfig(ConfigSection(), KParameter1, apiType );
       
  1112 	
       
  1113     TInt testType = 1;
       
  1114     _LIT( KParameter2, "Parameter2" );
       
  1115 	res = GetIntFromConfig(ConfigSection(), KParameter2, testType);
       
  1116 	
       
  1117    
       
  1118     switch(apiType)
       
  1119     	{
       
  1120     	case EPThreadAttrSetSchedParam:
       
  1121     		{
       
  1122     		retVal = PThreadAttrSetSchedParmAPITest();
       
  1123     		break;
       
  1124     		}
       
  1125     	case EPThreadAttrGetSchedParam:
       
  1126     	    {
       
  1127     	    retVal = PThreadAttrGetSchedParmAPITest((TTestType)testType);
       
  1128     	    break;
       
  1129     	    }
       
  1130         case EPThreadAttrSetSchedPolicy:
       
  1131             {
       
  1132         	retVal = PThreadAttrSetSchedPolicyAPITest();
       
  1133     	    break;
       
  1134             }
       
  1135         case EPThreadAttrGetSchedPolicy:
       
  1136             {
       
  1137             retVal = PThreadAttrGetSchedPolicyAPITest(/*(TTestType)testType*/);
       
  1138     	    break;
       
  1139             }
       
  1140         case EPThreadSetSchedparam:
       
  1141             {
       
  1142             retVal = PThreadSetSchedParamAPITest((TTestType)testType);
       
  1143             break;
       
  1144             }
       
  1145         case EPThreadGetSchedparam:
       
  1146             {
       
  1147             retVal = PThreadGetSchedParamAPITest((TTestType)testType);
       
  1148             break;
       
  1149             }
       
  1150     	case EPThreadAttrSetScope:
       
  1151 	    	{
       
  1152 	    	retVal = PThreadAttrSetScopeAPITest();
       
  1153 	    	break;
       
  1154 	    	}
       
  1155 	    case EPThreadAttrGetScope1:
       
  1156 	    	{
       
  1157 	    	retVal = PThreadAttrGetScopeAPITest1();
       
  1158 	    	break;
       
  1159 	    	}
       
  1160 	    case EPThreadAttrGetScope2:
       
  1161 	    	{
       
  1162 	    	retVal = PThreadAttrGetScopeAPITest2();
       
  1163 	    	break;
       
  1164 	    	}
       
  1165 	    case EPThreadKeyCreate1:
       
  1166 	    	{
       
  1167 	    	retVal = PThreadKeyCreateAPITest1();
       
  1168 	    	break;
       
  1169 	    	}
       
  1170     	case  EPThreadKeyCreate2:
       
  1171     	    {
       
  1172 	    	retVal = PThreadKeyCreateAPITest2();
       
  1173 	    	break;
       
  1174 	    	}
       
  1175 
       
  1176 	    case EPThreadKeyDelete:
       
  1177 	    	{
       
  1178 	    	break;
       
  1179 	    	}
       
  1180     	case EPThreadSetSpecific:
       
  1181 	    	{
       
  1182 	    	retVal = PThreadSetSpecificAPITest();
       
  1183 	    	break;
       
  1184 	    	}
       
  1185     	case EPThreadGetSpecific:
       
  1186 	    	{
       
  1187 	    	retVal = PThreadGetSpecificAPITest();
       
  1188 	    	break;
       
  1189 	    	}
       
  1190 
       
  1191     	default:
       
  1192     		{
       
  1193     		INFO_PRINTF2(_L("Invalid arguments from INI file : %d"), apiType);
       
  1194     		retVal = KErrArgument;
       
  1195     		break;
       
  1196     		}
       
  1197     	}
       
  1198 
       
  1199     return retVal;
       
  1200 
       
  1201     }
       
  1202 
       
  1203 TInt CTestPThread::PThreadAttrSetSchedParmAPITest()
       
  1204 	{
       
  1205     TInt retVal = KErrNone;
       
  1206     pthread_attr_t at;
       
  1207 
       
  1208     if (pthread_attr_init (&at) != 0)
       
  1209         {
       
  1210         INFO_PRINTF1(_L("attr_init failed"));
       
  1211         retVal = KErrPThreadAttrGetSchedParam;
       
  1212         }
       
  1213     struct sched_param pa;
       
  1214  
       
  1215     pa.sched_priority= 100;
       
  1216     if (pthread_attr_setschedparam (&at, &pa) != 0)
       
  1217         {
       
  1218         INFO_PRINTF1(_L("attr_setschedparam failed"));
       
  1219         retVal = KErrPThreadAttrGetSchedParam;
       
  1220         }
       
  1221     else
       
  1222         {
       
  1223         TBuf<100> apiName(KPThreadSetSchedParam);
       
  1224 	    LogResult(EPositive, apiName);
       
  1225         }
       
  1226     return retVal;
       
  1227     }
       
  1228 
       
  1229 TInt CTestPThread::PThreadAttrGetSchedParmAPITest(TTestType aTestType)
       
  1230     {
       
  1231 	TInt retVal = KErrNone;
       
  1232 	pthread_attr_t at,at_test;
       
  1233 
       
  1234     if (pthread_attr_init (&at) != 0)
       
  1235         {
       
  1236         INFO_PRINTF1(_L("attr_init failed"));
       
  1237         retVal = KErrPThreadAttrGetSchedParam;
       
  1238         }
       
  1239     struct sched_param pa_first,pa_second;
       
  1240     pa_first.sched_priority = 100;
       
  1241     if (pthread_attr_setschedparam (&at, &pa_first) != 0)
       
  1242         {
       
  1243         INFO_PRINTF1(_L("attr_setschedparam failed"));
       
  1244         retVal = KErrPThreadAttrGetSchedParam;
       
  1245         }
       
  1246     if( aTestType == EPositive )
       
  1247 		{
       
  1248 		if(pthread_attr_getschedparam(&at,&pa_second) !=0)
       
  1249    	        {
       
  1250    	        INFO_PRINTF1(_L("attr_getschedparam failed"));
       
  1251             retVal = KErrPThreadAttrGetSchedParam;
       
  1252             }
       
  1253             if(pa_second.sched_priority != pa_first.sched_priority)
       
  1254    	        {
       
  1255    	        INFO_PRINTF1(_L("get_attrib and set_attrib mismatched"));
       
  1256    	        retVal = KErrPThreadAttrGetSchedParam;
       
  1257            }
       
  1258 		}
       
  1259 	else if(aTestType == ENegative)
       
  1260 	    {
       
  1261 	    struct sched_param pa_third;
       
  1262         pa_third.sched_priority=100;
       
  1263         if (pthread_attr_setschedparam (&at_test, &pa_third) != 0)
       
  1264             {
       
  1265             ERR_PRINTF1(_L("attr_setschedparam failed"));
       
  1266             retVal = KErrPThreadAttrGetSchedParam;
       
  1267             }
       
  1268         if(pthread_attr_getschedparam(&at,&pa_second) !=0)
       
  1269    	        {
       
  1270    	        ERR_PRINTF1(_L("attr_getschedparam failed"));
       
  1271             retVal = KErrPThreadAttrGetSchedParam;
       
  1272             }
       
  1273             if(pa_second.sched_priority == pa_third.sched_priority)
       
  1274    	        {
       
  1275    	        ERR_PRINTF1(_L("get_attrib and set_attrib mismatched"));
       
  1276    	        retVal = KErrPThreadAttrGetSchedParam;
       
  1277             }
       
  1278 	    }
       
  1279 	if(retVal !=KErrPThreadAttrGetSchedParam )
       
  1280        {
       
  1281        TBuf<100> apiName(KPThreadGetSchedParam);
       
  1282 	   LogResult(EPositive, apiName);
       
  1283        }
       
  1284 	return retVal;
       
  1285 	}
       
  1286    //API to test the setting of the scheduling policy.
       
  1287 
       
  1288 TInt CTestPThread::PThreadAttrSetSchedPolicyAPITest()
       
  1289     {
       
  1290    	TInt retVal =  KErrNone;
       
  1291    	long int r = rand();
       
  1292    	pthread_attr_t a;
       
  1293    	int s;
       
  1294    	if (r != SCHED_RR)
       
  1295 	    {
       
  1296 	    int e = pthread_attr_setschedpolicy (&a, r);
       
  1297 
       
  1298 	    if (e == 0)
       
  1299 	        {
       
  1300 	        ERR_PRINTF1(_L("API for setting the scheduling policy failed\n"));
       
  1301 	        retVal = KErrPThreadAttrSetSchedPolicy;
       
  1302 	        }
       
  1303 	    if (e != EINVAL)
       
  1304 	        {
       
  1305 	        ERR_PRINTF1(_L("API for setting the scheduling policy failed\n"));
       
  1306 	        retVal = KErrPThreadAttrSetSchedPolicy;
       
  1307 	        }
       
  1308 	    }
       
  1309 
       
  1310     int   e = pthread_attr_setschedpolicy (&a, SCHED_RR);
       
  1311     if (e != 0)
       
  1312         {
       
  1313         ERR_PRINTF1(_L("API for setting the scheduling policy failed\n"));
       
  1314         retVal = KErrPThreadAttrSetSchedPolicy;
       
  1315         }
       
  1316     if (pthread_attr_getschedpolicy (&a, &s) != 0)
       
  1317         {
       
  1318         ERR_PRINTF1(_L("API for setting the scheduling policy failed\n"));
       
  1319         retVal = KErrPThreadAttrSetSchedPolicy;
       
  1320         }
       
  1321     if (s != SCHED_RR)
       
  1322         {
       
  1323         ERR_PRINTF1(_L("schedpolicy could not set to SCHED_RR"));
       
  1324         retVal = KErrPThreadAttrSetSchedPolicy;
       
  1325         }
       
  1326     if(retVal == KErrNone)
       
  1327         {
       
  1328         TBuf<100> apiName(KPThreadSetSchedPolicy);
       
  1329     	LogResult(EPositive, apiName);
       
  1330         }
       
  1331    	return retVal;
       
  1332 	}
       
  1333 //Testing for the API pthread_attr_getschedpolicy: Date 01/03/06
       
  1334 TInt CTestPThread::PThreadAttrGetSchedPolicyAPITest(/*TTestType aTestType*/)
       
  1335     {
       
  1336 	TInt retVal = KErrNone;
       
  1337 	pthread_attr_t a;
       
  1338 	int s,e;
       
  1339 	e = pthread_attr_setschedpolicy (&a, SCHED_RR);
       
  1340 	if (e != 0)
       
  1341         {
       
  1342         ERR_PRINTF1(_L("API for setting the scheduling policy failed\n"));
       
  1343         retVal = KErrPThreadAttrSetSchedPolicy;
       
  1344         }
       
  1345     if (pthread_attr_getschedpolicy (&a, &s) != 0)
       
  1346         {
       
  1347         ERR_PRINTF1(_L("1st attr_getschedpolicy failed"));
       
  1348         retVal= KErrPThreadAttrGetSchedPolicy;
       
  1349         }
       
  1350     /* XXX What is the correct default value.  */
       
  1351     if (s != SCHED_RR)
       
  1352         {
       
  1353         ERR_PRINTF1(_L("incorrect default value for schedpolicy"));
       
  1354         retVal= KErrPThreadAttrGetSchedPolicy;
       
  1355         }
       
  1356     if(retVal == KErrNone)
       
  1357         {
       
  1358     	TBuf<100> apiName(KPThreadGetSchedPolicy);
       
  1359     	LogResult(EPositive, apiName);
       
  1360         }
       
  1361 	return retVal;
       
  1362     }
       
  1363 TInt CTestPThread::PThreadSetSchedParamAPITest(TTestType aTestType)
       
  1364 	{
       
  1365 	TInt retVal = KErrNone;
       
  1366 	int e ;
       
  1367 	struct sched_param param;
       
  1368 	pthread_t thread=100; // Thread which will be assigned the scheduling priority and the policy.
       
  1369 	if(aTestType == EPositive)
       
  1370 		{
       
  1371 		pthread_t thread= pthread_self();
       
  1372 		param.sched_priority = 100; 
       
  1373 		
       
  1374 		e = pthread_setschedparam(thread,SCHED_RR, &param);
       
  1375 		if(e != 0)
       
  1376 			{
       
  1377 			ERR_PRINTF1(_L("API for setting the scheduling policy and priority failed."));
       
  1378 			retVal = KErrPThreadSetSchedParam;
       
  1379 			}
       
  1380 		if(e == EINVAL)
       
  1381 			{
       
  1382 			ERR_PRINTF1(_L("policy or param are invalid settings."));
       
  1383 			retVal = KErrPThreadSetSchedParam;
       
  1384 			}
       
  1385 		else if(e == EFAULT)
       
  1386 			{
       
  1387 			ERR_PRINTF1(_L("param is an invalid pointer."));
       
  1388 			retVal = KErrPThreadSetSchedParam;
       
  1389 			}
       
  1390 		else if(e == ENOTSUP)
       
  1391 			{
       
  1392 			ERR_PRINTF1(_L("policy specifies an unsupported setting."));
       
  1393 			retVal = KErrPThreadSetSchedParam;
       
  1394 			}
       
  1395 		else if(e == ESRCH)
       
  1396 			{
       
  1397 			ERR_PRINTF1(_L("thread does not specify a currently running thread in the process."));
       
  1398 			retVal = KErrPThreadSetSchedParam;
       
  1399 			}
       
  1400 		}
       
  1401 	else if(aTestType == ENegative)
       
  1402 		{
       
  1403 		e = pthread_setschedparam(thread,SCHED_RR, &param);
       
  1404 		if(e == 0)
       
  1405 			{
       
  1406 			ERR_PRINTF1(_L("API for Negatively testing the pthread_setschedparam failed.."));
       
  1407 			retVal = KErrPThreadSetSchedParam;
       
  1408 			}
       
  1409 		if(e != EINVAL && e != EFAULT  && e == ENOTSUP && e != ESRCH )
       
  1410 			{
       
  1411 			ERR_PRINTF1(_L("Negative API test for the pthread_setschedparam failed."));
       
  1412 			retVal = KErrPThreadSetSchedParam;
       
  1413 			}
       
  1414 	    if(retVal == KErrNone)
       
  1415 			{
       
  1416 			TBuf<100> apiName(KPThreadSetSchdPrm);
       
  1417 			LogResult(EPositive, apiName);
       
  1418 			}
       
  1419 		}
       
  1420 	return retVal;
       
  1421 	}
       
  1422 
       
  1423 TInt CTestPThread::PThreadGetSchedParamAPITest(TTestType aTestType)
       
  1424 	{
       
  1425 	TInt retVal = KErrNone;
       
  1426 	int e ;
       
  1427 	struct sched_param param;
       
  1428 	int policy;
       
  1429 	pthread_t thread=100;
       
  1430 	if(aTestType == EPositive)
       
  1431 		{
       
  1432 		thread = pthread_self(); // Thread which will be assigned the scheduling priority and the policy.
       
  1433 		e = pthread_getschedparam(thread,&policy,&param);
       
  1434 		if (e == EINVAL)
       
  1435 			{
       
  1436 			ERR_PRINTF1(_L("The locations pointed to by policy or param point are not writeable."));
       
  1437 			retVal = KErrPThreadSetSchedParam;
       
  1438 			}
       
  1439 		else if(e == EFAULT)
       
  1440 			{
       
  1441 			ERR_PRINTF1(_L("policy or param is an invalid pointer."));
       
  1442 			retVal = KErrPThreadSetSchedParam;
       
  1443 			}
       
  1444 		else if (e == ESRCH)
       
  1445 			{
       
  1446 			ERR_PRINTF1(_L("thread does not specify a currently running thread in the process."));
       
  1447 			retVal = KErrPThreadSetSchedParam;
       
  1448 			}
       
  1449 		}
       
  1450 	else if(aTestType == ENegative)
       
  1451 		{
       
  1452 		e = pthread_getschedparam(thread,&policy,&param);
       
  1453 		if (e != EINVAL && e != EFAULT && e != ESRCH  )
       
  1454 			{
       
  1455 			ERR_PRINTF1(_L("Negative test for API pthread_getschedparam failed ."));
       
  1456 			retVal = KErrPThreadSetSchedParam;
       
  1457 			}
       
  1458 		}
       
  1459     if(retVal == KErrNone)
       
  1460 	    {
       
  1461 	    TBuf<100> apiName(KPThreadGetSchdPrm);
       
  1462 	    LogResult(EPositive, apiName);
       
  1463 	    }
       
  1464 
       
  1465 	return retVal;
       
  1466 }
       
  1467 
       
  1468 //Stand alone test cases exists , added to STIF framework
       
  1469 TInt CTestPThread::PThreadAttrSetScopeAPITest()
       
  1470 	{
       
  1471 	TInt retVal = KErrNone;
       
  1472 	pthread_attr_t a;
       
  1473     int e;
       
  1474     int i;
       
  1475     int s;
       
  1476     long int r;
       
  1477 	if (pthread_attr_init (&a) != 0)
       
  1478 	    {
       
  1479 	    ERR_PRINTF1(_L("attr_init failed\n"));
       
  1480 	    retVal = KErrPThreadAttrSetScope;
       
  1481 	    }
       
  1482 
       
  1483     for (i = 0; i < 100; ++i)
       
  1484 		{
       
  1485         r = rand();
       
  1486 
       
  1487         if (r != PTHREAD_SCOPE_SYSTEM)
       
  1488 			{
       
  1489             e = pthread_attr_setscope (&a, r);
       
  1490             if (e != EINVAL)
       
  1491 	            {
       
  1492 	            ERR_PRINTF1(_L("attr_setscope didn't return EINVAL\n"));
       
  1493 	            retVal = KErrPThreadAttrSetScope;
       
  1494 	            }
       
  1495 
       
  1496             if (pthread_attr_getscope(&a, &s) != 0)
       
  1497 	            {
       
  1498 	            ERR_PRINTF1(_L("attr_getscope failed.\n"));
       
  1499 	            retVal = KErrPThreadAttrSetScope;
       
  1500 	            }
       
  1501 
       
  1502             if (s != PTHREAD_SCOPE_SYSTEM)
       
  1503 	            {
       
  1504 	            ERR_PRINTF1(_L("contentionscope changed by invalid setscope call.\n"));
       
  1505 	            retVal = KErrPThreadAttrSetScope;
       
  1506 	            }
       
  1507 			}
       
  1508 		}
       
  1509     if(retVal == KErrNone)
       
  1510 	    {
       
  1511 	    TBuf<100> apiName(KPThreadSetScope);
       
  1512 	    LogResult(EPositive, apiName);
       
  1513 	    }
       
  1514 	return retVal;
       
  1515 	}
       
  1516 TInt CTestPThread::PThreadAttrGetScopeAPITest1()
       
  1517 	{
       
  1518 	TInt retVal = KErrNone;
       
  1519 	pthread_attr_t a;
       
  1520     int e;
       
  1521     int s;
       
  1522     if (pthread_attr_init (&a) != 0)
       
  1523 	    {
       
  1524 	    ERR_PRINTF1(_L("attr_init failed.\n"));
       
  1525 	    retVal = KErrPThreadAttrGetScope;
       
  1526 	    }
       
  1527 
       
  1528     if (pthread_attr_getscope (&a, &s) != 0)
       
  1529 	    {
       
  1530 	    ERR_PRINTF1(_L("1st attr_getscope failed"));
       
  1531 	    retVal = KErrPThreadAttrGetScope;
       
  1532 	    }
       
  1533     /* correct default value.  */
       
  1534     if (s != PTHREAD_SCOPE_SYSTEM)
       
  1535 	    {
       
  1536 	    ERR_PRINTF1(_L("incorrect default value for contentionscope.\n"));
       
  1537 	    retVal = KErrPThreadAttrGetScope;
       
  1538 	    }
       
  1539 
       
  1540     e = pthread_attr_setscope (&a, PTHREAD_SCOPE_SYSTEM);
       
  1541     if (e != ENOTSUP)
       
  1542 	    {
       
  1543 	        if (e != 0)
       
  1544 		        {
       
  1545 		        ERR_PRINTF1(_L("1st attr_setscope failed."));
       
  1546 		        retVal = KErrPThreadAttrGetScope;
       
  1547 		        }
       
  1548 	        if (pthread_attr_getscope (&a, &s) != 0)
       
  1549 		        {
       
  1550 		        ERR_PRINTF1(_L("2nd attr_getscope failed."));
       
  1551 		        retVal = KErrPThreadAttrGetScope;
       
  1552 		        }
       
  1553 	        if (s != PTHREAD_SCOPE_SYSTEM)
       
  1554 		        {
       
  1555 		        ERR_PRINTF1(_L("\
       
  1556 		        contentionscope could not set to PTHREAD_SCOPE_SYSTEM.\n"));
       
  1557 		        retVal = KErrPThreadAttrGetScope;
       
  1558 		        }
       
  1559 	    }
       
  1560 
       
  1561     e = pthread_attr_setscope (&a, PTHREAD_SCOPE_SYSTEM);
       
  1562     if (e != 0)
       
  1563 	    {
       
  1564 	    ERR_PRINTF1(_L("2nd attr_setscope failed."));
       
  1565 	    retVal = KErrPThreadAttrGetScope;
       
  1566 	    }
       
  1567     if (pthread_attr_getscope (&a, &s) != 0)
       
  1568 	    {
       
  1569 	    ERR_PRINTF1(_L("3rd attr_getscope failed."));
       
  1570 	    retVal = KErrPThreadAttrGetScope;
       
  1571 	    }
       
  1572     if (s != PTHREAD_SCOPE_SYSTEM)
       
  1573 	    {
       
  1574 	    ERR_PRINTF1(_L("contentionscope could not set to PTHREAD_SCOPE_SYSTEM.\n"));
       
  1575 	    retVal = KErrPThreadAttrGetScope;
       
  1576 	    }
       
  1577     if(retVal == KErrNone)
       
  1578 	    {
       
  1579 	    TBuf<100> apiName(KPThreadGetScope);
       
  1580 	    LogResult(EPositive , apiName);
       
  1581 	    }
       
  1582 	//All test cases passed...
       
  1583 
       
  1584 	return retVal;
       
  1585 	}
       
  1586 
       
  1587 TInt CTestPThread::PThreadAttrGetScopeAPITest2()
       
  1588 	{
       
  1589 	TInt retVal = KErrNone;
       
  1590 	pthread_attr_t a;
       
  1591     int s1;
       
  1592     int s2;
       
  1593 
       
  1594     if (pthread_attr_init (&a) != 0)
       
  1595 	    {
       
  1596 	    ERR_PRINTF1(_L("attr_init failed.\n"));
       
  1597 	    retVal = KErrPThreadAttrGetScope;
       
  1598 	    }
       
  1599 
       
  1600     if (pthread_attr_getscope (&a, &s1) != 0)
       
  1601 	    {
       
  1602 	    ERR_PRINTF1(_L("1st attr_getscope failed."));
       
  1603 	    retVal = KErrPThreadAttrGetScope;
       
  1604 	    }
       
  1605     if (pthread_attr_getscope (&a, &s2) != 0)
       
  1606 	    {
       
  1607 	    ERR_PRINTF1(_L("2nd attr_getscope failed."));
       
  1608 	    retVal = KErrPThreadAttrGetScope;
       
  1609 	    }
       
  1610     if (s2 != s1)
       
  1611 	    {
       
  1612 	    ERR_PRINTF1(_L("Both scopes are not equal: failed."));
       
  1613 	    retVal = KErrPThreadAttrGetScope;
       
  1614 	    }
       
  1615 
       
  1616     /* correct default value.  */
       
  1617      if(s1 != PTHREAD_SCOPE_SYSTEM)
       
  1618 	    {
       
  1619 	    ERR_PRINTF1(_L("incorrect default value for contentionscope\n"));
       
  1620 	    retVal = KErrPThreadAttrGetScope;
       
  1621 	    }
       
  1622      if(retVal == KErrNone)
       
  1623 	    {
       
  1624 	    TBuf<100> apiName(KPThreadGetScope);
       
  1625 	    LogResult(EPositive , apiName);
       
  1626 	    }
       
  1627 	//All Test cases passed...
       
  1628 
       
  1629     return retVal;
       
  1630 	}
       
  1631 
       
  1632 TInt CTestPThread::PThreadKeyCreateAPITest1()
       
  1633 	{
       
  1634     TInt retVal = KErrNone;
       
  1635     int max;
       
  1636     int i;
       
  1637     pthread_key_t *keys;
       
  1638 
       
  1639 	#ifdef PTHREAD_KEYS_MAX
       
  1640 		max = PTHREAD_KEYS_MAX;
       
  1641 	#else
       
  1642 		max = _POSIX_THREAD_KEYS_MAX;
       
  1643 	#endif
       
  1644 
       
  1645     keys = (pthread_key_t *)malloc (max * sizeof (pthread_key_t));
       
  1646 
       
  1647     for (i = 0; i < max; ++i)
       
  1648 		{
       
  1649         if (pthread_key_create (&keys[i], NULL) != 0)
       
  1650 			{
       
  1651             ERR_PRINTF1(_L("key_create failed  \n"));
       
  1652             retVal = KErrPThreadKeyCreate;
       
  1653 			}
       
  1654         else
       
  1655 			{	
       
  1656             if (pthread_setspecific (keys[i], (const void *) (i + 100l)) != 0)
       
  1657 				{
       
  1658                 ERR_PRINTF1(_L("setspecific failed\n"));
       
  1659                 retVal = KErrPThreadSetSpecific;
       
  1660 
       
  1661 				}
       
  1662 			}
       
  1663 		}
       
  1664 
       
  1665     INFO_PRINTF1(_L("All keys are created and value set successfully\n"));
       
  1666 
       
  1667     for (i = 0; i < max; ++i)
       
  1668 		{
       
  1669         if (pthread_getspecific (keys[i]) != (void *) (i + 100l))
       
  1670 			{
       
  1671             ERR_PRINTF1(_L("getspecific failed \n"));
       
  1672             retVal = KErrPThreadGetSpecific;
       
  1673 			}
       
  1674 
       
  1675         if (pthread_key_delete (keys[i]) != 0)
       
  1676 			{
       
  1677             ERR_PRINTF1(_L("key_delete failed\n"));
       
  1678             retVal = KErrPThreadKeyDelete;
       
  1679 
       
  1680 			}
       
  1681 		}
       
  1682 
       
  1683     INFO_PRINTF1(_L("All keys get and delete success\n"));
       
  1684 
       
  1685   /* Now it must be once again possible to allocate keys.  */
       
  1686     if (pthread_key_create (&keys[0], NULL) != 0)
       
  1687 		{
       
  1688         ERR_PRINTF1(_L("key_create failed \n"));
       
  1689         retVal = KErrPThreadKeyCreate;
       
  1690 		}
       
  1691 
       
  1692     INFO_PRINTF1(_L("all key 2nd time create success\n"));
       
  1693     if (pthread_key_delete (keys[0]) != 0)
       
  1694 		{
       
  1695         ERR_PRINTF1(_L("key_delete failed\n"));
       
  1696         retVal = KErrPThreadKeyDelete;
       
  1697 
       
  1698 		}
       
  1699     INFO_PRINTF1(_L("all key 2nd time delete success\n"));
       
  1700 
       
  1701     if(retVal == KErrNone)
       
  1702 	    {
       
  1703 	    TBuf<100> apiName(KPThreadGetScope);
       
  1704 	    LogResult(EPositive , apiName);
       
  1705 	    free(keys);
       
  1706 	    }
       
  1707 	//All Test cases passed...
       
  1708 
       
  1709     return retVal;
       
  1710 
       
  1711 	}
       
  1712 
       
  1713 TInt CTestPThread::PThreadKeyCreateAPITest2()
       
  1714 	{
       
  1715     TInt retVal = KErrNone;
       
  1716     int max;
       
  1717     int i;
       
  1718     pthread_key_t *keys=NULL;
       
  1719 
       
  1720 	#ifdef PTHREAD_KEYS_MAX
       
  1721 		max = PTHREAD_KEYS_MAX;
       
  1722 	#else
       
  1723 		max = _POSIX_THREAD_KEYS_MAX;
       
  1724 	#endif
       
  1725 
       
  1726     keys = (pthread_key_t *)malloc ((max +1) * sizeof (pthread_key_t));
       
  1727     if(!keys)
       
  1728         return KErrGeneral;
       
  1729     for (i = 0;i < max; ++i)
       
  1730 		{
       
  1731         if (pthread_key_create (&keys[i], NULL) != 0)
       
  1732 			{
       
  1733             ERR_PRINTF1(_L("key_create failed \n"));
       
  1734             retVal = KErrPThreadKeyCreate;
       
  1735 			}
       
  1736 		}
       
  1737     INFO_PRINTF1(_L("All keys are created successfully\n"));
       
  1738 
       
  1739     /* Try more than MAX keys */
       
  1740     if (pthread_key_create (&keys[i], NULL) != EAGAIN)
       
  1741 		{
       
  1742         ERR_PRINTF1(_L("key_create failed \n"));
       
  1743         retVal = KErrPThreadKeyCreate;
       
  1744 		}
       
  1745 
       
  1746     INFO_PRINTF1(_L("Testcase: overflow test passed\n"));
       
  1747     if(retVal == KErrNone)
       
  1748 	    {
       
  1749 	    TBuf<100> apiName(KPThreadGetScope);
       
  1750 	    LogResult(EPositive , apiName);
       
  1751 	    }
       
  1752 	//All Test cases passed...
       
  1753 
       
  1754     for (i = 0;i < max; ++i)
       
  1755 		{
       
  1756         if (pthread_key_delete (keys[i]) != 0)
       
  1757 			{
       
  1758             ERR_PRINTF1(_L("key_create failed \n"));
       
  1759             retVal = KErrPThreadKeyCreate;
       
  1760 			}
       
  1761 		}
       
  1762 
       
  1763 	if (keys )
       
  1764 		{
       
  1765 		free(keys);
       
  1766 		}
       
  1767     return retVal;
       
  1768 	}
       
  1769 
       
  1770 
       
  1771 TInt CTestPThread::PThreadSetSpecificAPITest()
       
  1772 	{
       
  1773     TInt retVal = KErrNone;
       
  1774     pthread_key_t keys;
       
  1775 
       
  1776     // Max Key testing : Manually changing key for testing purpose only
       
  1777     keys = 128;
       
  1778 
       
  1779     if (pthread_setspecific (keys, (void *) 20l) != EINVAL)
       
  1780 		{
       
  1781         ERR_PRINTF1(_L("setspecific failed \n"));
       
  1782         retVal = KErrPThreadSetSpecific;
       
  1783 		}
       
  1784     INFO_PRINTF1(_L("setspecfic passed MAX test\n"));
       
  1785 
       
  1786     if (pthread_getspecific (keys) != NULL)
       
  1787 		{
       
  1788         ERR_PRINTF1(_L("getspecific failed \n"));
       
  1789         retVal = KErrPThreadGetSpecific;
       
  1790 		}
       
  1791     INFO_PRINTF1(_L("getspecfic passed MAX test\n"));
       
  1792 
       
  1793     if (pthread_key_delete (keys) != EINVAL)
       
  1794 		{
       
  1795         ERR_PRINTF1(_L("key_delete failed \n"));
       
  1796         retVal = KErrPThreadKeyDelete;
       
  1797 		}
       
  1798     INFO_PRINTF1(_L("key_delete passed MAX test\n"));
       
  1799 
       
  1800     INFO_PRINTF1(_L("Testcase: MAX test passed\n"));
       
  1801     if(retVal == KErrNone)
       
  1802 	    {
       
  1803 	    TBuf<100> apiName(KPThreadGetScope);
       
  1804 	    LogResult(EPositive , apiName);
       
  1805 	    }
       
  1806 	//All Test cases passed...
       
  1807     return retVal;
       
  1808 	}
       
  1809 
       
  1810 TInt CTestPThread::PThreadGetSpecificAPITest()
       
  1811 	{
       
  1812     TInt retVal = KErrNone;
       
  1813     pthread_key_t keys;
       
  1814 
       
  1815     // Max Key testing : Manually changing key for testing purpose only
       
  1816     keys = 128;
       
  1817 
       
  1818     if (pthread_setspecific (keys, (void *) 20l) != EINVAL)
       
  1819 		{
       
  1820         ERR_PRINTF1(_L("setspecific failed \n"));
       
  1821         retVal = KErrPThreadSetSpecific;
       
  1822 		}
       
  1823     INFO_PRINTF1(_L("setspecfic passed MAX test\n"));
       
  1824 
       
  1825     if (pthread_getspecific (keys) != NULL)
       
  1826 		{
       
  1827         ERR_PRINTF1(_L("getspecific failed \n"));
       
  1828         retVal = KErrPThreadGetSpecific;
       
  1829 		}
       
  1830     INFO_PRINTF1(_L("getspecfic passed MAX test\n"));
       
  1831 
       
  1832     if (pthread_key_delete (keys) != EINVAL)
       
  1833 		{
       
  1834         ERR_PRINTF1(_L("key_delete failed \n"));
       
  1835         retVal = KErrPThreadKeyDelete;
       
  1836 		}
       
  1837     INFO_PRINTF1(_L("key_delete passed MAX test\n"));
       
  1838 
       
  1839     INFO_PRINTF1(_L("Testcase: MAX test passed\n"));
       
  1840 
       
  1841     if(retVal == KErrNone)
       
  1842 	    {
       
  1843 	    TBuf<100> apiName(KPThreadGetScope);
       
  1844 	    LogResult(EPositive , apiName);
       
  1845 	    }
       
  1846 	//All Test cases passed...
       
  1847     return retVal;
       
  1848 	}
       
  1849 	
       
  1850 //static functions added for PThreadJoinTest case
       
  1851 static void *first(void *arg) 
       
  1852 	{
       
  1853 	if(arg!=NULL)
       
  1854 		{
       
  1855 		printf("the argument passed to this function is not NULL");
       
  1856 		}
       
  1857 	printf("First");
       
  1858 	fflush(stdout);
       
  1859 	return 0;
       
  1860 	}
       
  1861 
       
  1862 static void *second(void *arg) 	
       
  1863 	{
       
  1864 	if(arg!=NULL)
       
  1865 		{
       
  1866 		printf("the argument passed to this function is not NULL");
       
  1867 		}
       
  1868 	printf("Second");
       
  1869 	fflush(stdout);
       
  1870 	return 0;
       
  1871 	}
       
  1872 
       
  1873 static void *third(void *arg) 
       
  1874 	{
       
  1875 	if(arg!=NULL)
       
  1876 		{
       
  1877 		printf("the argument passed to this function is not NULL");
       
  1878 		}
       
  1879 	printf("Third");
       
  1880 	fflush(stdout);
       
  1881 	return 0;
       
  1882 	}
       
  1883 
       
  1884 TInt CTestPThread::PThreadJoinTest()
       
  1885 	{
       
  1886 	TInt retVal = KErrNone, fd, newfd;
       
  1887 	pthread_t new_th[3];
       
  1888 	char buf[64];
       
  1889 	fd = open("c:\\pthread.txt" , O_CREAT | O_RDWR | O_EXCL , 0666);
       
  1890 	if(fd < 0 ) 
       
  1891   		{
       
  1892   		INFO_PRINTF2(_L("Failed to create and open a file\n %d"), errno);
       
  1893    		}
       
  1894 	else
       
  1895   		{
       
  1896    		INFO_PRINTF1(_L("File created and opened in current working directory \n"  ));
       
  1897    		}
       
  1898    	//writing the contents to a file
       
  1899 	newfd = dup2(fd,1);
       
  1900 	if(newfd < 0 ) 
       
  1901  		{
       
  1902   		INFO_PRINTF1(_L("Failed to duplicate file descriptor \n"));
       
  1903  		}
       
  1904 	else
       
  1905   		{
       
  1906   		INFO_PRINTF2(_L("the newfd returned is %d"),newfd);
       
  1907   		}
       
  1908   	FILE *fp = fopen("c:\\pthread.txt", "r");
       
  1909 	if(fp)
       
  1910 		{
       
  1911 		fscanf(fp, "%s", buf);
       
  1912 		}
       
  1913 	if(strcmp(buf, "FirstSecondThird") != 0)
       
  1914 		{
       
  1915 		INFO_PRINTF1(_L("All threads are not executed\n"));
       
  1916 		}
       
  1917 	else
       
  1918 		{
       
  1919 		INFO_PRINTF1(_L("Executed all the threads\n"));	
       
  1920 		}	
       
  1921 	
       
  1922 	// Create a first thread. 
       
  1923 	if((retVal = pthread_create(&new_th[0], NULL, first, NULL)) != 0)
       
  1924 		{
       
  1925 	  	INFO_PRINTF1(_L("Error in creating first thread \n"));
       
  1926 	  	INFO_PRINTF2(_L("the value returned is %d\n"),retVal);
       
  1927 	  	}
       
  1928 	 
       
  1929 	// Create a second thread. 
       
  1930 	if((retVal = pthread_create(&new_th[1], NULL, second, NULL) )!= 0)
       
  1931 		{
       
  1932 	  	INFO_PRINTF1(_L("Error in creating second thread \n"));
       
  1933 	  	INFO_PRINTF2(_L("the value returned is %d\n"),retVal);
       
  1934 	  	}
       
  1935 	 
       
  1936 	// Create a third thread. 
       
  1937 	if((retVal = pthread_create(&new_th[2], NULL, third, NULL)) != 0)
       
  1938 	 	{
       
  1939 		INFO_PRINTF1(_L("Error in creating third thread \n"));
       
  1940 		INFO_PRINTF2(_L("the value returned is %d\n"),retVal);
       
  1941 	    }
       
  1942 	
       
  1943 	// Wait for threads to return 
       
  1944 	if((retVal = pthread_join(new_th[0], NULL)) != 0)//first thread returning
       
  1945 		{
       
  1946 	    INFO_PRINTF2(_L("Error in pthread_join() and the value returned is %d\n"),retVal);
       
  1947 	    }
       
  1948 	 
       
  1949 	if((retVal = pthread_join(new_th[1], NULL)) != 0)//second thread returning
       
  1950 		{
       
  1951 	    INFO_PRINTF2(_L("Error in pthread_join() and the value returned is %d\n"),retVal);
       
  1952 	  	}
       
  1953 	 
       
  1954 	if((retVal = pthread_join(new_th[2], NULL)) != 0)//third thread returning
       
  1955 		{
       
  1956 	    INFO_PRINTF2(_L("Error in pthread_join() and the value returned is %d\n"),retVal);
       
  1957 		}	 
       
  1958 	close(fd);
       
  1959  	close(newfd);
       
  1960  	fclose(fp);
       
  1961 	//All Test cases passed...
       
  1962     return retVal;
       
  1963 	}
       
  1964 /*
       
  1965  * the below test case is added to check the memory leak on navi engine in pthread_exit.
       
  1966  
       
  1967  */
       
  1968 
       
  1969 static void* ThreadFunc(void*)
       
  1970 	{
       
  1971 	 return 0;
       
  1972 	}
       
  1973 TInt CTestPThread::PThreadExitMemLeak()
       
  1974 	{
       
  1975 		const char* const KThreadName = "THRD1";
       
  1976 		pthread_t threadId = 0;
       
  1977 		int err = 0;
       
  1978 		err = pthread_create(&threadId, 0, &ThreadFunc, (void*)KThreadName);
       
  1979 		if( err  != 0 )
       
  1980 			{
       
  1981 			INFO_PRINTF2(_L("Error in pthread_create() and the value returned is %d\n"),err);
       
  1982 			}
       
  1983 		err = pthread_join(threadId, 0);
       
  1984 		if( err  != 0)
       
  1985 			{
       
  1986 			 INFO_PRINTF2(_L("Error in pthread_join() and the value returned is %d\n"),err);
       
  1987 			}
       
  1988 		return err;
       
  1989 	}
       
  1990