0
|
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".
|
|
7 |
//
|
|
8 |
// Initial Contributors:
|
|
9 |
// Nokia Corporation - initial contribution.
|
|
10 |
//
|
|
11 |
// Contributors:
|
|
12 |
//
|
|
13 |
// Description:
|
|
14 |
// e32test\bench\t_ctxsw.cpp
|
|
15 |
// Overview:
|
|
16 |
// Test and benchmark inter-thread context switch.
|
|
17 |
// API Information:
|
|
18 |
// RThread, RSemaphore, TRequestStatus.
|
|
19 |
// Details:
|
|
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:
|
|
30 |
// All.
|
|
31 |
// Assumptions/Requirement/Pre-requisites:
|
|
32 |
// Failures and causes:
|
|
33 |
// Base Port information:
|
|
34 |
//
|
|
35 |
//
|
|
36 |
|
|
37 |
#define __E32TEST_EXTENSION__
|
|
38 |
#include <e32test.h>
|
|
39 |
|
|
40 |
RTest test(_L("T_CTXSW"));
|
|
41 |
|
|
42 |
volatile TInt Count1;
|
|
43 |
volatile TInt Count2;
|
|
44 |
|
|
45 |
TInt Thread1(TAny* aSemaphore)
|
|
46 |
{
|
|
47 |
RSemaphore s;
|
|
48 |
s.SetHandle((TInt)aSemaphore);
|
|
49 |
User::WaitForAnyRequest();
|
|
50 |
s.Signal();
|
|
51 |
FOREVER
|
|
52 |
{
|
|
53 |
User::WaitForAnyRequest();
|
|
54 |
Count1++;
|
|
55 |
}
|
|
56 |
}
|
|
57 |
|
|
58 |
TInt Thread2(TAny* aThread)
|
|
59 |
{
|
|
60 |
TRequestStatus s;
|
|
61 |
RThread t;
|
|
62 |
t.SetHandle((TInt)aThread);
|
|
63 |
FOREVER
|
|
64 |
{
|
|
65 |
Count2++;
|
|
66 |
TRequestStatus* ps=&s;
|
|
67 |
t.RequestComplete(ps,0);
|
|
68 |
}
|
|
69 |
}
|
|
70 |
|
|
71 |
const TInt KHeapSize=4096;
|
|
72 |
void Test1()
|
|
73 |
{
|
|
74 |
test.Start(_L("Test 1"));
|
|
75 |
|
|
76 |
RSemaphore sem;
|
|
77 |
TInt r = sem.CreateLocal(0);
|
|
78 |
test_KErrNone(r);
|
|
79 |
RThread t1,t2;
|
|
80 |
RThread* pt1, *pt2;
|
|
81 |
|
|
82 |
pt1 = NULL;
|
|
83 |
pt2 = NULL;
|
|
84 |
|
|
85 |
Count1=Count2=0;
|
|
86 |
r=t1.Create(_L("Thread1"),Thread1,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)sem.Handle());
|
|
87 |
if (!pt1)
|
|
88 |
pt1=&t1;
|
|
89 |
else
|
|
90 |
test(pt1==&t1);
|
|
91 |
|
|
92 |
test_KErrNone(r);
|
|
93 |
t1.SetPriority(EPriorityLess);
|
|
94 |
r=t2.Create(_L("Thread2"),Thread2,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)t1.Handle());
|
|
95 |
if (!pt2)
|
|
96 |
pt2=&t2;
|
|
97 |
else
|
|
98 |
test(pt2==&t2);
|
|
99 |
test_KErrNone(r);
|
|
100 |
t2.SetPriority(EPriorityMuchLess);
|
|
101 |
|
|
102 |
TRequestStatus s1,s2;
|
|
103 |
t1.Logon(s1);
|
|
104 |
t2.Logon(s2);
|
|
105 |
t1.Resume();
|
|
106 |
t2.Resume();
|
|
107 |
sem.Wait();
|
|
108 |
Count1=Count2=0;
|
|
109 |
User::After(16000000);
|
|
110 |
t2.Kill(0);
|
|
111 |
User::WaitForRequest(s2);
|
|
112 |
t1.Kill(0);
|
|
113 |
User::WaitForRequest(s1);
|
|
114 |
|
|
115 |
t2.Close();
|
|
116 |
t1.Close();
|
|
117 |
test.Printf(_L("Ran %d times in one second.\n"),(Count1+Count2)/16); // get the value
|
|
118 |
sem.Close();
|
|
119 |
|
|
120 |
test.End();
|
|
121 |
}
|
|
122 |
|
|
123 |
struct SThread3Info
|
|
124 |
{
|
|
125 |
TRequestStatus iStatus;
|
|
126 |
TRequestStatus iLogonStatus;
|
|
127 |
RThread iThread;
|
|
128 |
volatile TUint32 iCount;
|
|
129 |
SThread3Info* iOther;
|
|
130 |
TUint32 iPadding[9];
|
|
131 |
};
|
|
132 |
|
|
133 |
void Thread3(TAny* aInfo)
|
|
134 |
{
|
|
135 |
SThread3Info& me = *(SThread3Info*)aInfo;
|
|
136 |
SThread3Info& you = *me.iOther;
|
|
137 |
FOREVER
|
|
138 |
{
|
|
139 |
TRequestStatus* ps = &you.iStatus;
|
|
140 |
you.iThread.RequestComplete(ps, 0);
|
|
141 |
User::WaitForAnyRequest();
|
|
142 |
++me.iCount;
|
|
143 |
Count2++;
|
|
144 |
}
|
|
145 |
}
|
|
146 |
|
|
147 |
void Test2()
|
|
148 |
{
|
|
149 |
test.Start(_L("Test 2"));
|
|
150 |
|
|
151 |
SThread3Info info[2];
|
|
152 |
TInt r;
|
|
153 |
TInt i;
|
|
154 |
for (i=0; i<2; ++i)
|
|
155 |
{
|
|
156 |
info[i].iCount = 0;
|
|
157 |
info[i].iOther = &info[1-i];
|
|
158 |
r = info[i].iThread.Create(KNullDesC, (TThreadFunction)&Thread3, 0x1000, 0, &info[i]);
|
|
159 |
test_KErrNone(r);
|
|
160 |
info[i].iThread.Logon(info[i].iLogonStatus);
|
|
161 |
test_Equal(KRequestPending, info[i].iLogonStatus.Int());
|
|
162 |
}
|
|
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());
|
|
179 |
|
|
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);
|
|
184 |
|
|
185 |
CLOSE_AND_WAIT(info[0].iThread);
|
|
186 |
CLOSE_AND_WAIT(info[1].iThread);
|
|
187 |
|
|
188 |
test.End();
|
|
189 |
}
|
|
190 |
|
|
191 |
TInt E32Main()
|
|
192 |
{
|
|
193 |
|
|
194 |
test.Title();
|
|
195 |
test.Start(_L("Timing inter-thread context switches..."));
|
|
196 |
|
|
197 |
Test1();
|
|
198 |
Test2();
|
|
199 |
|
|
200 |
test.End();
|
|
201 |
return 0;
|
|
202 |
}
|