0
|
1 |
// Copyright (c) 1995-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\prime\t_semutx.cpp
|
|
15 |
// Tests the RSemaphore, RMutex and RCriticalSection classes
|
|
16 |
// Overview:
|
|
17 |
// Tests the RSemaphore, RMutex and RCriticalSection classes
|
|
18 |
// API Information:
|
|
19 |
// RSemaphore, RMutex, RCriticalSection
|
|
20 |
// Details:
|
|
21 |
// - Test RSemaphore and RMutex with the producer/consumer scenario.
|
|
22 |
// Create two threads, use signal and wait to coordinate the
|
|
23 |
// threads. Verify results are as expected.
|
|
24 |
// - Calculate the time required to create, resume and close a thread.
|
|
25 |
// - Test RSemaphore::Wait(timeout) in a variety ways and timeout
|
|
26 |
// values. Verify results are as expected.
|
|
27 |
// - Test RMutex via two threads which write to an array. The writing
|
|
28 |
// and updating of the index is wrapped within a mutex pair. Verify
|
|
29 |
// results are as expected.
|
|
30 |
// - Test RCriticalSection via two threads which write to an array. The
|
|
31 |
// writing and updating of the index is wrapped within a critical section
|
|
32 |
// pair. Verify results are as expected.
|
|
33 |
// Platforms/Drives/Compatibility:
|
|
34 |
// All.
|
|
35 |
// Assumptions/Requirement/Pre-requisites:
|
|
36 |
// Failures and causes:
|
|
37 |
// Base Port information:
|
|
38 |
//
|
|
39 |
//
|
|
40 |
|
132
|
41 |
#define __E32TEST_EXTENSION__
|
0
|
42 |
#include <e32test.h>
|
132
|
43 |
#include <u32std.h>
|
|
44 |
#include <e32svr.h>
|
0
|
45 |
|
|
46 |
const TInt KMaxBufferSize=10;
|
|
47 |
const TInt KMaxArraySize=10;
|
|
48 |
const TInt KNumProducerItems=100;
|
|
49 |
|
|
50 |
enum {EThread1ID=1,EThread2ID};
|
|
51 |
|
|
52 |
RTest test(_L("T_SEMUTX"));
|
|
53 |
RMutex mutex;
|
|
54 |
RCriticalSection criticalSn;
|
|
55 |
TInt thread1Count,thread2Count;
|
|
56 |
TInt arrayIndex;
|
|
57 |
TInt array[KMaxArraySize];
|
|
58 |
TInt consumerArray[KNumProducerItems];
|
|
59 |
RSemaphore slotAvailable,itemAvailable;
|
|
60 |
|
|
61 |
class CStack
|
|
62 |
{
|
|
63 |
public:
|
|
64 |
CStack() {iCount=0;};
|
|
65 |
void Push(TInt aItem) {iStack[iCount++]=aItem;};
|
|
66 |
TInt Pop(void) {return(iStack[--iCount]);};
|
|
67 |
private:
|
|
68 |
TInt iStack[KMaxBufferSize];
|
|
69 |
TInt iCount;
|
|
70 |
};
|
|
71 |
CStack stack;
|
|
72 |
|
|
73 |
|
|
74 |
TInt Producer(TAny*)
|
|
75 |
{
|
|
76 |
for(TInt ii=0;ii<KNumProducerItems;ii++)
|
|
77 |
{
|
|
78 |
slotAvailable.Wait();
|
|
79 |
mutex.Wait();
|
|
80 |
stack.Push(ii);
|
|
81 |
mutex.Signal();
|
|
82 |
itemAvailable.Signal();
|
|
83 |
}
|
|
84 |
return(KErrNone);
|
|
85 |
}
|
|
86 |
|
|
87 |
TInt Consumer(TAny*)
|
|
88 |
{
|
|
89 |
TInt item;
|
|
90 |
for(TInt ii=0;ii<KNumProducerItems;ii++)
|
|
91 |
{
|
|
92 |
itemAvailable.Wait();
|
|
93 |
mutex.Wait();
|
|
94 |
item=stack.Pop();
|
|
95 |
mutex.Signal();
|
|
96 |
slotAvailable.Signal();
|
|
97 |
consumerArray[item]=item;
|
|
98 |
}
|
|
99 |
return(KErrNone);
|
|
100 |
}
|
|
101 |
|
|
102 |
void BusyWait(TInt aMicroseconds)
|
|
103 |
{
|
|
104 |
TTime begin;
|
|
105 |
begin.HomeTime();
|
|
106 |
FOREVER
|
|
107 |
{
|
|
108 |
TTime now;
|
|
109 |
now.HomeTime();
|
|
110 |
TTimeIntervalMicroSeconds iv=now.MicroSecondsFrom(begin);
|
|
111 |
if (iv.Int64()>=TInt64(aMicroseconds))
|
|
112 |
return;
|
|
113 |
}
|
|
114 |
}
|
|
115 |
|
|
116 |
TInt MutexThreadEntryPoint1(TAny*)
|
|
117 |
//
|
|
118 |
// Mutex test thread 1
|
|
119 |
//
|
|
120 |
{
|
|
121 |
|
|
122 |
thread1Count=0;
|
|
123 |
TBool running=ETrue;
|
|
124 |
do
|
|
125 |
{
|
|
126 |
mutex.Wait();
|
|
127 |
BusyWait(100000);
|
|
128 |
if (arrayIndex<KMaxArraySize)
|
|
129 |
{
|
|
130 |
array[arrayIndex++]=EThread1ID;
|
|
131 |
thread1Count++;
|
|
132 |
}
|
|
133 |
else
|
|
134 |
running=EFalse;
|
|
135 |
mutex.Signal();
|
|
136 |
} while (running);
|
|
137 |
return(KErrNone);
|
|
138 |
}
|
|
139 |
|
|
140 |
TInt MutexThreadEntryPoint2(TAny*)
|
|
141 |
//
|
|
142 |
// Mutex test thread 2
|
|
143 |
//
|
|
144 |
{
|
|
145 |
|
|
146 |
thread2Count=0;
|
|
147 |
TBool running=ETrue;
|
|
148 |
do
|
|
149 |
{
|
|
150 |
mutex.Wait();
|
|
151 |
BusyWait(200000);
|
|
152 |
if (arrayIndex<KMaxArraySize)
|
|
153 |
{
|
|
154 |
array[arrayIndex++]=EThread2ID;
|
|
155 |
thread2Count++;
|
|
156 |
}
|
|
157 |
else
|
|
158 |
running=EFalse;
|
|
159 |
mutex.Signal();
|
|
160 |
} while (running);
|
|
161 |
return(KErrNone);
|
|
162 |
}
|
|
163 |
|
|
164 |
TInt CriticalSnThreadEntryPoint1(TAny*)
|
|
165 |
//
|
|
166 |
// Critical Section test thread 1
|
|
167 |
//
|
|
168 |
{
|
|
169 |
|
|
170 |
thread1Count=0;
|
|
171 |
TBool running=ETrue;
|
|
172 |
do
|
|
173 |
{
|
|
174 |
criticalSn.Wait();
|
|
175 |
User::After(100000);
|
|
176 |
if (arrayIndex<KMaxArraySize)
|
|
177 |
{
|
|
178 |
array[arrayIndex++]=EThread1ID;
|
|
179 |
thread1Count++;
|
|
180 |
}
|
|
181 |
else
|
|
182 |
running=EFalse;
|
|
183 |
criticalSn.Signal();
|
|
184 |
} while (running);
|
|
185 |
return(KErrNone);
|
|
186 |
}
|
|
187 |
|
|
188 |
TInt CriticalSnThreadEntryPoint2(TAny*)
|
|
189 |
//
|
|
190 |
// Critical Section test thread 2
|
|
191 |
//
|
|
192 |
{
|
|
193 |
|
|
194 |
thread2Count=0;
|
|
195 |
TBool running=ETrue;
|
|
196 |
do
|
|
197 |
{
|
|
198 |
criticalSn.Wait();
|
|
199 |
User::After(200000);
|
|
200 |
if (arrayIndex<KMaxArraySize)
|
|
201 |
{
|
|
202 |
array[arrayIndex++]=EThread2ID;
|
|
203 |
thread2Count++;
|
|
204 |
}
|
|
205 |
else
|
|
206 |
running=EFalse;
|
|
207 |
criticalSn.Signal();
|
|
208 |
} while (running);
|
|
209 |
return(KErrNone);
|
|
210 |
}
|
|
211 |
|
|
212 |
struct SWaitSem
|
|
213 |
{
|
|
214 |
RSemaphore iSem;
|
|
215 |
TInt iTimeout;
|
|
216 |
};
|
|
217 |
|
|
218 |
TInt WaitSemThread(TAny* a)
|
|
219 |
{
|
|
220 |
SWaitSem& ws = *(SWaitSem*)a;
|
|
221 |
return ws.iSem.Wait(ws.iTimeout);
|
|
222 |
}
|
|
223 |
|
|
224 |
void StartWaitSemThread(RThread& aT, SWaitSem& aW, TThreadPriority aP=EPriorityLess)
|
|
225 |
{
|
|
226 |
TInt r = aT.Create(KNullDesC, &WaitSemThread, 0x1000, 0x1000, 0x1000, &aW);
|
132
|
227 |
test_KErrNone(r);
|
0
|
228 |
aT.SetPriority(aP);
|
|
229 |
aT.Resume();
|
|
230 |
}
|
|
231 |
|
|
232 |
void WaitForWaitSemThread(RThread& aT, TInt aResult)
|
|
233 |
{
|
|
234 |
TRequestStatus s;
|
|
235 |
aT.Logon(s);
|
|
236 |
User::WaitForRequest(s);
|
132
|
237 |
test_Equal(EExitKill, aT.ExitType());
|
|
238 |
test_Equal(aResult, aT.ExitReason());
|
|
239 |
test_Equal(aResult, s.Int());
|
0
|
240 |
CLOSE_AND_WAIT(aT);
|
|
241 |
}
|
|
242 |
|
|
243 |
TInt DummyThread(TAny*)
|
|
244 |
{
|
|
245 |
return 0;
|
|
246 |
}
|
|
247 |
|
|
248 |
void TestSemaphore2()
|
|
249 |
{
|
|
250 |
test.Start(_L("Test semaphore wait with timeout"));
|
|
251 |
SWaitSem ws;
|
|
252 |
RThread t;
|
|
253 |
TTime initial;
|
|
254 |
TTime final;
|
|
255 |
TInt elapsed=0;
|
|
256 |
TInt r = ws.iSem.CreateLocal(0);
|
132
|
257 |
test_KErrNone(r);
|
0
|
258 |
|
|
259 |
RThread().SetPriority(EPriorityAbsoluteVeryLow);
|
|
260 |
TInt threadcount=0;
|
|
261 |
initial.HomeTime();
|
|
262 |
while (elapsed<1000000)
|
|
263 |
{
|
|
264 |
r = t.Create(KNullDesC, &DummyThread, 0x1000, NULL, NULL);
|
132
|
265 |
test_KErrNone(r);
|
0
|
266 |
t.SetPriority(EPriorityMore);
|
|
267 |
t.Resume();
|
|
268 |
t.Close();
|
|
269 |
++threadcount;
|
|
270 |
final.HomeTime();
|
|
271 |
elapsed = I64INT(final.Int64()-initial.Int64());
|
|
272 |
}
|
|
273 |
RThread().SetPriority(EPriorityNormal);
|
|
274 |
test.Printf(_L("%d threads in 1 sec\n"),threadcount);
|
|
275 |
TInt overhead = 1000000/threadcount;
|
|
276 |
test.Printf(_L("overhead = %dus\n"),overhead);
|
|
277 |
|
|
278 |
ws.iTimeout=1000000;
|
|
279 |
initial.HomeTime();
|
|
280 |
StartWaitSemThread(t, ws);
|
|
281 |
WaitForWaitSemThread(t, KErrTimedOut);
|
|
282 |
final.HomeTime();
|
|
283 |
elapsed = I64INT(final.Int64()-initial.Int64());
|
|
284 |
test.Printf(_L("Time taken = %dus\n"), elapsed);
|
|
285 |
test(elapsed>=900000+overhead && elapsed<1500000+overhead);
|
|
286 |
|
|
287 |
ws.iTimeout=-1;
|
|
288 |
initial.HomeTime();
|
|
289 |
StartWaitSemThread(t, ws);
|
|
290 |
WaitForWaitSemThread(t, KErrArgument);
|
|
291 |
final.HomeTime();
|
|
292 |
elapsed = I64INT(final.Int64()-initial.Int64());
|
|
293 |
test.Printf(_L("Time taken = %dus\n"), elapsed);
|
|
294 |
|
|
295 |
ws.iTimeout=2000000;
|
|
296 |
initial.HomeTime();
|
|
297 |
StartWaitSemThread(t, ws);
|
|
298 |
User::After(1000000);
|
|
299 |
ws.iSem.Signal();
|
|
300 |
WaitForWaitSemThread(t, KErrNone);
|
|
301 |
final.HomeTime();
|
|
302 |
elapsed = I64INT(final.Int64()-initial.Int64());
|
|
303 |
test.Printf(_L("Time taken = %dus\n"), elapsed);
|
|
304 |
test(elapsed>=900000+overhead && elapsed<1500000+overhead);
|
|
305 |
|
|
306 |
ws.iTimeout=100000;
|
|
307 |
StartWaitSemThread(t, ws, EPriorityMore);
|
|
308 |
t.Suspend();
|
|
309 |
ws.iSem.Signal();
|
|
310 |
User::After(200000);
|
|
311 |
t.Resume();
|
|
312 |
WaitForWaitSemThread(t, KErrTimedOut);
|
132
|
313 |
test_KErrNone(ws.iSem.Wait(1));
|
0
|
314 |
|
|
315 |
ws.iTimeout=100000;
|
|
316 |
StartWaitSemThread(t, ws, EPriorityMore);
|
|
317 |
t.Suspend();
|
|
318 |
ws.iSem.Signal();
|
|
319 |
User::After(50000);
|
|
320 |
t.Resume();
|
|
321 |
WaitForWaitSemThread(t, KErrNone);
|
132
|
322 |
test_Equal(KErrTimedOut, ws.iSem.Wait(1));
|
0
|
323 |
|
|
324 |
RThread t2;
|
|
325 |
ws.iTimeout=100000;
|
|
326 |
StartWaitSemThread(t, ws, EPriorityMuchMore);
|
|
327 |
StartWaitSemThread(t2, ws, EPriorityMore);
|
|
328 |
t.Suspend();
|
|
329 |
ws.iSem.Signal();
|
132
|
330 |
test_Equal(EExitKill, t2.ExitType());
|
|
331 |
test_Equal(EExitPending, t.ExitType());
|
0
|
332 |
t.Resume();
|
|
333 |
WaitForWaitSemThread(t, KErrTimedOut);
|
|
334 |
WaitForWaitSemThread(t2, KErrNone);
|
132
|
335 |
test_Equal(KErrTimedOut, ws.iSem.Wait(1));
|
0
|
336 |
|
|
337 |
ws.iTimeout=1000000;
|
|
338 |
initial.HomeTime();
|
|
339 |
StartWaitSemThread(t2, ws, EPriorityMore);
|
|
340 |
StartWaitSemThread(t, ws, EPriorityMuchMore);
|
|
341 |
ws.iSem.Signal();
|
|
342 |
WaitForWaitSemThread(t, KErrNone);
|
|
343 |
final.HomeTime();
|
|
344 |
elapsed = I64INT(final.Int64()-initial.Int64());
|
|
345 |
test.Printf(_L("Time taken = %dus\n"), elapsed);
|
|
346 |
WaitForWaitSemThread(t2, KErrTimedOut);
|
|
347 |
final.HomeTime();
|
|
348 |
elapsed = I64INT(final.Int64()-initial.Int64());
|
|
349 |
test.Printf(_L("Time taken = %dus\n"), elapsed);
|
|
350 |
test(elapsed>=900000+2*overhead && elapsed<1500000+2*overhead);
|
|
351 |
|
|
352 |
ws.iTimeout=1000000;
|
|
353 |
initial.HomeTime();
|
|
354 |
StartWaitSemThread(t2, ws, EPriorityMore);
|
|
355 |
StartWaitSemThread(t, ws, EPriorityMuchMore);
|
|
356 |
WaitForWaitSemThread(t, KErrTimedOut);
|
|
357 |
final.HomeTime();
|
|
358 |
elapsed = I64INT(final.Int64()-initial.Int64());
|
|
359 |
test.Printf(_L("Time taken = %dus\n"), elapsed);
|
|
360 |
WaitForWaitSemThread(t2, KErrTimedOut);
|
|
361 |
final.HomeTime();
|
|
362 |
elapsed = I64INT(final.Int64()-initial.Int64());
|
|
363 |
test.Printf(_L("Time taken = %dus\n"), elapsed);
|
|
364 |
test(elapsed>=900000+2*overhead && elapsed<1500000+2*overhead);
|
|
365 |
|
|
366 |
ws.iTimeout=1000000;
|
|
367 |
initial.HomeTime();
|
|
368 |
StartWaitSemThread(t2, ws, EPriorityMore);
|
|
369 |
StartWaitSemThread(t, ws, EPriorityMuchMore);
|
|
370 |
t.Kill(299792458);
|
|
371 |
WaitForWaitSemThread(t2, KErrTimedOut);
|
|
372 |
WaitForWaitSemThread(t, 299792458);
|
|
373 |
final.HomeTime();
|
|
374 |
elapsed = I64INT(final.Int64()-initial.Int64());
|
|
375 |
test.Printf(_L("Time taken = %dus\n"), elapsed);
|
|
376 |
test(elapsed>=900000+2*overhead && elapsed<1500000+2*overhead);
|
|
377 |
|
|
378 |
ws.iTimeout=1000000;
|
|
379 |
initial.HomeTime();
|
|
380 |
StartWaitSemThread(t, ws, EPriorityMore);
|
|
381 |
StartWaitSemThread(t2, ws, EPriorityMuchMore);
|
132
|
382 |
test_Equal(EExitPending, t.ExitType());
|
|
383 |
test_Equal(EExitPending, t2.ExitType());
|
0
|
384 |
ws.iSem.Close();
|
132
|
385 |
test_Equal(EExitKill, t.ExitType());
|
|
386 |
test_Equal(EExitKill, t2.ExitType());
|
0
|
387 |
WaitForWaitSemThread(t2, KErrGeneral);
|
|
388 |
WaitForWaitSemThread(t, KErrGeneral);
|
|
389 |
final.HomeTime();
|
|
390 |
elapsed = I64INT(final.Int64()-initial.Int64());
|
|
391 |
test.Printf(_L("Time taken = %dus\n"), elapsed);
|
|
392 |
test(elapsed<=50000+3*overhead);
|
|
393 |
|
|
394 |
test.End();
|
|
395 |
}
|
|
396 |
|
|
397 |
void TestSemaphore()
|
|
398 |
{
|
|
399 |
/*********** TO DO ************/
|
|
400 |
// Check it panics if the count <0
|
|
401 |
|
|
402 |
test.Start(_L("Create"));
|
|
403 |
RSemaphore semaphore;
|
|
404 |
RThread thread1, thread2;
|
|
405 |
|
|
406 |
semaphore.CreateLocal(0); // creates a DPlatSemaphore but casts it to a pointer to a DSemaphore
|
|
407 |
// sets semaphore count to the value of the parameter,
|
|
408 |
// adds object to the K::Semaphores container, sets iHandle
|
|
409 |
// Local sets DSemaphore.iName to NULL & iOwner to Kern::CurrentProcess()
|
|
410 |
// Global sets iName to that passed and iOwner to NULL
|
|
411 |
// Adds a record into CObjectIx containing a pointer to the semaphore object
|
|
412 |
/* test.Next(_L("Find"));
|
|
413 |
fullName=semaphore.FullName();
|
|
414 |
find.Find(fullName); // sets iMatch to fullName (misleadingly named method as it doesn't find anything)
|
|
415 |
test(find.Next(fullName)== KErrNone);
|
|
416 |
*/
|
|
417 |
test.Next(_L("Producer/Consumer scenario"));
|
|
418 |
// Test Rsemaphore with the producer/consumer scenario RThread thread1, thread2;
|
|
419 |
TRequestStatus stat1, stat2;
|
132
|
420 |
test_KErrNone(mutex.CreateLocal());
|
|
421 |
test_KErrNone(slotAvailable.CreateLocal(KMaxBufferSize));
|
|
422 |
test_KErrNone(itemAvailable.CreateLocal(0));
|
|
423 |
test_KErrNone(thread1.Create(_L("Thread1"),Producer,KDefaultStackSize,0x200,0x200,NULL));
|
|
424 |
test_KErrNone(thread2.Create(_L("Thread2"),Consumer,KDefaultStackSize,0x200,0x200,NULL));
|
0
|
425 |
thread1.Logon(stat1);
|
|
426 |
thread2.Logon(stat2);
|
132
|
427 |
test_Equal(KRequestPending, stat1.Int());
|
|
428 |
test_Equal(KRequestPending, stat2.Int());
|
0
|
429 |
thread1.Resume();
|
|
430 |
thread2.Resume();
|
|
431 |
User::WaitForRequest(stat1);
|
|
432 |
User::WaitForRequest(stat2);
|
132
|
433 |
test_KErrNone(stat1.Int());
|
|
434 |
test_KErrNone(stat2.Int());
|
0
|
435 |
for(TInt jj=0;jj<KNumProducerItems;jj++)
|
132
|
436 |
test_Equal(jj, consumerArray[jj]);
|
0
|
437 |
|
|
438 |
test.Next(_L("Close"));
|
|
439 |
mutex.Close();
|
|
440 |
CLOSE_AND_WAIT(thread1);
|
|
441 |
CLOSE_AND_WAIT(thread2);
|
|
442 |
test.End();
|
|
443 |
}
|
|
444 |
|
|
445 |
void TestMutex2()
|
|
446 |
{
|
|
447 |
RMutex m;
|
|
448 |
test.Start(_L("Create"));
|
132
|
449 |
test_KErrNone(m.CreateLocal());
|
0
|
450 |
|
|
451 |
// Test RMutex::IsHeld()
|
|
452 |
test.Next(_L("IsHeld ?"));
|
|
453 |
test(!m.IsHeld());
|
|
454 |
test.Next(_L("Wait"));
|
|
455 |
m.Wait();
|
|
456 |
test.Next(_L("IsHeld ?"));
|
|
457 |
test(m.IsHeld());
|
|
458 |
test.Next(_L("Signal"));
|
|
459 |
m.Signal();
|
|
460 |
test.Next(_L("IsHeld ?"));
|
|
461 |
test(!m.IsHeld());
|
|
462 |
|
|
463 |
test.End();
|
|
464 |
}
|
|
465 |
|
|
466 |
void TestMutex()
|
|
467 |
{
|
|
468 |
test.Start(_L("Create"));
|
132
|
469 |
test_KErrNone(mutex.CreateLocal());
|
0
|
470 |
|
|
471 |
test.Next(_L("Threads writing to arrays test"));
|
|
472 |
//
|
|
473 |
// Create two threads which write to two arrays. The arrays and indexs
|
|
474 |
// are global and each thread writes an identifier to the arrays. For
|
|
475 |
// one array the writing and updating of the index is wrapped in a mutex
|
|
476 |
// pair. The other array is a control and is not wrapaped within mutex.
|
|
477 |
// Each thread records the number of instances it "thinks" it wrote to
|
|
478 |
// each array. For the mutex controlled array the actual instances
|
|
479 |
// written to the array should always be the same as the threads think.
|
|
480 |
//
|
|
481 |
arrayIndex=0;
|
|
482 |
RThread thread1,thread2;
|
132
|
483 |
test_KErrNone(thread1.Create(_L("Thread1"),MutexThreadEntryPoint1,KDefaultStackSize,0x2000,0x2000,NULL));
|
|
484 |
test_KErrNone(thread2.Create(_L("Thread2"),MutexThreadEntryPoint2,KDefaultStackSize,0x2000,0x2000,NULL));
|
0
|
485 |
TRequestStatus stat1,stat2;
|
|
486 |
thread1.Logon(stat1);
|
|
487 |
thread2.Logon(stat2);
|
132
|
488 |
test_Equal(KRequestPending, stat1.Int());
|
|
489 |
test_Equal(KRequestPending, stat2.Int());
|
0
|
490 |
thread1.Resume();
|
|
491 |
thread2.Resume();
|
|
492 |
User::WaitForRequest(stat1);
|
|
493 |
User::WaitForRequest(stat2);
|
132
|
494 |
test_KErrNone(stat1.Int());
|
|
495 |
test_KErrNone(stat2.Int());
|
0
|
496 |
TInt thread1ActualCount=0;
|
|
497 |
TInt thread2ActualCount=0;
|
|
498 |
TInt ii=0;
|
|
499 |
while(ii<KMaxArraySize)
|
|
500 |
{
|
|
501 |
if (array[ii]==EThread1ID)
|
|
502 |
thread1ActualCount++;
|
|
503 |
if (array[ii]==EThread2ID)
|
|
504 |
thread2ActualCount++;
|
|
505 |
ii++;
|
|
506 |
}
|
|
507 |
test.Printf(_L("T1 %d T1ACT %d T2 %d T2ACT %d"),thread1Count,thread1ActualCount,thread2Count,thread2ActualCount);
|
132
|
508 |
test_Equal(thread1Count, thread1ActualCount);
|
|
509 |
test_Equal(thread2Count, thread2ActualCount);
|
|
510 |
test_Equal(thread2Count, thread1Count);
|
|
511 |
test_Equal((KMaxArraySize>>1), thread1Count);
|
0
|
512 |
|
|
513 |
test.Next(_L("Close"));
|
|
514 |
CLOSE_AND_WAIT(thread1);
|
|
515 |
CLOSE_AND_WAIT(thread2);
|
|
516 |
mutex.Close();
|
|
517 |
test.End();
|
|
518 |
}
|
|
519 |
|
|
520 |
void TestCriticalSection()
|
|
521 |
//
|
|
522 |
//As TestMutex, but for RCriticalSection
|
|
523 |
//
|
|
524 |
{
|
|
525 |
|
|
526 |
test.Start(_L("Create"));
|
132
|
527 |
test_KErrNone(criticalSn.CreateLocal());
|
0
|
528 |
|
|
529 |
/***************** TO DO ***********************
|
|
530 |
|
|
531 |
test.Next(_L("Find"));
|
|
532 |
//
|
|
533 |
// Test finding the RCriticalSection
|
|
534 |
//
|
|
535 |
TFindCriticalSection find;
|
|
536 |
TFullName fullName;
|
|
537 |
fullName=criticalSn.FullName();
|
|
538 |
find.Find(fullName);
|
|
539 |
test(find.Next(fullName)==KErrNone);
|
|
540 |
test(fullName==criticalSn.FullName());
|
|
541 |
|
|
542 |
************************************************/
|
|
543 |
|
|
544 |
test.Next(_L("Threads writing to arrays test"));
|
|
545 |
//
|
|
546 |
// Create two threads which write to two arrays. The arrays and indexs
|
|
547 |
// are global and each thread writes an identifier to the arrays. For
|
|
548 |
// one array the writing and updating of the index is wrapped in a critical
|
|
549 |
// section pair. The other array is a control and is not wrapaped within
|
|
550 |
// a critical section. Each thread records the number of instances it
|
|
551 |
// "thinks" it wrote to each array. For the mutex controlled array the
|
|
552 |
// actual instances written to the array should always be the same as the
|
|
553 |
// threads think.
|
|
554 |
//
|
|
555 |
arrayIndex=0;
|
|
556 |
RThread thread1,thread2;
|
132
|
557 |
test_KErrNone(thread1.Create(_L("Thread1"),CriticalSnThreadEntryPoint1,KDefaultStackSize,0x2000,0x2000,NULL));
|
|
558 |
test_KErrNone(thread2.Create(_L("Thread2"),CriticalSnThreadEntryPoint2,KDefaultStackSize,0x2000,0x2000,NULL));
|
0
|
559 |
TRequestStatus stat1,stat2;
|
|
560 |
thread1.Logon(stat1);
|
|
561 |
thread2.Logon(stat2);
|
132
|
562 |
test_Equal(KRequestPending, stat1.Int());
|
|
563 |
test_Equal(KRequestPending, stat2.Int());
|
0
|
564 |
thread1.Resume();
|
|
565 |
thread2.Resume();
|
|
566 |
User::WaitForRequest(stat1);
|
|
567 |
User::WaitForRequest(stat2);
|
132
|
568 |
test_KErrNone(stat1.Int());
|
|
569 |
test_KErrNone(stat2.Int());
|
0
|
570 |
TInt thread1ActualCount=0;
|
|
571 |
TInt thread2ActualCount=0;
|
|
572 |
TInt ii=0;
|
|
573 |
while(ii<KMaxArraySize)
|
|
574 |
{
|
|
575 |
if (array[ii]==EThread1ID)
|
|
576 |
thread1ActualCount++;
|
|
577 |
if (array[ii]==EThread2ID)
|
|
578 |
thread2ActualCount++;
|
|
579 |
ii++;
|
|
580 |
}
|
132
|
581 |
test_Equal(thread1Count, thread1ActualCount);
|
|
582 |
test_Equal(thread2Count, thread2ActualCount);
|
|
583 |
test_Equal(thread2Count, thread1Count);
|
|
584 |
test_Equal((KMaxArraySize>>1), thread1Count);
|
0
|
585 |
|
|
586 |
test.Next(_L("Close"));
|
|
587 |
CLOSE_AND_WAIT(thread1);
|
|
588 |
CLOSE_AND_WAIT(thread2);
|
|
589 |
criticalSn.Close();
|
|
590 |
test.End();
|
|
591 |
}
|
|
592 |
|
|
593 |
|
|
594 |
GLDEF_C TInt E32Main()
|
|
595 |
{
|
132
|
596 |
TInt cpus = UserSvr::HalFunction(EHalGroupKernel, EKernelHalNumLogicalCpus, 0, 0);
|
|
597 |
if (cpus != 1)
|
|
598 |
{
|
|
599 |
test(cpus>1);
|
|
600 |
// This test will require compatibility mode (and probably other changes)
|
|
601 |
// to work on SMP - it depends on explicit scheduling order.
|
|
602 |
test.Printf(_L("T_SEMUTX skipped, does not work on SMP\n"));
|
|
603 |
return KErrNone;
|
|
604 |
}
|
|
605 |
|
0
|
606 |
|
|
607 |
test.Title();
|
|
608 |
__UHEAP_MARK;
|
|
609 |
test.Start(_L("Test RSemaphore"));
|
|
610 |
TestSemaphore();
|
|
611 |
TestSemaphore2();
|
|
612 |
test.Next(_L("Test RMutex"));
|
|
613 |
TestMutex();
|
|
614 |
TestMutex2();
|
|
615 |
test.Next(_L("Test RCriticalSection"));
|
|
616 |
TestCriticalSection();
|
|
617 |
test.End();
|
|
618 |
__UHEAP_MARKEND;
|
|
619 |
return(KErrNone);
|
|
620 |
}
|
|
621 |
|
|
622 |
|