1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/dmav2/test_thread.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,216 @@
1.4 +/*
1.5 +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +* Implementation of test_thread.h
1.19 +*
1.20 +*/
1.21 +#include "test_thread.h"
1.22 +
1.23 +TInt TTestRemote::iCount=0;
1.24 +
1.25 +TInt TTestRemote::RunFunctor(TAny* aFunctor)
1.26 + {
1.27 + TFunctor& functor = *(TFunctor*)aFunctor;
1.28 + functor();
1.29 + return KErrNone;
1.30 + }
1.31 +
1.32 +TTestThread::TTestThread(const TDesC& aName, TThreadFunction aFn, TAny* aData, TBool aAutoResume)
1.33 + {
1.34 + Init(aName, aFn, aData, aAutoResume);
1.35 + }
1.36 +
1.37 +TTestThread::TTestThread(const TDesC& aName, TFunctor& aFunctor, TBool aAutoResume)
1.38 + {
1.39 + Init(aName, RunFunctor, &aFunctor, aAutoResume);
1.40 + }
1.41 +
1.42 +TTestThread::~TTestThread()
1.43 + {
1.44 + //RTest::CloseHandleAndWaitForDestruction(iThread);
1.45 + iThread.Close();
1.46 + }
1.47 +
1.48 +void TTestThread::Resume()
1.49 + {
1.50 + iThread.Resume();
1.51 + }
1.52 +
1.53 +TInt TTestThread::WaitForExitL()
1.54 + {
1.55 + User::WaitForRequest(iLogonStatus);
1.56 + const TInt exitType = iThread.ExitType();
1.57 + const TInt exitReason = iThread.ExitReason();
1.58 +
1.59 + __ASSERT_ALWAYS(exitType != EExitPending, User::Panic(_L("TTestThread"),0));
1.60 +
1.61 + if(exitType != EExitKill)
1.62 + User::Leave(exitReason);
1.63 +
1.64 + return exitReason;
1.65 + }
1.66 +
1.67 +void TTestThread::Rendezvous(TRequestStatus& aStatus)
1.68 + {
1.69 + iThread.Rendezvous(aStatus);
1.70 + }
1.71 +
1.72 +void TTestThread::Init(const TDesC& aName, TThreadFunction aFn, TAny* aData, TBool aAutoResume)
1.73 + {
1.74 + TKName name(aName);
1.75 + name.AppendFormat(_L("-%d"), iCount++);
1.76 + TInt r=iThread.Create(name, aFn, KDefaultStackSize, KHeapSize, KHeapSize, aData);
1.77 + if(r!=KErrNone)
1.78 + {
1.79 + RDebug::Printf("RThread::Create failed, code=%d", r);
1.80 + User::Panic(KPanicCat, EThreadCreateFailed);
1.81 + }
1.82 +
1.83 + iThread.Logon(iLogonStatus);
1.84 + __ASSERT_ALWAYS(iLogonStatus == KRequestPending, User::Panic(_L("TTestThread"),0));
1.85 +
1.86 + if(aAutoResume)
1.87 + iThread.Resume();
1.88 + }
1.89 +
1.90 +
1.91 +CTest::~CTest()
1.92 + {
1.93 + iName.Close();
1.94 + }
1.95 +
1.96 +void CTest::operator()()
1.97 + {
1.98 + for(TInt i=0; i<iIterations; i++)
1.99 + {
1.100 + RunTest();
1.101 + }
1.102 + }
1.103 +
1.104 +
1.105 +void CTest::Announce() const
1.106 +{
1.107 + RDebug::RawPrint(_L("Test: "));
1.108 + PrintTestInfo();
1.109 + RDebug::RawPrint(_L(": "));
1.110 + PrintTestType();
1.111 + RDebug::RawPrint(_L(": "));
1.112 + RDebug::RawPrint(iName);
1.113 + RDebug::RawPrint(_L(": "));
1.114 + RDebug::Printf("(%d iterations)", iIterations);
1.115 +}
1.116 +
1.117 +
1.118 +void CTest::PrintTestInfo() const
1.119 + {
1.120 + }
1.121 +const TDesC& CTest::Name() const
1.122 + {
1.123 + return iName;
1.124 + }
1.125 +
1.126 +CTest::CTest(const TDesC& aName, TInt aIterations)
1.127 + :iIterations(aIterations)
1.128 + {
1.129 + iName.CreateL(aName);
1.130 + }
1.131 +
1.132 +CTest::CTest(const CTest& aOther)
1.133 + :iIterations(aOther.iIterations)
1.134 + {
1.135 + iName.CreateL(aOther.iName);
1.136 + }
1.137 +
1.138 +void MultipleTestRun(RTest& test, const CTest& aTest, TInt aNumberOfThreads)
1.139 + {
1.140 + RPointerArray<CTest> testArray;
1.141 + RPointerArray<TTestThread> threadArray;
1.142 +
1.143 + for(TInt i=0; i<aNumberOfThreads; i++)
1.144 + {
1.145 + //test.Next(_L("Create test thread"));
1.146 + CTest* newTest = aTest.Clone();
1.147 + test_NotNull(newTest);
1.148 +
1.149 + TTestThread* thread = new TTestThread(aTest.Name(), *newTest);
1.150 + test_NotNull(thread);
1.151 +
1.152 + threadArray.AppendL(thread);
1.153 + testArray.AppendL(newTest);
1.154 + }
1.155 +
1.156 + const TInt count = threadArray.Count();
1.157 + for(TInt j=0; j<count; j++)
1.158 + {
1.159 + TTestThread* thread = threadArray[0];
1.160 +
1.161 + TInt r = KErrNone;
1.162 + TRAPD(leaveCode, r = thread->WaitForExitL());
1.163 + if(leaveCode != KErrNone)
1.164 + {
1.165 + test.Printf(_L("Thread %d: Panic code:%d\n"), j, leaveCode);
1.166 + test_KErrNone(leaveCode);
1.167 + }
1.168 +
1.169 + if(r!=KErrNone)
1.170 + {
1.171 + test.Printf(_L("Thread Number %d\n"), j);
1.172 + test_KErrNone(r);
1.173 + }
1.174 +
1.175 + threadArray.Remove(0);
1.176 + delete thread;
1.177 + }
1.178 + threadArray.Close();
1.179 +
1.180 + testArray.ResetAndDestroy();
1.181 + testArray.Close();
1.182 + }
1.183 +
1.184 +/**
1.185 +Runs each CTest in aTests in its own thread
1.186 +Returns once all threads have terminated
1.187 +*/
1.188 +void MultipleTestRun(const RPointerArray<CTest>& aTests)
1.189 + {
1.190 + RTest test(_L("CTest::MultipleTestRun"));
1.191 + RPointerArray<TTestThread> threads;
1.192 +
1.193 + const TInt count = aTests.Count();
1.194 +
1.195 + TInt i;
1.196 + for(i=0; i<count; i++)
1.197 + {
1.198 + _LIT(KDmaTestThread, "DMA-test-thread");
1.199 + TTestThread* thread = new TTestThread(KDmaTestThread, *aTests[i], EFalse);
1.200 + test_NotNull(thread);
1.201 + TInt r = threads.Append(thread);
1.202 + test_KErrNone(r);
1.203 + }
1.204 +
1.205 + for(i=0; i<count; i++)
1.206 + {
1.207 + threads[i]->Resume();
1.208 + }
1.209 +
1.210 +
1.211 + for(i=0; i<count; i++)
1.212 + {
1.213 + TInt r = threads[i]->WaitForExitL();
1.214 + test_KErrNone(r);
1.215 + }
1.216 +
1.217 + threads.ResetAndDestroy();
1.218 + test.Close();
1.219 + }