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