1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/bench/t_ctxsw3.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,113 @@
1.4 +// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// e32test\bench\t_ctxsw3.cpp
1.18 +// Overview:
1.19 +// Benchmark the inter-thread context switching.
1.20 +// API Information:
1.21 +// RThread, RRSemaphore.
1.22 +// Details:
1.23 +// - Create a semaphore.
1.24 +// - Create a thread (thread1) with a handle to the Semaphore and
1.25 +// priority EPriorityLess. Verify the successful creation.
1.26 +// - Create another thread (thread2) with a handle to the Semaphore
1.27 +// and priority EPriorityMuchLess. Verify the successful creation.
1.28 +// - thread1 counts the number of times the semaphore count is decremented.
1.29 +// - thread2 counts the number of times the semaphore count is incremented.
1.30 +// - Display the number of times the semaphore is signalled per second.
1.31 +// Platforms/Drives/Compatibility:
1.32 +// All.
1.33 +// Assumptions/Requirement/Pre-requisites:
1.34 +// Failures and causes:
1.35 +// Base Port information:
1.36 +//
1.37 +//
1.38 +
1.39 +#include <e32test.h>
1.40 +
1.41 +RTest test(_L("T_CTXSW3"));
1.42 +
1.43 +LOCAL_D TInt Count;
1.44 +
1.45 +LOCAL_D RSemaphore StartSem;
1.46 +
1.47 +LOCAL_C TInt Thread1(TAny* aSemaphore)
1.48 + {
1.49 + RSemaphore s;
1.50 + s.SetHandle((TInt)aSemaphore);
1.51 + StartSem.Wait();
1.52 + FOREVER
1.53 + {
1.54 + s.Wait();
1.55 + Count++;
1.56 + }
1.57 + }
1.58 +
1.59 +LOCAL_C TInt Thread2(TAny* aSemaphore)
1.60 + {
1.61 + RSemaphore s;
1.62 + s.SetHandle((TInt)aSemaphore);
1.63 + StartSem.Wait();
1.64 + FOREVER
1.65 + {
1.66 + s.Signal();
1.67 + Count++;
1.68 + }
1.69 + }
1.70 +
1.71 +const TInt KHeapSize=4096;
1.72 +
1.73 +GLDEF_C TInt E32Main()
1.74 + {
1.75 + test.Title();
1.76 + test.Start(_L("Timing inter-thread context switches..."));
1.77 +
1.78 + TInt r=StartSem.CreateLocal(0);
1.79 + test(r==KErrNone);
1.80 +
1.81 + RSemaphore sem;
1.82 + r=sem.CreateLocal(0);
1.83 + test(r==KErrNone);
1.84 + RThread t1,t2;
1.85 + r=t1.Create(_L("Thread1"),Thread1,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)sem.Handle());
1.86 + test(r==KErrNone);
1.87 + t1.SetPriority(EPriorityLess);
1.88 + r=t2.Create(_L("Thread2"),Thread2,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)sem.Handle());
1.89 + test(r==KErrNone);
1.90 + t2.SetPriority(EPriorityMuchLess);
1.91 +
1.92 + TRequestStatus s1,s2;
1.93 + t1.Logon(s1);
1.94 + t2.Logon(s2);
1.95 + t1.Resume();
1.96 + t2.Resume();
1.97 +
1.98 + Count=0;
1.99 + User::After(20000);
1.100 + StartSem.Signal(2);
1.101 + User::After(2000000);
1.102 + TInt count=Count;
1.103 +
1.104 + t2.Kill(0);
1.105 + User::WaitForRequest(s2);
1.106 + t1.Kill(0);
1.107 + User::WaitForRequest(s1);
1.108 + t2.Close();
1.109 + t1.Close();
1.110 + sem.Close();
1.111 +
1.112 + test.Printf(_L("%d per second\n"),count/2);
1.113 +
1.114 + test.End();
1.115 + return 0;
1.116 + }