servicediscoveryandcontrol/pnp/test/upnp/chunkmgr/ts_chunkmgr/src/test08multiplethreads.cpp
changeset 0 f5a58ecadc66
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // @file
       
    15 // @internalComponent
       
    16 // 
       
    17 //
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <ss_std.h>
       
    21 #include <rmemchunk.h>
       
    22 
       
    23 #include "test08multiplethreads.h"
       
    24 
       
    25 
       
    26 TVerdict CTest08MultipleThreads::doTestStepL ()
       
    27 	{
       
    28 	__UHEAP_MARK;
       
    29 	iMainTryCount = 0;
       
    30 	iChildTryCount = 0;
       
    31 	
       
    32 	CleanupStack::PushL ( CreateChunkMgrL ( KMinHeapSize ) );	
       
    33 	RMemoryAllocator allocator ( iChkMgr );
       
    34 	
       
    35 	INFO_PRINTF1 ( _L ( "Create high priority thread and resume it" ) );
       
    36 	
       
    37 	RThread childThread;
       
    38 	TInt err = childThread.Create ( _L ( "testChildThread" ),
       
    39 		                       		ChildThreadStep,
       
    40  							   		KDefaultStackSize,
       
    41 							   		KDefaultHeapSize,
       
    42 							   		KMaxHeapSize,
       
    43 							   		(TAny*)this,
       
    44 							   		EOwnerProcess );
       
    45 	if ( err != KErrNone )
       
    46 		{
       
    47 		INFO_PRINTF2 ( _L ( "\nError:%d Could not create child thread" ), err );		 
       
    48 		User::Leave ( EFail );
       
    49 		}
       
    50 	childThread.SetPriority ( EPriorityAbsoluteHigh );
       
    51 	childThread.Resume ();
       
    52 	
       
    53 	INFO_PRINTF1 ( _L ( "\nDo alloc/free in loop that ends when the child task completes" ) );	
       
    54 	TInt ret;
       
    55 	volatile TExitType exitType;
       
    56 	do {
       
    57 		RMemChunk chunk;
       
    58 		ret = chunk.Alloc ( 500, allocator );
       
    59 		if ( ret != KErrNone )
       
    60 			{
       
    61 			INFO_PRINTF1 ( _L ( "\nError: Couldn't allocate RMemChunk" ) );
       
    62 			User::Leave ( EFail );
       
    63 			}
       
    64 		iMainTryCount++;
       
    65 		chunk.Free ();
       
    66 		// Check if the child task is completed
       
    67 		exitType = childThread.ExitType ();
       
    68 		}
       
    69 	while ( exitType == EExitPending );
       
    70 	
       
    71 	CleanupStack::PopAndDestroy ( iChkMgr );
       
    72 	childThread.Close ();
       
    73 	if ( iChildTryCount < 1000 )
       
    74 		{
       
    75 		INFO_PRINTF1 ( _L ( "\nError: Child thread ended before 1000 loops completetion" ) );
       
    76 		return EFail;
       
    77 		}
       
    78 
       
    79 	INFO_PRINTF2 ( _L ( "\nChild thread Alloc/Dealloc's %d" ), iChildTryCount );	
       
    80 	INFO_PRINTF2 ( _L ( "\nMain Thread Alloc/Dealloc's %d" ), iMainTryCount );
       
    81 	
       
    82 	__UHEAP_MARKEND;
       
    83 	
       
    84 	return EPass;
       
    85 	}
       
    86 
       
    87 TInt CTest08MultipleThreads::ChildThreadStep ( TAny* aParams )
       
    88 	{
       
    89 	CTest08MultipleThreads* testObj = ( CTest08MultipleThreads* ) aParams;
       
    90 	RMemoryAllocator allocator ( testObj->iChkMgr );
       
    91 	
       
    92 	CTrapCleanup* trapHandler = CTrapCleanup::New ();
       
    93     CActiveScheduler* scheduler = new CActiveScheduler;
       
    94 	if ( scheduler == NULL )
       
    95 		{
       
    96 		return KErrNoMemory;
       
    97 		}
       
    98 	CActiveScheduler::Install ( scheduler );
       
    99 	
       
   100 	RTimer timer;
       
   101 	TRequestStatus status;
       
   102 	timer.CreateLocal ();
       
   103 	TInt ret = KErrNone;
       
   104 	for ( TInt i = 0 ; i < 1000; i++ )
       
   105 		{
       
   106 		RMemChunk chunk;
       
   107 		ret = chunk.Alloc ( 500, allocator );
       
   108 		if ( ret != KErrNone )
       
   109 			break;
       
   110 		
       
   111 		testObj->iChildTryCount++;
       
   112 		
       
   113 		chunk.Free ();
       
   114 	 		 	
       
   115 	 	timer.After ( status, 500 ); // sleep for 5ms
       
   116 		User::WaitForRequest ( status );
       
   117 		}
       
   118 	
       
   119 	CActiveScheduler::Install ( NULL );
       
   120 	delete scheduler;
       
   121 	delete trapHandler;
       
   122 	
       
   123 	return ret;
       
   124 	}