First public contribution.
1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\bench\t_ctxsw.cpp
16 // Test and benchmark inter-thread context switch.
18 // RThread, RSemaphore, TRequestStatus.
20 // - Creates a thread with a handle to a Semaphore, check for successful
21 // creation and assign priority EPriorityLess.
22 // - Creates another thread with the handle to the thread already created,
23 // check for successful creation and assign priority EPriorityMuchLess.
24 // - Makes the two threads eligible for execution.
25 // - First thread counts the number of semaphore requests.
26 // - Second thread counts the number of times the thread is signalled on
27 // completion of request.
28 // - Terminate the threads, print the count per second.
29 // Platforms/Drives/Compatibility:
31 // Assumptions/Requirement/Pre-requisites:
32 // Failures and causes:
33 // Base Port information:
37 #define __E32TEST_EXTENSION__
40 RTest test(_L("T_CTXSW"));
45 TInt Thread1(TAny* aSemaphore)
48 s.SetHandle((TInt)aSemaphore);
49 User::WaitForAnyRequest();
53 User::WaitForAnyRequest();
58 TInt Thread2(TAny* aThread)
62 t.SetHandle((TInt)aThread);
66 TRequestStatus* ps=&s;
67 t.RequestComplete(ps,0);
71 const TInt KHeapSize=4096;
74 test.Start(_L("Test 1"));
77 TInt r = sem.CreateLocal(0);
86 r=t1.Create(_L("Thread1"),Thread1,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)sem.Handle());
93 t1.SetPriority(EPriorityLess);
94 r=t2.Create(_L("Thread2"),Thread2,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)t1.Handle());
100 t2.SetPriority(EPriorityMuchLess);
102 TRequestStatus s1,s2;
109 User::After(16000000);
111 User::WaitForRequest(s2);
113 User::WaitForRequest(s1);
117 test.Printf(_L("Ran %d times in one second.\n"),(Count1+Count2)/16); // get the value
125 TRequestStatus iStatus;
126 TRequestStatus iLogonStatus;
128 volatile TUint32 iCount;
129 SThread3Info* iOther;
133 void Thread3(TAny* aInfo)
135 SThread3Info& me = *(SThread3Info*)aInfo;
136 SThread3Info& you = *me.iOther;
139 TRequestStatus* ps = &you.iStatus;
140 you.iThread.RequestComplete(ps, 0);
141 User::WaitForAnyRequest();
149 test.Start(_L("Test 2"));
151 SThread3Info info[2];
157 info[i].iOther = &info[1-i];
158 r = info[i].iThread.Create(KNullDesC, (TThreadFunction)&Thread3, 0x1000, 0, &info[i]);
160 info[i].iThread.Logon(info[i].iLogonStatus);
161 test_Equal(KRequestPending, info[i].iLogonStatus.Int());
163 info[0].iThread.SetPriority(EPriorityMuchLess);
164 info[1].iThread.SetPriority(EPriorityLess);
165 info[0].iThread.Resume();
166 info[1].iThread.Resume();
167 User::AfterHighRes(100000);
168 TUint32 ic0 = info[0].iCount;
169 TUint32 ic1 = info[1].iCount;
170 User::AfterHighRes(10000000);
171 info[0].iThread.Kill(0);
172 info[1].iThread.Kill(0);
173 User::WaitForRequest(info[0].iLogonStatus);
174 User::WaitForRequest(info[1].iLogonStatus);
175 test_Equal(EExitKill, info[0].iThread.ExitType());
176 test_Equal(0, info[0].iThread.ExitReason());
177 test_Equal(EExitKill, info[1].iThread.ExitType());
178 test_Equal(0, info[1].iThread.ExitReason());
180 TUint32 fc0 = info[0].iCount;
181 TUint32 fc1 = info[1].iCount;
182 test.Printf(_L("T0: Initial %10u Final %10u Diff %10u PerSec %10u\n"), ic0, fc0, (fc0-ic0), (fc0-ic0)/10);
183 test.Printf(_L("T1: Initial %10u Final %10u Diff %10u PerSec %10u\n"), ic1, fc1, (fc1-ic1), (fc1-ic1)/10);
185 CLOSE_AND_WAIT(info[0].iThread);
186 CLOSE_AND_WAIT(info[1].iThread);
195 test.Start(_L("Timing inter-thread context switches..."));