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_ctxsw3.cpp sl@0: // Overview: sl@0: // Benchmark the inter-thread context switching. sl@0: // API Information: sl@0: // RThread, RRSemaphore. sl@0: // Details: sl@0: // - Create a semaphore. sl@0: // - Create a thread (thread1) with a handle to the Semaphore and sl@0: // priority EPriorityLess. Verify the successful creation. sl@0: // - Create another thread (thread2) with a handle to the Semaphore sl@0: // and priority EPriorityMuchLess. Verify the successful creation. sl@0: // - thread1 counts the number of times the semaphore count is decremented. sl@0: // - thread2 counts the number of times the semaphore count is incremented. sl@0: // - Display the number of times the semaphore is signalled 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: #include sl@0: sl@0: RTest test(_L("T_CTXSW3")); sl@0: sl@0: LOCAL_D TInt Count; sl@0: sl@0: LOCAL_D RSemaphore StartSem; sl@0: sl@0: LOCAL_C TInt Thread1(TAny* aSemaphore) sl@0: { sl@0: RSemaphore s; sl@0: s.SetHandle((TInt)aSemaphore); sl@0: StartSem.Wait(); sl@0: FOREVER sl@0: { sl@0: s.Wait(); sl@0: Count++; sl@0: } sl@0: } sl@0: sl@0: LOCAL_C TInt Thread2(TAny* aSemaphore) sl@0: { sl@0: RSemaphore s; sl@0: s.SetHandle((TInt)aSemaphore); sl@0: StartSem.Wait(); sl@0: FOREVER sl@0: { sl@0: s.Signal(); sl@0: Count++; sl@0: } sl@0: } sl@0: sl@0: const TInt KHeapSize=4096; sl@0: sl@0: GLDEF_C TInt E32Main() sl@0: { sl@0: test.Title(); sl@0: test.Start(_L("Timing inter-thread context switches...")); sl@0: sl@0: TInt r=StartSem.CreateLocal(0); sl@0: test(r==KErrNone); sl@0: sl@0: RSemaphore sem; sl@0: r=sem.CreateLocal(0); sl@0: test(r==KErrNone); sl@0: RThread t1,t2; sl@0: r=t1.Create(_L("Thread1"),Thread1,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)sem.Handle()); sl@0: test(r==KErrNone); sl@0: t1.SetPriority(EPriorityLess); sl@0: r=t2.Create(_L("Thread2"),Thread2,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)sem.Handle()); sl@0: test(r==KErrNone); 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: sl@0: Count=0; sl@0: User::After(20000); sl@0: StartSem.Signal(2); sl@0: User::After(2000000); sl@0: TInt count=Count; sl@0: sl@0: t2.Kill(0); sl@0: User::WaitForRequest(s2); sl@0: t1.Kill(0); sl@0: User::WaitForRequest(s1); sl@0: t2.Close(); sl@0: t1.Close(); sl@0: sem.Close(); sl@0: sl@0: test.Printf(_L("%d per second\n"),count/2); sl@0: sl@0: test.End(); sl@0: return 0; sl@0: }