kerneltest/e32test/dmav2/test_thread.cpp
branchRCL_3
changeset 43 c1f20ce4abcf
equal deleted inserted replaced
42:a179b74831c9 43:c1f20ce4abcf
       
     1 /*
       
     2 * Copyright (c) 2009-2010 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 * Implementation of test_thread.h
       
    16 *
       
    17 */
       
    18 #include "test_thread.h"
       
    19 
       
    20 TInt TTestRemote::iCount=0;
       
    21 
       
    22 TInt TTestRemote::RunFunctor(TAny* aFunctor)
       
    23 	{
       
    24 	TFunctor& functor = *(TFunctor*)aFunctor;
       
    25 	functor();
       
    26 	return KErrNone;
       
    27 	}
       
    28 
       
    29 TTestThread::TTestThread(const TDesC& aName, TThreadFunction aFn, TAny* aData, TBool aAutoResume)
       
    30 	{
       
    31 	Init(aName, aFn, aData, aAutoResume);
       
    32 	}
       
    33 
       
    34 TTestThread::TTestThread(const TDesC& aName, TFunctor& aFunctor, TBool aAutoResume)
       
    35 	{
       
    36 	Init(aName, RunFunctor, &aFunctor, aAutoResume);
       
    37 	}
       
    38 
       
    39 TTestThread::~TTestThread()
       
    40 	{
       
    41 	iThread.Close();
       
    42 	}
       
    43 
       
    44 void TTestThread::Resume()
       
    45 	{
       
    46 	iThread.Resume();
       
    47 	}
       
    48 
       
    49 TInt TTestThread::WaitForExitL()
       
    50 	{
       
    51 	User::WaitForRequest(iLogonStatus);
       
    52 	const TInt exitType = iThread.ExitType();
       
    53 	const TInt exitReason = iThread.ExitReason();
       
    54 
       
    55 	__ASSERT_ALWAYS(exitType != EExitPending, User::Panic(_L("TTestThread"),0));
       
    56 
       
    57 	if(exitType != EExitKill)
       
    58 		User::Leave(exitReason);
       
    59 
       
    60 	return exitReason;
       
    61 	}
       
    62 
       
    63 void TTestThread::Rendezvous(TRequestStatus& aStatus)
       
    64 	{
       
    65 	iThread.Rendezvous(aStatus);
       
    66 	}
       
    67 
       
    68 void TTestThread::Init(const TDesC& aName, TThreadFunction aFn, TAny* aData, TBool aAutoResume)
       
    69 	{
       
    70 	TKName name(aName);
       
    71 	name.AppendFormat(_L("-%d"), iCount++);
       
    72 	TInt r=iThread.Create(name, aFn, KDefaultStackSize, KHeapSize, KHeapSize, aData);
       
    73 	if(r!=KErrNone)
       
    74 		{
       
    75 		RDebug::Printf("RThread::Create failed, code=%d", r);
       
    76 		User::Panic(KPanicCat, EThreadCreateFailed);
       
    77 		}
       
    78 
       
    79 	iThread.Logon(iLogonStatus);
       
    80 	__ASSERT_ALWAYS(iLogonStatus == KRequestPending, User::Panic(_L("TTestThread"),0));
       
    81 
       
    82 	if(aAutoResume)
       
    83 		iThread.Resume();
       
    84 	}
       
    85 
       
    86 
       
    87 CTest::~CTest()
       
    88 	{
       
    89 	iName.Close();
       
    90 	}
       
    91 
       
    92 void CTest::SetupL()
       
    93 	{
       
    94 	}
       
    95 
       
    96 void CTest::operator()()
       
    97 	{
       
    98 	for(TInt i=0; i<iIterations; i++)
       
    99 		{
       
   100 		RunTest();
       
   101 		}
       
   102 	}
       
   103 
       
   104 
       
   105 void CTest::Announce() const
       
   106 {
       
   107 	RDebug::RawPrint(_L("Test: "));
       
   108 	PrintTestInfo();
       
   109 	RDebug::RawPrint(_L(": "));
       
   110 	PrintTestType();
       
   111 	RDebug::RawPrint(_L(": "));
       
   112 	RDebug::RawPrint(iName);
       
   113 	RDebug::RawPrint(_L(": "));
       
   114 	RDebug::Printf("(%d iterations)", iIterations);
       
   115 }
       
   116 
       
   117 
       
   118 void CTest::PrintTestInfo() const
       
   119 	{
       
   120 	}
       
   121 const TDesC& CTest::Name() const
       
   122 	{
       
   123 	return iName;
       
   124 	}
       
   125 
       
   126 CTest::CTest(const TDesC& aName, TInt aIterations)
       
   127 	:iIterations(aIterations)
       
   128 	{
       
   129 	iName.CreateL(aName);
       
   130 	}
       
   131 
       
   132 CTest::CTest(const CTest& aOther)
       
   133 	:iIterations(aOther.iIterations)
       
   134 	{
       
   135 	iName.CreateL(aOther.iName);
       
   136 	}
       
   137 
       
   138 void MultipleTestRun(RTest& test, const CTest& aTest, TInt aNumberOfThreads)
       
   139 	{
       
   140 	RPointerArray<CTest> testArray;
       
   141 	RPointerArray<TTestThread> threadArray;
       
   142 
       
   143 	for(TInt i=0; i<aNumberOfThreads; i++)
       
   144 		{
       
   145 		test.Next(_L("Create test thread"));
       
   146 		CTest* newTest = aTest.Clone();
       
   147 		test_NotNull(newTest);
       
   148 
       
   149 		TTestThread* thread = new TTestThread(aTest.Name(), *newTest);
       
   150 		test_NotNull(thread);
       
   151 
       
   152 		threadArray.AppendL(thread);
       
   153 		testArray.AppendL(newTest);
       
   154 		}
       
   155 
       
   156 	const TInt count = threadArray.Count();
       
   157 	for(TInt j=0; j<count; j++)
       
   158 		{
       
   159 		TTestThread* thread = threadArray[0];
       
   160 		
       
   161 		TInt r = KErrNone;
       
   162 		TRAPD(leaveCode, r = thread->WaitForExitL());
       
   163 		if(leaveCode != KErrNone)
       
   164 			{
       
   165 			test.Printf(_L("Thread %d: Panic code:%d\n"), j, leaveCode);
       
   166 			test_KErrNone(leaveCode);
       
   167 			}
       
   168 
       
   169 		if(r!=KErrNone)
       
   170 			{
       
   171 			test.Printf(_L("Thread Number %d\n"), j);
       
   172 			test_KErrNone(r);
       
   173 			}
       
   174 
       
   175 		threadArray.Remove(0);
       
   176 		delete thread;
       
   177 		}
       
   178 	threadArray.Close();
       
   179 
       
   180 	testArray.ResetAndDestroy();
       
   181 	testArray.Close();
       
   182 	}
       
   183 
       
   184 /**
       
   185 Runs each CTest in aTests in its own thread
       
   186 Returns once all threads have terminated
       
   187 */
       
   188 void MultipleTestRun(const RPointerArray<CTest>& aTests)
       
   189 	{
       
   190 	RTest test(_L("CTest::MultipleTestRun"));
       
   191 	RPointerArray<TTestThread> threads;
       
   192 
       
   193 	const TInt count = aTests.Count();
       
   194 
       
   195 	TInt i;
       
   196 	for(i=0; i<count; i++)
       
   197 		{
       
   198 		_LIT(KDmaTestThread, "DMA-test-thread");
       
   199 		TTestThread* thread = new TTestThread(KDmaTestThread, *aTests[i], EFalse);
       
   200 		test_NotNull(thread);
       
   201 		TInt r = threads.Append(thread);
       
   202 		test_KErrNone(r);
       
   203 		}
       
   204 
       
   205 	for(i=0; i<count; i++)
       
   206 		{
       
   207 		threads[i]->Resume();
       
   208 		}
       
   209 
       
   210 
       
   211 	for(i=0; i<count; i++)
       
   212 		{
       
   213 		TInt r = threads[i]->WaitForExitL();
       
   214 		test_KErrNone(r);
       
   215 		}
       
   216 
       
   217 	threads.ResetAndDestroy();
       
   218 	test.Close();
       
   219 	}