sl@0
|
1 |
// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
// e32test\misc\t_condvar2.cpp
|
sl@0
|
15 |
//
|
sl@0
|
16 |
//
|
sl@0
|
17 |
|
sl@0
|
18 |
#define __E32TEST_EXTENSION__
|
sl@0
|
19 |
|
sl@0
|
20 |
#include <e32base.h>
|
sl@0
|
21 |
#include <e32base_private.h>
|
sl@0
|
22 |
#include <e32test.h>
|
sl@0
|
23 |
#include <e32svr.h>
|
sl@0
|
24 |
#include "u32std.h"
|
sl@0
|
25 |
#include "../misc/prbs.h"
|
sl@0
|
26 |
#include "../mmu/freeram.h"
|
sl@0
|
27 |
|
sl@0
|
28 |
const TInt KStackSize=0x1000;
|
sl@0
|
29 |
|
sl@0
|
30 |
RTest test(_L("T_CONDVAR2"));
|
sl@0
|
31 |
|
sl@0
|
32 |
const TInt KPacketSize = 1024;
|
sl@0
|
33 |
const TInt KCrcSize = KPacketSize;
|
sl@0
|
34 |
struct SMessage
|
sl@0
|
35 |
{
|
sl@0
|
36 |
TDblQueLink iLink;
|
sl@0
|
37 |
TUint iSeq;
|
sl@0
|
38 |
TUint iData[KPacketSize/4];
|
sl@0
|
39 |
TUint iCrc;
|
sl@0
|
40 |
};
|
sl@0
|
41 |
|
sl@0
|
42 |
TDblQue<SMessage> Queue(0);
|
sl@0
|
43 |
TUint PSeq;
|
sl@0
|
44 |
TUint CSeq;
|
sl@0
|
45 |
RHeap* SharedHeap;
|
sl@0
|
46 |
RMutex Mutex;
|
sl@0
|
47 |
RCondVar CV;
|
sl@0
|
48 |
TInt NThreads;
|
sl@0
|
49 |
|
sl@0
|
50 |
class CThread : public CActive
|
sl@0
|
51 |
{
|
sl@0
|
52 |
public:
|
sl@0
|
53 |
CThread(TInt aPriority);
|
sl@0
|
54 |
~CThread();
|
sl@0
|
55 |
virtual void RunL();
|
sl@0
|
56 |
virtual void DoCancel();
|
sl@0
|
57 |
virtual void DisplayStats()=0;
|
sl@0
|
58 |
TInt Start();
|
sl@0
|
59 |
public:
|
sl@0
|
60 |
static TInt ThreadFunc(TAny* aPtr);
|
sl@0
|
61 |
public:
|
sl@0
|
62 |
virtual TInt StartThread()=0;
|
sl@0
|
63 |
virtual TInt RunThread()=0;
|
sl@0
|
64 |
public:
|
sl@0
|
65 |
RThread iThread;
|
sl@0
|
66 |
TInt iInnerCount;
|
sl@0
|
67 |
TInt iOuterCount;
|
sl@0
|
68 |
TInt iSuspendCount;
|
sl@0
|
69 |
TInt iExitCount;
|
sl@0
|
70 |
TInt iTerminateCount;
|
sl@0
|
71 |
TInt iCvCloseCount;
|
sl@0
|
72 |
TBool iProducer;
|
sl@0
|
73 |
TInt iId;
|
sl@0
|
74 |
TBool iCritical;
|
sl@0
|
75 |
TAny* iTempAlloc;
|
sl@0
|
76 |
};
|
sl@0
|
77 |
|
sl@0
|
78 |
class CProducerThread : public CThread
|
sl@0
|
79 |
{
|
sl@0
|
80 |
public:
|
sl@0
|
81 |
static void NewL(TInt aId);
|
sl@0
|
82 |
CProducerThread(TInt aId);
|
sl@0
|
83 |
virtual TInt StartThread();
|
sl@0
|
84 |
virtual TInt RunThread();
|
sl@0
|
85 |
virtual void DisplayStats();
|
sl@0
|
86 |
};
|
sl@0
|
87 |
|
sl@0
|
88 |
class CConsumerThread : public CThread
|
sl@0
|
89 |
{
|
sl@0
|
90 |
public:
|
sl@0
|
91 |
static void NewL(TInt aId);
|
sl@0
|
92 |
CConsumerThread(TInt aId);
|
sl@0
|
93 |
virtual TInt StartThread();
|
sl@0
|
94 |
virtual TInt RunThread();
|
sl@0
|
95 |
virtual void DisplayStats();
|
sl@0
|
96 |
};
|
sl@0
|
97 |
|
sl@0
|
98 |
class CRandomTimer : public CActive
|
sl@0
|
99 |
{
|
sl@0
|
100 |
public:
|
sl@0
|
101 |
static void NewL();
|
sl@0
|
102 |
CRandomTimer(TInt aPriority);
|
sl@0
|
103 |
~CRandomTimer();
|
sl@0
|
104 |
virtual void RunL();
|
sl@0
|
105 |
virtual void DoCancel();
|
sl@0
|
106 |
void Start();
|
sl@0
|
107 |
public:
|
sl@0
|
108 |
RTimer iTimer;
|
sl@0
|
109 |
TUint iSeed[2];
|
sl@0
|
110 |
TInt iCount;
|
sl@0
|
111 |
TInt iBackOff;
|
sl@0
|
112 |
TInt iZeroHandle;
|
sl@0
|
113 |
CThread* iSuspended;
|
sl@0
|
114 |
};
|
sl@0
|
115 |
|
sl@0
|
116 |
class CStatsTimer : public CActive
|
sl@0
|
117 |
{
|
sl@0
|
118 |
public:
|
sl@0
|
119 |
static void NewL();
|
sl@0
|
120 |
CStatsTimer(TInt aPriority);
|
sl@0
|
121 |
~CStatsTimer();
|
sl@0
|
122 |
virtual void RunL();
|
sl@0
|
123 |
virtual void DoCancel();
|
sl@0
|
124 |
void Start();
|
sl@0
|
125 |
public:
|
sl@0
|
126 |
RTimer iTimer;
|
sl@0
|
127 |
TInt iInitFreeRam;
|
sl@0
|
128 |
TInt iMaxDelta;
|
sl@0
|
129 |
TInt iCount;
|
sl@0
|
130 |
};
|
sl@0
|
131 |
|
sl@0
|
132 |
const TInt KNumProducers=4;
|
sl@0
|
133 |
CProducerThread* TheProducers[KNumProducers];
|
sl@0
|
134 |
const TInt KNumConsumers=4;
|
sl@0
|
135 |
CConsumerThread* TheConsumers[KNumConsumers];
|
sl@0
|
136 |
CRandomTimer* TheRandomTimer;
|
sl@0
|
137 |
|
sl@0
|
138 |
CThread::CThread(TInt aPriority)
|
sl@0
|
139 |
: CActive(aPriority)
|
sl@0
|
140 |
{
|
sl@0
|
141 |
}
|
sl@0
|
142 |
|
sl@0
|
143 |
CThread::~CThread()
|
sl@0
|
144 |
{
|
sl@0
|
145 |
Cancel();
|
sl@0
|
146 |
iThread.Kill(0);
|
sl@0
|
147 |
iThread.Close();
|
sl@0
|
148 |
}
|
sl@0
|
149 |
|
sl@0
|
150 |
TInt StartAllThreads()
|
sl@0
|
151 |
{
|
sl@0
|
152 |
TInt i;
|
sl@0
|
153 |
TInt r = CV.CreateLocal();
|
sl@0
|
154 |
if (r!=KErrNone)
|
sl@0
|
155 |
return r;
|
sl@0
|
156 |
for (i=0; i<KNumConsumers; ++i)
|
sl@0
|
157 |
{
|
sl@0
|
158 |
r = TheConsumers[i]->Start();
|
sl@0
|
159 |
if (r!=KErrNone)
|
sl@0
|
160 |
return r;
|
sl@0
|
161 |
}
|
sl@0
|
162 |
for (i=0; i<KNumProducers; ++i)
|
sl@0
|
163 |
{
|
sl@0
|
164 |
r = TheProducers[i]->Start();
|
sl@0
|
165 |
if (r!=KErrNone)
|
sl@0
|
166 |
return r;
|
sl@0
|
167 |
}
|
sl@0
|
168 |
return KErrNone;
|
sl@0
|
169 |
}
|
sl@0
|
170 |
|
sl@0
|
171 |
void CThread::RunL()
|
sl@0
|
172 |
{
|
sl@0
|
173 |
TExitType exitType = iThread.ExitType();
|
sl@0
|
174 |
TInt exitReason = iThread.ExitReason();
|
sl@0
|
175 |
const TDesC& exitCat = iThread.ExitCategory();
|
sl@0
|
176 |
TBool bad=EFalse;
|
sl@0
|
177 |
if (exitType==EExitKill)
|
sl@0
|
178 |
{
|
sl@0
|
179 |
if (exitReason!=KErrNone && exitReason!=KErrGeneral)
|
sl@0
|
180 |
bad=ETrue;
|
sl@0
|
181 |
}
|
sl@0
|
182 |
else if (exitType==EExitPanic)
|
sl@0
|
183 |
{
|
sl@0
|
184 |
if (exitCat!=_L("KERN-EXEC") || exitReason!=0 || CV.Handle()!=0)
|
sl@0
|
185 |
bad=ETrue;
|
sl@0
|
186 |
else
|
sl@0
|
187 |
++iCvCloseCount;
|
sl@0
|
188 |
}
|
sl@0
|
189 |
if (bad)
|
sl@0
|
190 |
{
|
sl@0
|
191 |
TFullName n(iThread.FullName());
|
sl@0
|
192 |
if (iProducer)
|
sl@0
|
193 |
test.Printf(_L("Thread %S (P%1d) exited %d,%d,%S\n"),&n,iId,exitType,exitReason,&exitCat);
|
sl@0
|
194 |
else
|
sl@0
|
195 |
test.Printf(_L("Thread %S (C%1d) exited %d,%d,%S\n"),&n,iId,exitType,exitReason,&exitCat);
|
sl@0
|
196 |
CActiveScheduler::Stop();
|
sl@0
|
197 |
return;
|
sl@0
|
198 |
}
|
sl@0
|
199 |
CLOSE_AND_WAIT(iThread);
|
sl@0
|
200 |
if (exitType==EExitTerminate)
|
sl@0
|
201 |
++iTerminateCount;
|
sl@0
|
202 |
else if (exitType==EExitKill && exitReason==KErrNone)
|
sl@0
|
203 |
++iExitCount;
|
sl@0
|
204 |
else if (exitType==EExitKill && exitReason==KErrGeneral)
|
sl@0
|
205 |
++iCvCloseCount;
|
sl@0
|
206 |
--NThreads;
|
sl@0
|
207 |
if (iTempAlloc)
|
sl@0
|
208 |
{
|
sl@0
|
209 |
SharedHeap->Free(iTempAlloc);
|
sl@0
|
210 |
iTempAlloc = NULL;
|
sl@0
|
211 |
}
|
sl@0
|
212 |
TInt r;
|
sl@0
|
213 |
if (CV.Handle()==0)
|
sl@0
|
214 |
{
|
sl@0
|
215 |
if (NThreads==0)
|
sl@0
|
216 |
r = StartAllThreads();
|
sl@0
|
217 |
else
|
sl@0
|
218 |
return;
|
sl@0
|
219 |
}
|
sl@0
|
220 |
else
|
sl@0
|
221 |
r=Start();
|
sl@0
|
222 |
if (r!=KErrNone)
|
sl@0
|
223 |
{
|
sl@0
|
224 |
test.Printf(_L("Start thread error %d\n"),r);
|
sl@0
|
225 |
CActiveScheduler::Stop();
|
sl@0
|
226 |
}
|
sl@0
|
227 |
}
|
sl@0
|
228 |
|
sl@0
|
229 |
void CThread::DoCancel()
|
sl@0
|
230 |
{
|
sl@0
|
231 |
iThread.LogonCancel(iStatus);
|
sl@0
|
232 |
}
|
sl@0
|
233 |
|
sl@0
|
234 |
TInt CThread::Start()
|
sl@0
|
235 |
{
|
sl@0
|
236 |
TInt r;
|
sl@0
|
237 |
FOREVER
|
sl@0
|
238 |
{
|
sl@0
|
239 |
r=StartThread();
|
sl@0
|
240 |
if (r==KErrNone)
|
sl@0
|
241 |
break;
|
sl@0
|
242 |
if (r!=KErrAlreadyExists)
|
sl@0
|
243 |
break;
|
sl@0
|
244 |
User::After(100000);
|
sl@0
|
245 |
}
|
sl@0
|
246 |
if (r==KErrNone)
|
sl@0
|
247 |
{
|
sl@0
|
248 |
iThread.Logon(iStatus);
|
sl@0
|
249 |
SetActive();
|
sl@0
|
250 |
}
|
sl@0
|
251 |
++NThreads;
|
sl@0
|
252 |
return r;
|
sl@0
|
253 |
}
|
sl@0
|
254 |
|
sl@0
|
255 |
TInt CThread::ThreadFunc(TAny* aPtr)
|
sl@0
|
256 |
{
|
sl@0
|
257 |
return ((CThread*)aPtr)->RunThread();
|
sl@0
|
258 |
}
|
sl@0
|
259 |
|
sl@0
|
260 |
CConsumerThread::CConsumerThread(TInt aId)
|
sl@0
|
261 |
: CThread(0)
|
sl@0
|
262 |
{
|
sl@0
|
263 |
iId = aId;
|
sl@0
|
264 |
}
|
sl@0
|
265 |
|
sl@0
|
266 |
void CConsumerThread::NewL(TInt aId)
|
sl@0
|
267 |
{
|
sl@0
|
268 |
CConsumerThread* pT=new (ELeave) CConsumerThread(aId);
|
sl@0
|
269 |
TheConsumers[aId] = pT;
|
sl@0
|
270 |
CActiveScheduler::Add(pT);
|
sl@0
|
271 |
User::LeaveIfError(pT->Start());
|
sl@0
|
272 |
}
|
sl@0
|
273 |
|
sl@0
|
274 |
TInt CConsumerThread::StartThread()
|
sl@0
|
275 |
{
|
sl@0
|
276 |
TInt r=iThread.Create(KNullDesC(), &ThreadFunc, KStackSize, SharedHeap, this); // use unnamed thread
|
sl@0
|
277 |
if (r!=KErrNone)
|
sl@0
|
278 |
return r;
|
sl@0
|
279 |
iThread.Resume();
|
sl@0
|
280 |
return KErrNone;
|
sl@0
|
281 |
}
|
sl@0
|
282 |
|
sl@0
|
283 |
void CConsumerThread::DisplayStats()
|
sl@0
|
284 |
{
|
sl@0
|
285 |
test.Printf(_L("C%1d: I:%9d O:%9d S:%9d T:%9d C:%9d\n"), iId, iInnerCount, iOuterCount, iSuspendCount, iTerminateCount, iCvCloseCount);
|
sl@0
|
286 |
}
|
sl@0
|
287 |
|
sl@0
|
288 |
TInt CConsumerThread::RunThread()
|
sl@0
|
289 |
{
|
sl@0
|
290 |
Mutex.Wait();
|
sl@0
|
291 |
TInt r = KErrNone;
|
sl@0
|
292 |
FOREVER
|
sl@0
|
293 |
{
|
sl@0
|
294 |
while (Queue.IsEmpty())
|
sl@0
|
295 |
{
|
sl@0
|
296 |
r = CV.Wait(Mutex);
|
sl@0
|
297 |
++iInnerCount;
|
sl@0
|
298 |
if (r!=KErrNone)
|
sl@0
|
299 |
return r;
|
sl@0
|
300 |
}
|
sl@0
|
301 |
++iOuterCount;
|
sl@0
|
302 |
iCritical = ETrue;
|
sl@0
|
303 |
SMessage* m = Queue.First();
|
sl@0
|
304 |
m->iLink.Deque();
|
sl@0
|
305 |
iTempAlloc = m;
|
sl@0
|
306 |
TBool seq_ok = (m->iSeq == CSeq++);
|
sl@0
|
307 |
iCritical = EFalse;
|
sl@0
|
308 |
Mutex.Signal();
|
sl@0
|
309 |
if (!seq_ok)
|
sl@0
|
310 |
return KErrCorrupt;
|
sl@0
|
311 |
TUint16 crc = 0;
|
sl@0
|
312 |
Mem::Crc(crc, m->iData, KCrcSize);
|
sl@0
|
313 |
if (crc != m->iCrc)
|
sl@0
|
314 |
return KErrCorrupt;
|
sl@0
|
315 |
iCritical = ETrue;
|
sl@0
|
316 |
iTempAlloc = NULL;
|
sl@0
|
317 |
User::Free(m);
|
sl@0
|
318 |
iCritical = EFalse;
|
sl@0
|
319 |
Mutex.Wait();
|
sl@0
|
320 |
}
|
sl@0
|
321 |
}
|
sl@0
|
322 |
|
sl@0
|
323 |
CProducerThread::CProducerThread(TInt aId)
|
sl@0
|
324 |
: CThread(0)
|
sl@0
|
325 |
{
|
sl@0
|
326 |
iId = aId;
|
sl@0
|
327 |
}
|
sl@0
|
328 |
|
sl@0
|
329 |
void CProducerThread::NewL(TInt aId)
|
sl@0
|
330 |
{
|
sl@0
|
331 |
CProducerThread* pT=new (ELeave) CProducerThread(aId);
|
sl@0
|
332 |
TheProducers[aId] = pT;
|
sl@0
|
333 |
CActiveScheduler::Add(pT);
|
sl@0
|
334 |
User::LeaveIfError(pT->Start());
|
sl@0
|
335 |
}
|
sl@0
|
336 |
|
sl@0
|
337 |
TInt CProducerThread::StartThread()
|
sl@0
|
338 |
{
|
sl@0
|
339 |
TInt r=iThread.Create(KNullDesC(), &ThreadFunc, KStackSize, SharedHeap, this); // use unnamed thread
|
sl@0
|
340 |
if (r!=KErrNone)
|
sl@0
|
341 |
return r;
|
sl@0
|
342 |
iThread.Resume();
|
sl@0
|
343 |
return KErrNone;
|
sl@0
|
344 |
}
|
sl@0
|
345 |
|
sl@0
|
346 |
void CProducerThread::DisplayStats()
|
sl@0
|
347 |
{
|
sl@0
|
348 |
test.Printf(_L("P%1d: I:%9d O:%9d S:%9d T:%9d C:%9d\n"), iId, iInnerCount, iOuterCount, iSuspendCount, iTerminateCount, iCvCloseCount);
|
sl@0
|
349 |
}
|
sl@0
|
350 |
|
sl@0
|
351 |
TInt CProducerThread::RunThread()
|
sl@0
|
352 |
{
|
sl@0
|
353 |
TUint seed[2];
|
sl@0
|
354 |
seed[0] = User::TickCount();
|
sl@0
|
355 |
seed[1] = 0;
|
sl@0
|
356 |
FOREVER
|
sl@0
|
357 |
{
|
sl@0
|
358 |
iCritical = ETrue;
|
sl@0
|
359 |
SMessage* m = new SMessage;
|
sl@0
|
360 |
iTempAlloc = m;
|
sl@0
|
361 |
iCritical = EFalse;
|
sl@0
|
362 |
TInt i = 0;
|
sl@0
|
363 |
for (; i<KPacketSize/4; ++i)
|
sl@0
|
364 |
m->iData[i] = Random(seed);
|
sl@0
|
365 |
TUint16 crc = 0;
|
sl@0
|
366 |
Mem::Crc(crc, m->iData, KCrcSize);
|
sl@0
|
367 |
m->iCrc = crc;
|
sl@0
|
368 |
Mutex.Wait();
|
sl@0
|
369 |
iCritical = ETrue;
|
sl@0
|
370 |
m->iSeq = PSeq++;
|
sl@0
|
371 |
Queue.AddLast(*m);
|
sl@0
|
372 |
iTempAlloc = NULL;
|
sl@0
|
373 |
iCritical = EFalse;
|
sl@0
|
374 |
CV.Signal();
|
sl@0
|
375 |
Mutex.Signal();
|
sl@0
|
376 |
++iOuterCount;
|
sl@0
|
377 |
if (!(Random(seed)&1))
|
sl@0
|
378 |
User::AfterHighRes(1000);
|
sl@0
|
379 |
}
|
sl@0
|
380 |
}
|
sl@0
|
381 |
|
sl@0
|
382 |
void CRandomTimer::NewL()
|
sl@0
|
383 |
{
|
sl@0
|
384 |
CRandomTimer* pR=new (ELeave) CRandomTimer(20);
|
sl@0
|
385 |
User::LeaveIfError(pR->iTimer.CreateLocal());
|
sl@0
|
386 |
CActiveScheduler::Add(pR);
|
sl@0
|
387 |
TheRandomTimer=pR;
|
sl@0
|
388 |
pR->Start();
|
sl@0
|
389 |
}
|
sl@0
|
390 |
|
sl@0
|
391 |
CRandomTimer::CRandomTimer(TInt aPriority)
|
sl@0
|
392 |
: CActive(aPriority)
|
sl@0
|
393 |
{
|
sl@0
|
394 |
iSeed[0]=User::TickCount();
|
sl@0
|
395 |
}
|
sl@0
|
396 |
|
sl@0
|
397 |
CRandomTimer::~CRandomTimer()
|
sl@0
|
398 |
{
|
sl@0
|
399 |
Cancel();
|
sl@0
|
400 |
iTimer.Close();
|
sl@0
|
401 |
}
|
sl@0
|
402 |
|
sl@0
|
403 |
void CRandomTimer::RunL()
|
sl@0
|
404 |
{
|
sl@0
|
405 |
++iCount;
|
sl@0
|
406 |
FOREVER
|
sl@0
|
407 |
{
|
sl@0
|
408 |
TUint x=Random(iSeed)&511;
|
sl@0
|
409 |
TInt tn=(TInt)(Random(iSeed) % (KNumConsumers + KNumProducers));
|
sl@0
|
410 |
CThread* pT = (tn>=KNumConsumers) ? (CThread*)TheProducers[tn-KNumConsumers] : (CThread*)TheConsumers[tn];
|
sl@0
|
411 |
if (x==511)
|
sl@0
|
412 |
{
|
sl@0
|
413 |
CV.Close();
|
sl@0
|
414 |
if (iSuspended)
|
sl@0
|
415 |
{
|
sl@0
|
416 |
if (iSuspended->iThread.Handle())
|
sl@0
|
417 |
iSuspended->iThread.Resume();
|
sl@0
|
418 |
iSuspended = NULL;
|
sl@0
|
419 |
}
|
sl@0
|
420 |
break;
|
sl@0
|
421 |
}
|
sl@0
|
422 |
if (pT->iThread.Handle()==0)
|
sl@0
|
423 |
{
|
sl@0
|
424 |
++iZeroHandle;
|
sl@0
|
425 |
continue;
|
sl@0
|
426 |
}
|
sl@0
|
427 |
if (x>=500)
|
sl@0
|
428 |
{
|
sl@0
|
429 |
if (pT->iCritical)
|
sl@0
|
430 |
{
|
sl@0
|
431 |
++iBackOff;
|
sl@0
|
432 |
continue;
|
sl@0
|
433 |
}
|
sl@0
|
434 |
pT->iThread.Terminate(0);
|
sl@0
|
435 |
if (iSuspended == pT)
|
sl@0
|
436 |
iSuspended = NULL;
|
sl@0
|
437 |
break;
|
sl@0
|
438 |
}
|
sl@0
|
439 |
if (iSuspended && (x&1))
|
sl@0
|
440 |
{
|
sl@0
|
441 |
if (iSuspended->iThread.Handle())
|
sl@0
|
442 |
iSuspended->iThread.Resume();
|
sl@0
|
443 |
iSuspended = NULL;
|
sl@0
|
444 |
}
|
sl@0
|
445 |
if (!iSuspended && !(x&15))
|
sl@0
|
446 |
{
|
sl@0
|
447 |
iSuspended = pT;
|
sl@0
|
448 |
pT->iThread.Suspend();
|
sl@0
|
449 |
++pT->iSuspendCount;
|
sl@0
|
450 |
}
|
sl@0
|
451 |
TThreadPriority tp;
|
sl@0
|
452 |
if (x>=400)
|
sl@0
|
453 |
tp = EPriorityMuchMore;
|
sl@0
|
454 |
else if (x>=300)
|
sl@0
|
455 |
tp = EPriorityMore;
|
sl@0
|
456 |
else if (x>=200)
|
sl@0
|
457 |
tp = EPriorityNormal;
|
sl@0
|
458 |
else if (x>=100)
|
sl@0
|
459 |
tp = EPriorityLess;
|
sl@0
|
460 |
else
|
sl@0
|
461 |
tp = EPriorityMuchLess;
|
sl@0
|
462 |
pT->iThread.SetPriority(tp);
|
sl@0
|
463 |
break;
|
sl@0
|
464 |
}
|
sl@0
|
465 |
Start();
|
sl@0
|
466 |
}
|
sl@0
|
467 |
|
sl@0
|
468 |
void CRandomTimer::Start()
|
sl@0
|
469 |
{
|
sl@0
|
470 |
TUint x=Random(iSeed)&15;
|
sl@0
|
471 |
x+=1;
|
sl@0
|
472 |
iTimer.HighRes(iStatus, x*1000);
|
sl@0
|
473 |
SetActive();
|
sl@0
|
474 |
}
|
sl@0
|
475 |
|
sl@0
|
476 |
void CRandomTimer::DoCancel()
|
sl@0
|
477 |
{
|
sl@0
|
478 |
iTimer.Cancel();
|
sl@0
|
479 |
}
|
sl@0
|
480 |
|
sl@0
|
481 |
void CStatsTimer::NewL()
|
sl@0
|
482 |
{
|
sl@0
|
483 |
CStatsTimer* pT=new (ELeave) CStatsTimer(-10);
|
sl@0
|
484 |
User::LeaveIfError(pT->iTimer.CreateLocal());
|
sl@0
|
485 |
CActiveScheduler::Add(pT);
|
sl@0
|
486 |
pT->Start();
|
sl@0
|
487 |
}
|
sl@0
|
488 |
|
sl@0
|
489 |
CStatsTimer::CStatsTimer(TInt aPriority)
|
sl@0
|
490 |
: CActive(aPriority)
|
sl@0
|
491 |
{
|
sl@0
|
492 |
iInitFreeRam = FreeRam();
|
sl@0
|
493 |
}
|
sl@0
|
494 |
|
sl@0
|
495 |
CStatsTimer::~CStatsTimer()
|
sl@0
|
496 |
{
|
sl@0
|
497 |
Cancel();
|
sl@0
|
498 |
iTimer.Close();
|
sl@0
|
499 |
}
|
sl@0
|
500 |
|
sl@0
|
501 |
void CStatsTimer::RunL()
|
sl@0
|
502 |
{
|
sl@0
|
503 |
TInt i;
|
sl@0
|
504 |
for (i=0; i<KNumProducers; i++)
|
sl@0
|
505 |
TheProducers[i]->DisplayStats();
|
sl@0
|
506 |
for (i=0; i<KNumConsumers; i++)
|
sl@0
|
507 |
TheConsumers[i]->DisplayStats();
|
sl@0
|
508 |
test.Printf(_L("RndTm: %9d BO: %9d ZH: %9d\n"), TheRandomTimer->iCount, TheRandomTimer->iBackOff, TheRandomTimer->iZeroHandle);
|
sl@0
|
509 |
TInt free_ram = FreeRam();
|
sl@0
|
510 |
TInt delta_ram = iInitFreeRam - free_ram;
|
sl@0
|
511 |
if (delta_ram > iMaxDelta)
|
sl@0
|
512 |
iMaxDelta = delta_ram;
|
sl@0
|
513 |
if (++iCount==10)
|
sl@0
|
514 |
{
|
sl@0
|
515 |
test.Printf(_L("Max RAM delta %dK Free RAM %08x\n"), iMaxDelta/1024, free_ram);
|
sl@0
|
516 |
iCount=0;
|
sl@0
|
517 |
}
|
sl@0
|
518 |
Start();
|
sl@0
|
519 |
}
|
sl@0
|
520 |
|
sl@0
|
521 |
void CStatsTimer::Start()
|
sl@0
|
522 |
{
|
sl@0
|
523 |
iTimer.After(iStatus, 1000000);
|
sl@0
|
524 |
SetActive();
|
sl@0
|
525 |
}
|
sl@0
|
526 |
|
sl@0
|
527 |
void CStatsTimer::DoCancel()
|
sl@0
|
528 |
{
|
sl@0
|
529 |
iTimer.Cancel();
|
sl@0
|
530 |
}
|
sl@0
|
531 |
|
sl@0
|
532 |
_LIT(KSharedHeap, "SharedHeap");
|
sl@0
|
533 |
void InitialiseL()
|
sl@0
|
534 |
{
|
sl@0
|
535 |
PSeq = 0;
|
sl@0
|
536 |
CSeq = 0;
|
sl@0
|
537 |
User::LeaveIfError(Mutex.CreateLocal());
|
sl@0
|
538 |
User::LeaveIfError(CV.CreateLocal());
|
sl@0
|
539 |
User::LeaveIfNull(SharedHeap = UserHeap::ChunkHeap(&KSharedHeap, 0x1000, 0x01000000));
|
sl@0
|
540 |
CActiveScheduler* pA=new (ELeave) CActiveScheduler;
|
sl@0
|
541 |
CActiveScheduler::Install(pA);
|
sl@0
|
542 |
TInt id;
|
sl@0
|
543 |
for (id=0; id<KNumConsumers; ++id)
|
sl@0
|
544 |
CConsumerThread::NewL(id);
|
sl@0
|
545 |
for (id=0; id<KNumProducers; ++id)
|
sl@0
|
546 |
CProducerThread::NewL(id);
|
sl@0
|
547 |
CRandomTimer::NewL();
|
sl@0
|
548 |
CStatsTimer::NewL();
|
sl@0
|
549 |
}
|
sl@0
|
550 |
|
sl@0
|
551 |
GLDEF_C TInt E32Main()
|
sl@0
|
552 |
//
|
sl@0
|
553 |
// Test timers.
|
sl@0
|
554 |
//
|
sl@0
|
555 |
{
|
sl@0
|
556 |
|
sl@0
|
557 |
test.Title();
|
sl@0
|
558 |
|
sl@0
|
559 |
RThread().SetPriority(EPriorityAbsoluteHigh);
|
sl@0
|
560 |
|
sl@0
|
561 |
TRAPD(r,InitialiseL());
|
sl@0
|
562 |
test(r==KErrNone);
|
sl@0
|
563 |
|
sl@0
|
564 |
User::SetJustInTime(EFalse);
|
sl@0
|
565 |
|
sl@0
|
566 |
CActiveScheduler::Start();
|
sl@0
|
567 |
|
sl@0
|
568 |
test(0);
|
sl@0
|
569 |
|
sl@0
|
570 |
return(0);
|
sl@0
|
571 |
}
|
sl@0
|
572 |
|