sl@0: // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // e32test\bench\t_ctxsw.cpp sl@0: // Overview: sl@0: // Test and benchmark inter-thread context switch. sl@0: // API Information: sl@0: // RThread, RSemaphore, TRequestStatus. sl@0: // Details: sl@0: // - Creates a thread with a handle to a Semaphore, check for successful sl@0: // creation and assign priority EPriorityLess. sl@0: // - Creates another thread with the handle to the thread already created, sl@0: // check for successful creation and assign priority EPriorityMuchLess. sl@0: // - Makes the two threads eligible for execution. sl@0: // - First thread counts the number of semaphore requests. sl@0: // - Second thread counts the number of times the thread is signalled on sl@0: // completion of request. sl@0: // - Terminate the threads, print the count per second. sl@0: // Platforms/Drives/Compatibility: sl@0: // All. sl@0: // Assumptions/Requirement/Pre-requisites: sl@0: // Failures and causes: sl@0: // Base Port information: sl@0: // sl@0: // sl@0: sl@0: #define __E32TEST_EXTENSION__ sl@0: #include sl@0: sl@0: RTest test(_L("T_CTXSW")); sl@0: sl@0: volatile TInt Count1; sl@0: volatile TInt Count2; sl@0: sl@0: TInt Thread1(TAny* aSemaphore) sl@0: { sl@0: RSemaphore s; sl@0: s.SetHandle((TInt)aSemaphore); sl@0: User::WaitForAnyRequest(); sl@0: s.Signal(); sl@0: FOREVER sl@0: { sl@0: User::WaitForAnyRequest(); sl@0: Count1++; sl@0: } sl@0: } sl@0: sl@0: TInt Thread2(TAny* aThread) sl@0: { sl@0: TRequestStatus s; sl@0: RThread t; sl@0: t.SetHandle((TInt)aThread); sl@0: FOREVER sl@0: { sl@0: Count2++; sl@0: TRequestStatus* ps=&s; sl@0: t.RequestComplete(ps,0); sl@0: } sl@0: } sl@0: sl@0: const TInt KHeapSize=4096; sl@0: void Test1() sl@0: { sl@0: test.Start(_L("Test 1")); sl@0: sl@0: RSemaphore sem; sl@0: TInt r = sem.CreateLocal(0); sl@0: test_KErrNone(r); sl@0: RThread t1,t2; sl@0: RThread* pt1, *pt2; sl@0: sl@0: pt1 = NULL; sl@0: pt2 = NULL; sl@0: sl@0: Count1=Count2=0; sl@0: r=t1.Create(_L("Thread1"),Thread1,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)sem.Handle()); sl@0: if (!pt1) sl@0: pt1=&t1; sl@0: else sl@0: test(pt1==&t1); sl@0: sl@0: test_KErrNone(r); sl@0: t1.SetPriority(EPriorityLess); sl@0: r=t2.Create(_L("Thread2"),Thread2,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)t1.Handle()); sl@0: if (!pt2) sl@0: pt2=&t2; sl@0: else sl@0: test(pt2==&t2); sl@0: test_KErrNone(r); sl@0: t2.SetPriority(EPriorityMuchLess); sl@0: sl@0: TRequestStatus s1,s2; sl@0: t1.Logon(s1); sl@0: t2.Logon(s2); sl@0: t1.Resume(); sl@0: t2.Resume(); sl@0: sem.Wait(); sl@0: Count1=Count2=0; sl@0: User::After(16000000); sl@0: t2.Kill(0); sl@0: User::WaitForRequest(s2); sl@0: t1.Kill(0); sl@0: User::WaitForRequest(s1); sl@0: sl@0: t2.Close(); sl@0: t1.Close(); sl@0: test.Printf(_L("Ran %d times in one second.\n"),(Count1+Count2)/16); // get the value sl@0: sem.Close(); sl@0: sl@0: test.End(); sl@0: } sl@0: sl@0: struct SThread3Info sl@0: { sl@0: TRequestStatus iStatus; sl@0: TRequestStatus iLogonStatus; sl@0: RThread iThread; sl@0: volatile TUint32 iCount; sl@0: SThread3Info* iOther; sl@0: TUint32 iPadding[9]; sl@0: }; sl@0: sl@0: void Thread3(TAny* aInfo) sl@0: { sl@0: SThread3Info& me = *(SThread3Info*)aInfo; sl@0: SThread3Info& you = *me.iOther; sl@0: FOREVER sl@0: { sl@0: TRequestStatus* ps = &you.iStatus; sl@0: you.iThread.RequestComplete(ps, 0); sl@0: User::WaitForAnyRequest(); sl@0: ++me.iCount; sl@0: Count2++; sl@0: } sl@0: } sl@0: sl@0: void Test2() sl@0: { sl@0: test.Start(_L("Test 2")); sl@0: sl@0: SThread3Info info[2]; sl@0: TInt r; sl@0: TInt i; sl@0: for (i=0; i<2; ++i) sl@0: { sl@0: info[i].iCount = 0; sl@0: info[i].iOther = &info[1-i]; sl@0: r = info[i].iThread.Create(KNullDesC, (TThreadFunction)&Thread3, 0x1000, 0, &info[i]); sl@0: test_KErrNone(r); sl@0: info[i].iThread.Logon(info[i].iLogonStatus); sl@0: test_Equal(KRequestPending, info[i].iLogonStatus.Int()); sl@0: } sl@0: info[0].iThread.SetPriority(EPriorityMuchLess); sl@0: info[1].iThread.SetPriority(EPriorityLess); sl@0: info[0].iThread.Resume(); sl@0: info[1].iThread.Resume(); sl@0: User::AfterHighRes(100000); sl@0: TUint32 ic0 = info[0].iCount; sl@0: TUint32 ic1 = info[1].iCount; sl@0: User::AfterHighRes(10000000); sl@0: info[0].iThread.Kill(0); sl@0: info[1].iThread.Kill(0); sl@0: User::WaitForRequest(info[0].iLogonStatus); sl@0: User::WaitForRequest(info[1].iLogonStatus); sl@0: test_Equal(EExitKill, info[0].iThread.ExitType()); sl@0: test_Equal(0, info[0].iThread.ExitReason()); sl@0: test_Equal(EExitKill, info[1].iThread.ExitType()); sl@0: test_Equal(0, info[1].iThread.ExitReason()); sl@0: sl@0: TUint32 fc0 = info[0].iCount; sl@0: TUint32 fc1 = info[1].iCount; sl@0: test.Printf(_L("T0: Initial %10u Final %10u Diff %10u PerSec %10u\n"), ic0, fc0, (fc0-ic0), (fc0-ic0)/10); sl@0: test.Printf(_L("T1: Initial %10u Final %10u Diff %10u PerSec %10u\n"), ic1, fc1, (fc1-ic1), (fc1-ic1)/10); sl@0: sl@0: CLOSE_AND_WAIT(info[0].iThread); sl@0: CLOSE_AND_WAIT(info[1].iThread); sl@0: sl@0: test.End(); sl@0: } sl@0: sl@0: TInt E32Main() sl@0: { sl@0: sl@0: test.Title(); sl@0: test.Start(_L("Timing inter-thread context switches...")); sl@0: sl@0: Test1(); sl@0: Test2(); sl@0: sl@0: test.End(); sl@0: return 0; sl@0: }