|
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_semutx2.cpp |
|
15 // Test RSemaphore and RMutex |
|
16 // Overview: |
|
17 // Tests the RSemaphore and RMutex |
|
18 // API Information: |
|
19 // RSemaphore, RMutex |
|
20 // Details: |
|
21 // - Test and verify that thread priorities work as expected. |
|
22 // - Test and verify that signalling an RMutex from the wrong |
|
23 // thread fails as expected. |
|
24 // - Test and verify that mutex priority inheritance works as |
|
25 // expected. |
|
26 // - Perform an exhaustive state transition test using mutexs |
|
27 // and up to ten threads. Verify priorities, order of execution, |
|
28 // mutex signalling, suspend, resume, kill and close. Verify |
|
29 // results are as expected. |
|
30 // - Test semaphore speed by counting how many Wait/Signal |
|
31 // operations can be completed in one second. |
|
32 // Platforms/Drives/Compatibility: |
|
33 // All. |
|
34 // Assumptions/Requirement/Pre-requisites: |
|
35 // Failures and causes: |
|
36 // Base Port information: |
|
37 // |
|
38 // |
|
39 |
|
40 #include <e32test.h> |
|
41 |
|
42 RMutex M1; |
|
43 RMutex M2; |
|
44 RSemaphore S; |
|
45 |
|
46 const TInt KBufferSize=4096; |
|
47 TUint ThreadId[KBufferSize]; |
|
48 TInt PutIx; |
|
49 TInt GetIx; |
|
50 TInt Count; |
|
51 RThread Main; |
|
52 |
|
53 |
|
54 RTest test(_L("T_SEMUTX2")); |
|
55 |
|
56 /***************************************************************************** |
|
57 * Utility functions / macros |
|
58 *****************************************************************************/ |
|
59 #define TRACE_ON User::SetDebugMask(0xffdfffff); |
|
60 #define TRACE_OFF User::SetDebugMask(0x80000000); |
|
61 |
|
62 //#define MCOUNT(m,c) test((m).Count() ==(c)) |
|
63 // mutex count value is not visible for user any more |
|
64 #define MCOUNT(m,c) (void)(1) |
|
65 #define IDCHECK(x) test(GetNextId()==(x)) |
|
66 #define NUMCHECK(x) test(NumIdsPending()==(x)) |
|
67 |
|
68 #define id0 id[0] |
|
69 #define id1 id[1] |
|
70 #define id2 id[2] |
|
71 #define id3 id[3] |
|
72 #define id4 id[4] |
|
73 #define id5 id[5] |
|
74 #define id6 id[6] |
|
75 #define id7 id[7] |
|
76 #define id8 id[8] |
|
77 #define id9 id[9] |
|
78 #define id10 id[10] |
|
79 |
|
80 TBool Exists(const TDesC& aName) |
|
81 { |
|
82 TFullName n(RProcess().Name()); |
|
83 n+=_L("::"); |
|
84 n+=aName; |
|
85 TFindThread ft(n); |
|
86 TFullName fn; |
|
87 return ft.Next(fn)==KErrNone; |
|
88 } |
|
89 |
|
90 TBool Exists(TInt aNum) |
|
91 { |
|
92 TBuf<4> b; |
|
93 b.Num(aNum); |
|
94 return Exists(b); |
|
95 } |
|
96 |
|
97 void BusyWait(TInt aMicroseconds) |
|
98 { |
|
99 TTime begin; |
|
100 begin.HomeTime(); |
|
101 FOREVER |
|
102 { |
|
103 TTime now; |
|
104 now.HomeTime(); |
|
105 TTimeIntervalMicroSeconds iv=now.MicroSecondsFrom(begin); |
|
106 if (iv.Int64()>=TInt64(aMicroseconds)) |
|
107 return; |
|
108 } |
|
109 } |
|
110 |
|
111 void Kick(RThread& t) |
|
112 { |
|
113 TRequestStatus s; |
|
114 TRequestStatus* pS=&s; |
|
115 t.RequestComplete(pS,0); |
|
116 } |
|
117 |
|
118 TUint GetNextId() |
|
119 { |
|
120 if (GetIx<PutIx) |
|
121 return ThreadId[GetIx++]; |
|
122 return 0; |
|
123 } |
|
124 |
|
125 TInt NumIdsPending() |
|
126 { |
|
127 return PutIx-GetIx; |
|
128 } |
|
129 |
|
130 /***************************************************************************** |
|
131 * General tests |
|
132 *****************************************************************************/ |
|
133 TInt Test0Thread(TAny* aPtr) |
|
134 { |
|
135 TInt& count=*(TInt*)aPtr; |
|
136 ++count; |
|
137 Main.SetPriority(EPriorityMuchMore); |
|
138 ++count; |
|
139 Main.SetPriority(EPriorityMuchMore); |
|
140 ++count; |
|
141 RThread().SetPriority(EPriorityNormal); |
|
142 ++count; |
|
143 return 0; |
|
144 } |
|
145 |
|
146 void Test0() |
|
147 { |
|
148 User::After(100000); // Test fails intermittently on hardware unless we pause here |
|
149 |
|
150 test.Start(_L("Test thread priorities work")); |
|
151 test.Next(_L("Create thread")); |
|
152 RThread t; |
|
153 TInt count=0; |
|
154 TRequestStatus s; |
|
155 TInt r=t.Create(_L("Test0"),Test0Thread,0x1000,NULL,&count); |
|
156 test(r==KErrNone); |
|
157 t.Logon(s); |
|
158 test(r==KErrNone); |
|
159 User::After(10000); // make sure we have a full timeslice |
|
160 t.Resume(); |
|
161 |
|
162 test(count==0); // t shouldn't have run yet |
|
163 RThread().SetPriority(EPriorityMuchMore); // shouldn't reschedule (priority unchanged) |
|
164 test(count==0); |
|
165 RThread().SetPriority(EPriorityMore); // shouldn't reschedule (priority decreasing, but not enough) |
|
166 test(count==0); |
|
167 RThread().SetPriority(EPriorityMuchMore); // shouldn't reschedule (priority increasing) |
|
168 test(count==0); |
|
169 RThread().SetPriority(EPriorityNormal); // should reschedule (we go behind t) |
|
170 test(count==1); |
|
171 RThread().SetPriority(EPriorityLess); // should reschedule (priority decreasing to below t) |
|
172 test(count==2); |
|
173 t.SetPriority(EPriorityMuchMore); // shouldn't reschedule (round-robin, timeslice not expired) |
|
174 test(count==2); |
|
175 t.SetPriority(EPriorityNormal); // shouldn't reschedule (t's priority decreasing) |
|
176 test(count==2); |
|
177 t.SetPriority(EPriorityNormal); // shouldn't reschedule (t's priority unchanged) |
|
178 test(count==2); |
|
179 BusyWait(100000); // use up our timeslice |
|
180 t.SetPriority(EPriorityMuchMore); // should reschedule (round-robin, timeslice expired) |
|
181 test(count==3); |
|
182 test(s==KRequestPending); |
|
183 test(t.ExitType()==EExitPending); |
|
184 t.SetPriority(EPriorityRealTime); // should reschedule (t increases above current) |
|
185 test(count==4); |
|
186 test(s==KErrNone); // t should have exited |
|
187 test(t.ExitType()==EExitKill); |
|
188 User::WaitForRequest(s); |
|
189 RThread().SetPriority(EPriorityMuchMore); |
|
190 t.Close(); |
|
191 test.End(); |
|
192 } |
|
193 |
|
194 TInt Test1Thread(TAny*) |
|
195 { |
|
196 M1.Signal(); |
|
197 return 0; |
|
198 } |
|
199 |
|
200 void Test1() |
|
201 { |
|
202 test.Start(_L("Test signalling from wrong thread")); |
|
203 TInt r=M1.CreateLocal(); |
|
204 test(r==KErrNone); |
|
205 M1.Wait(); |
|
206 RThread t; |
|
207 r=t.Create(_L("Test1"),Test1Thread,0x1000,NULL,NULL); |
|
208 test(r==KErrNone); |
|
209 TRequestStatus s; |
|
210 t.Logon(s); |
|
211 t.Resume(); |
|
212 TBool jit = User::JustInTime(); |
|
213 User::SetJustInTime(EFalse); |
|
214 User::WaitForRequest(s); |
|
215 User::SetJustInTime(jit); |
|
216 test(s==EAccessDenied); |
|
217 test(t.ExitType()==EExitPanic); |
|
218 test(t.ExitReason()==EAccessDenied); |
|
219 test(t.ExitCategory()==_L("KERN-EXEC")); |
|
220 t.Close(); |
|
221 M1.Close(); |
|
222 test.End(); |
|
223 } |
|
224 |
|
225 /***************************************************************************** |
|
226 * Mutex priority inheritance |
|
227 *****************************************************************************/ |
|
228 |
|
229 const TInt KTestDelay = 1000000; |
|
230 |
|
231 TInt LowThread(TAny* aPtr) |
|
232 { |
|
233 TInt& count=*(TInt*)aPtr; |
|
234 FOREVER |
|
235 { |
|
236 M1.Wait(); |
|
237 ++count; |
|
238 BusyWait(KTestDelay); |
|
239 M1.Signal(); |
|
240 User::WaitForAnyRequest(); |
|
241 } |
|
242 } |
|
243 |
|
244 TInt MedThread(TAny* aPtr) |
|
245 { |
|
246 TInt& count=*(TInt*)aPtr; |
|
247 FOREVER |
|
248 { |
|
249 ++count; |
|
250 User::WaitForAnyRequest(); |
|
251 } |
|
252 } |
|
253 |
|
254 TInt HighThread(TAny* aPtr) |
|
255 { |
|
256 TInt& count=*(TInt*)aPtr; |
|
257 FOREVER |
|
258 { |
|
259 M2.Wait(); |
|
260 ++count; |
|
261 M1.Wait(); |
|
262 ++count; |
|
263 BusyWait(KTestDelay); |
|
264 M1.Signal(); |
|
265 M2.Signal(); |
|
266 User::WaitForAnyRequest(); |
|
267 } |
|
268 } |
|
269 |
|
270 void TestMutex1() |
|
271 { |
|
272 test.Start(_L("Test mutex priority inheritance")); |
|
273 |
|
274 test.Next(_L("Create mutex")); |
|
275 TInt r=M1.CreateLocal(); |
|
276 test(r==KErrNone); |
|
277 |
|
278 test.Next(_L("Create low priority thread")); |
|
279 TInt lowcount=0; |
|
280 RThread low; |
|
281 r=low.Create(_L("low"),LowThread,0x1000,NULL,&lowcount); |
|
282 test(r==KErrNone); |
|
283 low.SetPriority(EPriorityMuchLess); |
|
284 test(Exists(_L("low"))); |
|
285 |
|
286 test.Next(_L("Create medium priority thread")); |
|
287 TInt medcount=0; |
|
288 RThread med; |
|
289 r=med.Create(_L("med"),MedThread,0x1000,NULL,&medcount); |
|
290 test(r==KErrNone); |
|
291 med.SetPriority(EPriorityNormal); |
|
292 test(Exists(_L("med"))); |
|
293 |
|
294 test.Next(_L("Start low priority thread")); |
|
295 low.Resume(); |
|
296 User::AfterHighRes(KTestDelay/10); |
|
297 test(lowcount==1); |
|
298 // MCOUNT(M1,0); |
|
299 |
|
300 test.Next(_L("Start medium priority thread")); |
|
301 med.Resume(); |
|
302 User::AfterHighRes(KTestDelay/10); |
|
303 test(medcount==1); |
|
304 Kick(med); |
|
305 User::AfterHighRes(KTestDelay/10); |
|
306 test(medcount==2); |
|
307 Kick(med); |
|
308 |
|
309 M1.Wait(); |
|
310 test(lowcount==1); |
|
311 test(medcount==2); |
|
312 test.Next(_L("Wait, check medium runs")); |
|
313 User::AfterHighRes(KTestDelay/10); |
|
314 test(medcount==3); |
|
315 M1.Signal(); |
|
316 |
|
317 test.Next(_L("Create mutex 2")); |
|
318 r=M2.CreateLocal(); |
|
319 test(r==KErrNone); |
|
320 |
|
321 test.Next(_L("Create high priority thread")); |
|
322 TInt highcount=0; |
|
323 RThread high; |
|
324 r=high.Create(_L("high"),HighThread,0x1000,NULL,&highcount); |
|
325 test(r==KErrNone); |
|
326 high.SetPriority(EPriorityMore); |
|
327 test(Exists(_L("high"))); |
|
328 |
|
329 Kick(low); |
|
330 User::AfterHighRes(KTestDelay/10); |
|
331 // MCOUNT(M1,0); |
|
332 Kick(med); |
|
333 |
|
334 // MCOUNT(M2,1); |
|
335 high.Resume(); |
|
336 User::AfterHighRes(KTestDelay/10); |
|
337 // MCOUNT(M2,0); |
|
338 // MCOUNT(M1,-1); |
|
339 test(highcount==1); |
|
340 |
|
341 M2.Wait(); |
|
342 test(lowcount==2); |
|
343 test(medcount==3); |
|
344 test(highcount==2); |
|
345 test.Next(_L("Wait, check medium runs")); |
|
346 User::AfterHighRes(KTestDelay/10); |
|
347 test(medcount==4); |
|
348 M2.Signal(); |
|
349 |
|
350 test.Next(_L("Kill threads")); |
|
351 low.Kill(0); |
|
352 med.Kill(0); |
|
353 high.Kill(0); |
|
354 low.Close(); |
|
355 med.Close(); |
|
356 high.Close(); |
|
357 test(!Exists(_L("low"))); |
|
358 test(!Exists(_L("med"))); |
|
359 test(!Exists(_L("high"))); |
|
360 |
|
361 M1.Close(); |
|
362 test.End(); |
|
363 } |
|
364 |
|
365 /***************************************************************************** |
|
366 * Utilities for mutex exhaustive state transition test |
|
367 *****************************************************************************/ |
|
368 void MutexWait() |
|
369 { |
|
370 M1.Wait(); |
|
371 ++Count; |
|
372 ThreadId[PutIx++]=(TUint)RThread().Id(); |
|
373 } |
|
374 |
|
375 void MutexSignal() |
|
376 { |
|
377 M1.Signal(); |
|
378 } |
|
379 |
|
380 typedef void (*PFV)(void); |
|
381 TInt ThreadFunction(TAny* aPtr) |
|
382 { |
|
383 PFV& f=*(PFV*)aPtr; |
|
384 FOREVER |
|
385 { |
|
386 MutexWait(); |
|
387 if (f) |
|
388 f(); |
|
389 MutexSignal(); |
|
390 User::WaitForAnyRequest(); |
|
391 } |
|
392 } |
|
393 |
|
394 void Exit() |
|
395 { |
|
396 User::Exit(0); |
|
397 } |
|
398 |
|
399 TUint CreateThread(RThread& t, TInt n, TAny* aPtr) |
|
400 { |
|
401 TBuf<4> b; |
|
402 b.Num(n); |
|
403 TInt r=t.Create(b,ThreadFunction,0x1000,NULL,aPtr); |
|
404 test(r==KErrNone); |
|
405 t.Resume(); |
|
406 TUint id=t.Id(); |
|
407 test.Printf(_L("id=%d\n"),id); |
|
408 return id; |
|
409 } |
|
410 |
|
411 /* |
|
412 Possible thread relationships with mutex: |
|
413 Holding |
|
414 Waiting |
|
415 Waiting + suspended |
|
416 Hold Pending |
|
417 |
|
418 Need to verify correct behaviour when the following actions occur for each of these states: |
|
419 Suspend thread |
|
420 Resume thread |
|
421 Change thread priority |
|
422 Thread exits |
|
423 Thread is killed |
|
424 Mutex deleted |
|
425 */ |
|
426 |
|
427 PFV HoldExtra; |
|
428 void KickMain() |
|
429 { |
|
430 RThread me; |
|
431 Kick(Main); |
|
432 User::WaitForAnyRequest(); |
|
433 me.SetPriority(EPriorityMuchMore); |
|
434 MutexSignal(); // this should wake up t8 |
|
435 MutexWait(); |
|
436 MutexSignal(); // this should wake up t9 |
|
437 MutexWait(); |
|
438 Kick(Main); |
|
439 User::WaitForAnyRequest(); |
|
440 if (HoldExtra) |
|
441 HoldExtra(); |
|
442 } |
|
443 |
|
444 void RackEmUp(RThread* t, PFV* f, TUint* id) |
|
445 { |
|
446 // set up t4 holding |
|
447 // t1, t2, t5, t10 waiting |
|
448 // t3, t6, t7 waiting+suspended |
|
449 // t8, t9 pending |
|
450 MCOUNT(M1,1); // check mutex free |
|
451 Kick(t[4]); |
|
452 f[4]=&KickMain; |
|
453 User::WaitForAnyRequest(); |
|
454 MCOUNT(M1,0); // check mutex now held |
|
455 TInt i; |
|
456 for (i=1; i<=10; ++i) |
|
457 if (i!=4) |
|
458 Kick(t[i]); // wake up threads |
|
459 User::After(50000); // let threads wait |
|
460 MCOUNT(M1,-9); // check 9 threads waiting |
|
461 Kick(t[4]); |
|
462 User::WaitForAnyRequest(); |
|
463 MCOUNT(M1,-7); // check 7 threads waiting |
|
464 NUMCHECK(3); |
|
465 IDCHECK(id4); // from the initial wait |
|
466 IDCHECK(id4); // now have t8, t9 pending, t4 holding, rest waiting |
|
467 IDCHECK(id4); // now have t8, t9 pending, t4 holding, rest waiting |
|
468 t[4].SetPriority(EPriorityNormal); |
|
469 t[7].Resume(); // test resume when not suspended |
|
470 MCOUNT(M1,-7); // check 7 threads waiting |
|
471 t[3].Suspend(); |
|
472 t[6].Suspend(); |
|
473 t[7].Suspend(); // now have required state |
|
474 t[3].Suspend(); // suspend and resume t3 again for good measure |
|
475 t[3].Resume(); |
|
476 MCOUNT(M1,-7); // check 7 threads waiting |
|
477 HoldExtra=NULL; |
|
478 } |
|
479 |
|
480 void SimpleCheck(TInt n, const TUint* id, ...) |
|
481 { |
|
482 VA_LIST list; |
|
483 VA_START(list,id); |
|
484 User::After(50000); // let stuff happen |
|
485 NUMCHECK(n); |
|
486 TInt i; |
|
487 for (i=0; i<n; ++i) |
|
488 { |
|
489 TInt tn=VA_ARG(list,TInt); |
|
490 IDCHECK(id[tn]); |
|
491 } |
|
492 } |
|
493 |
|
494 void Resurrect(TInt n, TThreadPriority aPriority, RThread* t, PFV* f, TUint* id) |
|
495 { |
|
496 f[n]=NULL; |
|
497 id[n]=CreateThread(t[n],n,f+n); |
|
498 t[n].SetPriority(EPriorityRealTime); |
|
499 t[n].SetPriority(aPriority); |
|
500 NUMCHECK(1); |
|
501 IDCHECK(id[n]); |
|
502 } |
|
503 |
|
504 /***************************************************************************** |
|
505 * Mutex exhaustive state transition test |
|
506 *****************************************************************************/ |
|
507 void TestMutex2() |
|
508 { |
|
509 test.Start(_L("Test mutex state transitions")); |
|
510 RThread t[11]; |
|
511 TUint id[11]; |
|
512 PFV f[11]={NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}; |
|
513 id[0]=(TUint)RThread().Id(); |
|
514 PutIx=0; |
|
515 GetIx=0; |
|
516 Count=0; |
|
517 test.Next(_L("Create mutex")); |
|
518 TInt r=M1.CreateLocal(); |
|
519 test(r==KErrNone); |
|
520 MCOUNT(M1,1); |
|
521 MutexWait(); |
|
522 MCOUNT(M1,0); |
|
523 IDCHECK(id[0]); |
|
524 test.Next(_L("Create threads")); |
|
525 TInt i; |
|
526 for (i=1; i<=5; ++i) |
|
527 id[i]=CreateThread(t[i],i,f+i); |
|
528 User::After(50000); // let threads wait on mutex |
|
529 MCOUNT(M1,-5); // check 5 threads waiting |
|
530 for (i=-4; i<=0; ++i) |
|
531 { |
|
532 MutexSignal(); // wake up next thread |
|
533 MutexWait(); |
|
534 MCOUNT(M1,i); // check right number of threads waiting |
|
535 IDCHECK(id0); // check we got mutex back straight away |
|
536 } |
|
537 MutexSignal(); |
|
538 User::After(50000); // let threads claim mutex |
|
539 MutexWait(); |
|
540 MCOUNT(M1,0); // check no threads waiting |
|
541 for (i=1; i<=5; ++i) |
|
542 { |
|
543 IDCHECK(id[i]); // check they ran in order t1...t5 |
|
544 Kick(t[i]); // wake up thread |
|
545 } |
|
546 IDCHECK(id0); // check we got it back last |
|
547 t[4].SetPriority(EPriorityMore); // make t4 higher priority |
|
548 User::After(50000); // let threads wait on mutex |
|
549 MCOUNT(M1,-5); // check 5 threads waiting |
|
550 // temp = M1.Count(); |
|
551 MutexSignal(); |
|
552 // temp = M1.Count(); |
|
553 User::After(50000); // let threads claim mutex |
|
554 MutexWait(); |
|
555 // temp = M1.Count(); |
|
556 MCOUNT(M1,0); // check no threads waiting |
|
557 IDCHECK(id4); // check they ran in order t4,t1,t2,t3,t5 |
|
558 IDCHECK(id1); |
|
559 IDCHECK(id2); |
|
560 IDCHECK(id3); |
|
561 IDCHECK(id5); |
|
562 IDCHECK(id0); |
|
563 t[4].SetPriority(EPriorityNormal); // make t4 normal priority |
|
564 for (i=1; i<=5; ++i) |
|
565 Kick(t[i]); // wake up thread |
|
566 User::After(50000); // let threads wait on mutex |
|
567 MCOUNT(M1,-5); // check 5 threads waiting |
|
568 |
|
569 t[3].SetPriority(EPriorityMore); // make t3 higher priority |
|
570 // temp = M1.Count(); |
|
571 MutexSignal(); |
|
572 // temp = M1.Count(); |
|
573 User::After(50000); // let threads claim mutex |
|
574 MutexWait(); |
|
575 // temp = M1.Count(); |
|
576 MCOUNT(M1,0); // check no threads waiting |
|
577 IDCHECK(id3); // check they ran in order t3,t1,t2,t4,t5 |
|
578 IDCHECK(id1); |
|
579 IDCHECK(id2); |
|
580 IDCHECK(id4); |
|
581 IDCHECK(id5); |
|
582 IDCHECK(id0); |
|
583 t[3].SetPriority(EPriorityNormal); // make t4 normal priority |
|
584 for (i=1; i<=5; ++i) |
|
585 Kick(t[i]); // wake up threads |
|
586 User::After(50000); // let threads wait on mutex |
|
587 MCOUNT(M1,-5); // check 5 threads waiting |
|
588 |
|
589 t[2].SetPriority(EPriorityMore); // make t2 higher priority |
|
590 t[1].SetPriority(EPriorityLess); // make t1 lower priority |
|
591 MutexSignal(); |
|
592 User::After(50000); // let threads claim mutex |
|
593 MutexWait(); |
|
594 MCOUNT(M1,0); // check no threads waiting |
|
595 IDCHECK(id2); // check they ran in order t2,t3,t4,t5,t1 |
|
596 IDCHECK(id3); |
|
597 IDCHECK(id4); |
|
598 IDCHECK(id5); |
|
599 IDCHECK(id1); |
|
600 IDCHECK(id0); |
|
601 |
|
602 for (i=1; i<=5; ++i) |
|
603 Kick(t[i]); // wake up threads |
|
604 User::After(50000); // let threads wait on mutex |
|
605 MCOUNT(M1,-5); // check 5 threads waiting |
|
606 MutexSignal(); |
|
607 User::After(50000); // let threads claim mutex |
|
608 MutexWait(); |
|
609 MCOUNT(M1,0); // check no threads waiting |
|
610 IDCHECK(id2); // check they ran in order t2,t3,t4,t5,t1 |
|
611 IDCHECK(id3); |
|
612 IDCHECK(id4); |
|
613 IDCHECK(id5); |
|
614 IDCHECK(id1); |
|
615 IDCHECK(id0); |
|
616 |
|
617 test(Exists(2)); |
|
618 for (i=1; i<=5; ++i) |
|
619 Kick(t[i]); // wake up threads |
|
620 User::After(50000); // let threads wait on mutex |
|
621 MCOUNT(M1,-5); // check 5 threads waiting |
|
622 f[2]=&Exit; // make t2 exit while holding the mutex |
|
623 MutexSignal(); |
|
624 User::After(50000); // let threads claim mutex |
|
625 MutexWait(); |
|
626 MCOUNT(M1,0); // check no threads waiting |
|
627 test(t[2].ExitType()==EExitKill); // check t2 has exited |
|
628 t[2].Close(); |
|
629 test(!Exists(2)); |
|
630 IDCHECK(id2); // check they ran in order t2,t3,t4,t5,t1 |
|
631 IDCHECK(id3); |
|
632 IDCHECK(id4); |
|
633 IDCHECK(id5); |
|
634 IDCHECK(id1); |
|
635 IDCHECK(id0); |
|
636 f[2]=NULL; |
|
637 id[2]=CreateThread(t[2],2,f+2); // recreate t2 |
|
638 User::After(50000); // let new t2 wait on mutex |
|
639 MCOUNT(M1,-1); // check 1 thread waiting |
|
640 MutexSignal(); |
|
641 User::After(50000); // let t2 claim mutex |
|
642 MutexWait(); |
|
643 MCOUNT(M1,0); // check no threads waiting |
|
644 IDCHECK(id2); |
|
645 IDCHECK(id0); |
|
646 |
|
647 t[2].SetPriority(EPriorityLess); // make t2 lower priority |
|
648 for (i=1; i<=5; ++i) |
|
649 Kick(t[i]); // wake up threads |
|
650 User::After(50000); // let threads wait on mutex |
|
651 MCOUNT(M1,-5); // check 5 threads waiting |
|
652 MutexSignal(); // t3 now pending |
|
653 MCOUNT(M1,-3); // check 4 threads waiting, mutex free |
|
654 t[3].Suspend(); // this should wake up t4 |
|
655 MCOUNT(M1,-2); // check 3 threads waiting, mutex free |
|
656 User::After(50000); // let threads claim mutex |
|
657 MutexWait(); |
|
658 MCOUNT(M1,0); // check no threads still waiting |
|
659 IDCHECK(id4); // check they ran in order t4,t5,t1,t2 |
|
660 IDCHECK(id5); |
|
661 IDCHECK(id1); |
|
662 IDCHECK(id2); |
|
663 IDCHECK(id0); |
|
664 Kick(t[1]); // wake up t1 |
|
665 User::After(50000); // let thread wait on mutex |
|
666 MCOUNT(M1,-1); // check 1 thread waiting |
|
667 t[3].Resume(); // resume pending t3 |
|
668 MutexSignal(); |
|
669 User::After(50000); // let t2 claim mutex |
|
670 MutexWait(); |
|
671 MCOUNT(M1,0); // check no threads waiting |
|
672 IDCHECK(id3); // check order t3,t1 |
|
673 IDCHECK(id1); |
|
674 IDCHECK(id0); |
|
675 |
|
676 for (i=1; i<=5; ++i) |
|
677 Kick(t[i]); // wake up threads |
|
678 User::After(50000); // let threads wait on mutex |
|
679 MCOUNT(M1,-5); // check 5 threads waiting |
|
680 t[4].Suspend(); // suspend t4 |
|
681 MCOUNT(M1,-5); // check 5 threads waiting |
|
682 t[4].Suspend(); // suspend t4 again |
|
683 MCOUNT(M1,-5); // check 5 threads waiting |
|
684 MutexSignal(); |
|
685 User::After(50000); // let threads claim mutex |
|
686 MutexWait(); |
|
687 MCOUNT(M1,-1); // check 1 thread still waiting |
|
688 IDCHECK(id3); // check they ran in order t3,t5,t1,t2 |
|
689 IDCHECK(id5); |
|
690 IDCHECK(id1); |
|
691 IDCHECK(id2); |
|
692 IDCHECK(id0); |
|
693 MutexSignal(); |
|
694 t[4].Resume(); |
|
695 User::After(50000); // let threads claim mutex |
|
696 MutexWait(); |
|
697 IDCHECK(id0); // check thread didn't get mutex (still suspended) |
|
698 MutexSignal(); |
|
699 t[4].Resume(); |
|
700 User::After(50000); // let threads claim mutex |
|
701 MutexWait(); |
|
702 IDCHECK(id4); // check order t4 then this |
|
703 IDCHECK(id0); |
|
704 |
|
705 for (i=1; i<=5; ++i) |
|
706 Kick(t[i]); // wake up threads |
|
707 User::After(50000); // let threads wait on mutex |
|
708 MCOUNT(M1,-5); // check 5 threads waiting |
|
709 MutexWait(); // wait on mutex again |
|
710 IDCHECK(id0); |
|
711 MutexSignal(); // signal once |
|
712 MCOUNT(M1,-5); // check 5 threads still waiting |
|
713 MutexSignal(); // signal again |
|
714 MCOUNT(M1,-3); // check one thread has been woken up and mutex is now free |
|
715 User::After(50000); // let threads claim mutex |
|
716 MutexWait(); |
|
717 MCOUNT(M1,0); // check no threads still waiting |
|
718 IDCHECK(id3); // check they ran in order t3,t4,t5,t1,t2 |
|
719 IDCHECK(id4); |
|
720 IDCHECK(id5); |
|
721 IDCHECK(id1); |
|
722 IDCHECK(id2); |
|
723 IDCHECK(id0); |
|
724 |
|
725 test.Next(_L("Create more threads")); |
|
726 for (i=6; i<=10; ++i) |
|
727 id[i]=CreateThread(t[i],i,f+i); |
|
728 User::After(50000); // let threads wait on mutex |
|
729 MCOUNT(M1,-5); // check 5 threads waiting |
|
730 MutexSignal(); |
|
731 User::After(50000); // let threads claim mutex |
|
732 MCOUNT(M1,1); // check no threads still waiting and mutex free |
|
733 IDCHECK(id6); // check they ran in order t6,t7,t8,t9,t10 |
|
734 IDCHECK(id7); |
|
735 IDCHECK(id8); |
|
736 IDCHECK(id9); |
|
737 IDCHECK(id10); |
|
738 t[8].SetPriority(EPriorityMore); // t1-t3=less, t4-t7=normal, t8-t10 more, t0 much more |
|
739 t[9].SetPriority(EPriorityMore); |
|
740 t[10].SetPriority(EPriorityMore); |
|
741 t[2].SetPriority(EPriorityLess); |
|
742 t[3].SetPriority(EPriorityLess); |
|
743 |
|
744 RackEmUp(t,f,id); |
|
745 SimpleCheck(0,NULL,NULL); // holding thread still blocked |
|
746 Kick(t[4]); |
|
747 SimpleCheck(6,id,10,8,9,5,1,2); // 3,6,7 suspended |
|
748 t[3].Resume(); |
|
749 t[6].Resume(); |
|
750 t[7].Resume(); |
|
751 SimpleCheck(3,id,6,7,3); // 3,6,7 resumed |
|
752 |
|
753 RackEmUp(t,f,id); |
|
754 SimpleCheck(0,NULL,NULL); // holding thread still blocked |
|
755 Kick(t[4]); |
|
756 t[4].Suspend(); |
|
757 SimpleCheck(0,NULL,NULL); // holding thread suspended |
|
758 t[4].Resume(); |
|
759 SimpleCheck(6,id,10,8,9,5,1,2); // 3,6,7 suspended |
|
760 t[3].Resume(); |
|
761 t[6].Resume(); |
|
762 t[7].Resume(); |
|
763 SimpleCheck(3,id,6,7,3); // 3,6,7 resumed |
|
764 |
|
765 RackEmUp(t,f,id); |
|
766 Kick(t[4]); |
|
767 t[4].SetPriority(EPriorityRealTime); |
|
768 MCOUNT(M1,-5); // should be 6 waiting, mutex free |
|
769 t[4].SetPriority(EPriorityNormal); |
|
770 t[8].SetPriority(EPriorityRealTime); // change pending thread priority |
|
771 MCOUNT(M1,-4); // should be 5 waiting, mutex free |
|
772 t[8].SetPriority(EPriorityMore); |
|
773 NUMCHECK(1); |
|
774 IDCHECK(id8); |
|
775 t[3].SetPriority(EPriorityRealTime); // change suspended thread priority |
|
776 SimpleCheck(5,id,9,10,5,1,2); // 3,6,7 suspended |
|
777 t[6].Resume(); |
|
778 t[7].Resume(); |
|
779 t[3].Resume(); // this should run right away |
|
780 NUMCHECK(1); |
|
781 IDCHECK(id3); |
|
782 SimpleCheck(2,id,6,7); // 6,7 resumed |
|
783 t[3].SetPriority(EPriorityLess); |
|
784 |
|
785 RackEmUp(t,f,id); |
|
786 Kick(t[4]); |
|
787 t[1].SetPriority(EPriorityRealTime); // change waiting thread priority |
|
788 // this should run right away |
|
789 NUMCHECK(1); |
|
790 IDCHECK(id1); |
|
791 t[1].SetPriority(EPriorityLess); |
|
792 // t8,t9,t10 should now be pending |
|
793 MCOUNT(M1,1-5); |
|
794 t[8].Suspend(); // this should wake up t5 |
|
795 t[9].Suspend(); // this should wake up t2 |
|
796 MCOUNT(M1,1-3); |
|
797 t[8].Suspend(); // this should have no further effect |
|
798 t[8].Resume(); // this should have no further effect |
|
799 MCOUNT(M1,1-3); |
|
800 SimpleCheck(3,id,10,5,2); |
|
801 MCOUNT(M1,1-3); |
|
802 t[3].Resume(); |
|
803 t[6].Resume(); |
|
804 t[7].Resume(); |
|
805 t[8].Resume(); |
|
806 t[9].Resume(); |
|
807 SimpleCheck(5,id,8,9,6,7,3); |
|
808 |
|
809 RackEmUp(t,f,id); |
|
810 MCOUNT(M1,-7); |
|
811 t[8].Suspend(); // this shouldn't wake anything up |
|
812 t[9].Suspend(); // this shouldn't wake anything up |
|
813 MCOUNT(M1,-7); |
|
814 Kick(t[4]); |
|
815 t[4].SetPriority(EPriorityRealTime); |
|
816 MCOUNT(M1,1-6); // should be 6 waiting, mutex free, t10 pending |
|
817 t[4].SetPriority(EPriorityNormal); |
|
818 t[10].SetPriority(EPriorityLess); // this should wake up t5 |
|
819 MCOUNT(M1,1-5); // should be 5 waiting, mutex free, t10, t5 pending |
|
820 SimpleCheck(4,id,5,10,1,2); |
|
821 t[3].SetPriority(EPriorityRealTime); // boost suspended+waiting thread |
|
822 MCOUNT(M1,1-3); // should be 3 waiting+suspended, mutex free, t8, t9 pending+suspended |
|
823 t[6].Resume(); |
|
824 t[7].Resume(); |
|
825 t[8].Resume(); |
|
826 t[9].Resume(); |
|
827 t[3].Resume(); // this should run immediately |
|
828 MCOUNT(M1,1); // t8,t9,t6,t7 pending, mutex free |
|
829 NUMCHECK(1); |
|
830 IDCHECK(id3); // t3 should have run |
|
831 t[3].SetPriority(EPriorityLess); |
|
832 t[9].SetPriority(EPriorityMuchLess); // lower pending thread priority |
|
833 SimpleCheck(4,id,8,6,7,9); |
|
834 t[9].SetPriority(EPriorityMore); |
|
835 t[10].SetPriority(EPriorityMore); |
|
836 |
|
837 RackEmUp(t,f,id); |
|
838 MCOUNT(M1,-7); |
|
839 t[8].Suspend(); // this shouldn't wake anything up |
|
840 t[9].Suspend(); // this shouldn't wake anything up |
|
841 MCOUNT(M1,-7); |
|
842 Kick(t[4]); |
|
843 MCOUNT(M1,-7); |
|
844 t[4].SetPriority(EPriorityRealTime); |
|
845 MCOUNT(M1,1-6); // should be 6 waiting, mutex free, t10 pending, t8,t9 pending+suspended |
|
846 t[4].SetPriority(EPriorityNormal); |
|
847 t[10].SetPriority(EPriorityMuchLess); // lower pending thread priority |
|
848 MCOUNT(M1,1-5); // should now be 5 waiting, mutex free, t10,t5 pending, t8,t9 pending+suspended |
|
849 t[6].Resume(); |
|
850 t[7].Resume(); |
|
851 t[3].Resume(); // this gets made READY straight away |
|
852 SimpleCheck(7,id,5,6,7,3,1,2,10); |
|
853 t[8].Resume(); |
|
854 t[9].Resume(); |
|
855 SimpleCheck(2,id,8,9); |
|
856 t[10].SetPriority(EPriorityMore); |
|
857 |
|
858 RackEmUp(t,f,id); |
|
859 MCOUNT(M1,-7); |
|
860 Kick(t[4]); |
|
861 t[9].Kill(0); // kill pending thread |
|
862 t[9].Close(); |
|
863 test(!Exists(9)); |
|
864 t[1].Kill(0); // kill waiting thread |
|
865 t[1].Close(); |
|
866 test(!Exists(1)); |
|
867 t[6].Kill(0); // kill suspended+waiting thread |
|
868 t[6].Close(); |
|
869 t[7].Resume(); |
|
870 t[3].Resume(); |
|
871 test(!Exists(6)); |
|
872 SimpleCheck(6,id,10,8,5,7,2,3); // 8 runs first and gets blocked behind 10 |
|
873 Resurrect(9,EPriorityMore,t,f,id); |
|
874 Resurrect(1,EPriorityLess,t,f,id); |
|
875 Resurrect(6,EPriorityNormal,t,f,id); |
|
876 |
|
877 RackEmUp(t,f,id); |
|
878 MCOUNT(M1,-7); |
|
879 t[8].Suspend(); // this shouldn't wake anything up |
|
880 t[9].Suspend(); // this shouldn't wake anything up |
|
881 MCOUNT(M1,-7); |
|
882 Kick(t[4]); |
|
883 MCOUNT(M1,-7); |
|
884 t[4].SetPriority(EPriorityRealTime); |
|
885 MCOUNT(M1,1-6); // should be 6 waiting, mutex free, t10 pending, t8,t9 pending+suspended |
|
886 t[4].SetPriority(EPriorityNormal); |
|
887 t[10].Kill(0); // kill pending thread - this should wake up t5 |
|
888 t[10].Close(); |
|
889 test(!Exists(10)); |
|
890 MCOUNT(M1,1-5); // should be 5 waiting, mutex free, t5 pending, t8,t9 pending+suspended |
|
891 t[5].SetPriority(EPriorityRealTime); // this should make t5 run |
|
892 MCOUNT(M1,1-4); // should be 4 waiting, mutex free, t1 pending, t8,t9 pending+suspended |
|
893 t[5].SetPriority(EPriorityNormal); |
|
894 NUMCHECK(1); |
|
895 IDCHECK(id5); |
|
896 t[8].SetPriority(EPriorityRealTime); // this shouldn't make anything happen |
|
897 MCOUNT(M1,1-4); // mutex free, t1 pending, t8,t9 pending+suspended, t3,t6,t7 wait+susp, t2 waiting |
|
898 NUMCHECK(0); |
|
899 t[8].Resume(); |
|
900 MCOUNT(M1,1-3); // mutex free, t1,t2 pending, t9 pending+suspended, t3,t6,t7 wait+susp |
|
901 NUMCHECK(1); |
|
902 IDCHECK(id8); |
|
903 t[8].SetPriority(EPriorityMore); |
|
904 t[3].Resume(); |
|
905 t[6].Resume(); |
|
906 t[7].Resume(); |
|
907 t[9].Resume(); |
|
908 SimpleCheck(6,id,9,6,7,1,2,3); |
|
909 Resurrect(10,EPriorityMore,t,f,id); |
|
910 |
|
911 RackEmUp(t,f,id); |
|
912 MCOUNT(M1,-7); |
|
913 t[8].Suspend(); // this shouldn't wake anything up |
|
914 t[9].Suspend(); // this shouldn't wake anything up |
|
915 MCOUNT(M1,-7); |
|
916 Kick(t[4]); |
|
917 MCOUNT(M1,-7); |
|
918 t[4].SetPriority(EPriorityRealTime); |
|
919 MCOUNT(M1,1-6); // mutex free, t10 pending, t8,t9 pending+susp, t3,t6,t7 wait+susp, t1,t2,t5 wait |
|
920 t[4].SetPriority(EPriorityNormal); |
|
921 t[1].SetPriority(EPriorityRealTime); // this should be able to run and claim the mutex |
|
922 NUMCHECK(1); |
|
923 IDCHECK(id1); |
|
924 MCOUNT(M1,1-4); // mutex free, t10,t5 pending, t8,t9 pending+susp, t3,t6,t7 wait+susp, t2 wait |
|
925 t[1].SetPriority(EPriorityLess); |
|
926 t[3].Resume(); |
|
927 t[6].Resume(); |
|
928 t[7].Resume(); |
|
929 t[9].Resume(); |
|
930 t[8].Resume(); |
|
931 SimpleCheck(8,id,10,9,8,5,6,7,3,2); |
|
932 |
|
933 RackEmUp(t,f,id); |
|
934 MCOUNT(M1,-7); |
|
935 Kick(t[4]); |
|
936 M1.Close(); // close the mutex - non-suspended threads should all panic with KERN-EXEC 0 |
|
937 TBool jit = User::JustInTime(); |
|
938 User::SetJustInTime(EFalse); |
|
939 User::After(1000000); |
|
940 User::SetJustInTime(jit); |
|
941 for (i=1; i<=10; ++i) |
|
942 { |
|
943 if (i==3 || i==6 || i==7) |
|
944 { |
|
945 test(t[i].ExitType()==EExitPending); |
|
946 } |
|
947 else |
|
948 { |
|
949 test(t[i].ExitType()==EExitPanic); |
|
950 test(t[i].ExitReason()==EBadHandle); |
|
951 test(t[i].ExitCategory()==_L("KERN-EXEC")); |
|
952 t[i].Close(); |
|
953 test(!Exists(i)); |
|
954 } |
|
955 } |
|
956 t[3].Resume(); |
|
957 t[6].Resume(); |
|
958 t[7].Resume(); |
|
959 User::SetJustInTime(EFalse); |
|
960 User::After(1000000); |
|
961 User::SetJustInTime(jit); |
|
962 for (i=1; i<=10; ++i) |
|
963 { |
|
964 if (i==3 || i==6 || i==7) |
|
965 { |
|
966 test(t[i].ExitType()==EExitPanic); |
|
967 test(t[i].ExitReason()==EBadHandle); |
|
968 test(t[i].ExitCategory()==_L("KERN-EXEC")); |
|
969 t[i].Close(); |
|
970 test(!Exists(i)); |
|
971 } |
|
972 } |
|
973 |
|
974 test.End(); |
|
975 } |
|
976 |
|
977 /***************************************************************************** |
|
978 * Mutex benchmarks |
|
979 *****************************************************************************/ |
|
980 TInt MutexSpeed(TAny* aPtr) |
|
981 { |
|
982 TInt& count=*(TInt*)aPtr; |
|
983 RThread().SetPriority(EPriorityMore); |
|
984 FOREVER |
|
985 { |
|
986 M1.Wait(); |
|
987 M1.Signal(); |
|
988 ++count; |
|
989 } |
|
990 } |
|
991 |
|
992 TInt MutexSpeed2(TAny* aPtr) |
|
993 { |
|
994 TInt& count=*(TInt*)aPtr; |
|
995 RThread().SetPriority(EPriorityMore); |
|
996 FOREVER |
|
997 { |
|
998 M1.Wait(); |
|
999 M1.Wait(); |
|
1000 M1.Signal(); |
|
1001 M1.Signal(); |
|
1002 ++count; |
|
1003 } |
|
1004 } |
|
1005 |
|
1006 void TestMutexSpeed() |
|
1007 { |
|
1008 test.Start(_L("Test mutex speed")); |
|
1009 TInt count=0; |
|
1010 TInt r=M1.CreateLocal(); |
|
1011 test(r==KErrNone); |
|
1012 |
|
1013 RThread t; |
|
1014 r=t.Create(_L("Speed"),MutexSpeed,0x1000,NULL,&count); |
|
1015 test(r==KErrNone); |
|
1016 t.SetPriority(EPriorityRealTime); |
|
1017 t.Resume(); |
|
1018 User::AfterHighRes(1000000); |
|
1019 t.Kill(0); |
|
1020 t.Close(); |
|
1021 test(!Exists(_L("Speed"))); |
|
1022 test.Printf(_L("%d wait/signal in 1 second\n"),count); |
|
1023 |
|
1024 TInt count2=0; |
|
1025 r=t.Create(_L("Speed2"),MutexSpeed2,0x1000,NULL,&count2); |
|
1026 test(r==KErrNone); |
|
1027 t.SetPriority(EPriorityRealTime); |
|
1028 t.Resume(); |
|
1029 User::AfterHighRes(1000000); |
|
1030 t.Kill(0); |
|
1031 t.Close(); |
|
1032 test(!Exists(_L("Speed2"))); |
|
1033 test.Printf(_L("%d double wait/signal in 1 second\n"),count2); |
|
1034 |
|
1035 M1.Close(); |
|
1036 test.End(); |
|
1037 } |
|
1038 |
|
1039 /***************************************************************************** |
|
1040 * Utilities for semaphore test |
|
1041 *****************************************************************************/ |
|
1042 void SemWait() |
|
1043 { |
|
1044 S.Wait(); |
|
1045 ++Count; |
|
1046 ThreadId[PutIx++]=(TUint)RThread().Id(); |
|
1047 } |
|
1048 |
|
1049 void SemSignal() |
|
1050 { |
|
1051 S.Signal(); |
|
1052 } |
|
1053 |
|
1054 TInt SemThreadFunction(TAny* aPtr) |
|
1055 { |
|
1056 PFV& f=*(PFV*)aPtr; |
|
1057 FOREVER |
|
1058 { |
|
1059 SemWait(); |
|
1060 if (f) |
|
1061 f(); |
|
1062 SemSignal(); |
|
1063 User::WaitForAnyRequest(); |
|
1064 } |
|
1065 } |
|
1066 |
|
1067 void Wait() |
|
1068 { |
|
1069 User::WaitForAnyRequest(); |
|
1070 } |
|
1071 |
|
1072 TUint CreateSemThread(RThread& t, TInt n, TAny* aPtr) |
|
1073 { |
|
1074 TBuf<4> b; |
|
1075 b.Num(n); |
|
1076 TInt r=t.Create(b,SemThreadFunction,0x1000,NULL,aPtr); |
|
1077 test(r==KErrNone); |
|
1078 t.Resume(); |
|
1079 TUint id=t.Id(); |
|
1080 return id; |
|
1081 } |
|
1082 |
|
1083 /* |
|
1084 Possible thread relationships with semaphore: |
|
1085 Waiting |
|
1086 Waiting + suspended |
|
1087 |
|
1088 Need to verify correct behaviour when the following actions occur for each of these states: |
|
1089 Suspend thread |
|
1090 Resume thread |
|
1091 Change thread priority |
|
1092 Thread exits |
|
1093 Thread is killed |
|
1094 Semaphore deleted |
|
1095 */ |
|
1096 |
|
1097 void RackEmUp2(RThread* t, PFV* f, TUint* id) |
|
1098 { |
|
1099 // set up |
|
1100 // t1, t2, t4, t5, t6, t8, t9 waiting |
|
1101 // t3, t7, t10 waiting+suspended |
|
1102 (void)f; |
|
1103 MCOUNT(S,2); // check semaphore level = 2 |
|
1104 SemWait(); |
|
1105 SemWait(); |
|
1106 MCOUNT(S,0); // check semaphore level = 0 |
|
1107 NUMCHECK(2); |
|
1108 IDCHECK(id0); |
|
1109 IDCHECK(id0); |
|
1110 TInt i; |
|
1111 for (i=1; i<=10; ++i) |
|
1112 Kick(t[i]); // wake up threads |
|
1113 User::After(50000); // let threads wait |
|
1114 MCOUNT(S,-10); // check 10 threads waiting |
|
1115 t[7].Resume(); // test resume when not suspended |
|
1116 MCOUNT(S,-10); // check 7 threads waiting |
|
1117 t[3].Suspend(); |
|
1118 t[7].Suspend(); |
|
1119 t[10].Suspend(); // now have required state |
|
1120 t[3].Suspend(); // suspend and resume t3 again for good measure |
|
1121 t[3].Resume(); |
|
1122 MCOUNT(S,-7); // check 7 threads waiting |
|
1123 } |
|
1124 |
|
1125 void Resurrect2(TInt n, TThreadPriority aPriority, RThread* t, PFV* f, TUint* id) |
|
1126 { |
|
1127 f[n]=NULL; |
|
1128 id[n]=CreateSemThread(t[n],n,f+n); |
|
1129 t[n].SetPriority(EPriorityRealTime); |
|
1130 t[n].SetPriority(aPriority); |
|
1131 NUMCHECK(1); |
|
1132 IDCHECK(id[n]); |
|
1133 } |
|
1134 |
|
1135 /***************************************************************************** |
|
1136 * Semaphore exhaustive state transition test |
|
1137 *****************************************************************************/ |
|
1138 void TestSemaphore() |
|
1139 { |
|
1140 test.Start(_L("Test semaphore state transitions")); |
|
1141 RThread t[11]; |
|
1142 TUint id[11]; |
|
1143 PFV f[11]={NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}; |
|
1144 id[0]=(TUint)RThread().Id(); |
|
1145 PutIx=0; |
|
1146 GetIx=0; |
|
1147 Count=0; |
|
1148 test.Next(_L("Create semaphore")); |
|
1149 TInt r=S.CreateLocal(2); |
|
1150 test(r==KErrNone); |
|
1151 MCOUNT(S,2); |
|
1152 SemWait(); |
|
1153 MCOUNT(S,1); |
|
1154 SemSignal(); |
|
1155 MCOUNT(S,2); |
|
1156 SemWait(); |
|
1157 SemWait(); |
|
1158 MCOUNT(S,0); |
|
1159 S.Signal(2); |
|
1160 MCOUNT(S,2); |
|
1161 NUMCHECK(3); |
|
1162 IDCHECK(id0); |
|
1163 IDCHECK(id0); |
|
1164 IDCHECK(id0); |
|
1165 test.Next(_L("Create threads")); |
|
1166 TInt i; |
|
1167 for (i=1; i<=10; ++i) |
|
1168 { |
|
1169 id[i]=CreateSemThread(t[i],i,f+i); |
|
1170 f[i]=&Wait; |
|
1171 } |
|
1172 t[8].SetPriority(EPriorityMore); // t1-t3=less, t4-t7=normal, t8-t10 more, t0 much more |
|
1173 t[9].SetPriority(EPriorityMore); |
|
1174 t[10].SetPriority(EPriorityMore); |
|
1175 t[1].SetPriority(EPriorityLess); |
|
1176 t[2].SetPriority(EPriorityLess); |
|
1177 t[3].SetPriority(EPriorityLess); |
|
1178 User::After(50000); |
|
1179 MCOUNT(S,-8); // check 8 waiting |
|
1180 NUMCHECK(2); |
|
1181 IDCHECK(id8); |
|
1182 IDCHECK(id9); // check t8,t9 got through |
|
1183 t[8].SetPriority(EPriorityRealTime); |
|
1184 Kick(t[8]); // let t8 run and signal |
|
1185 t[8].SetPriority(EPriorityMore); |
|
1186 MCOUNT(S,-7); // check 7 waiting |
|
1187 User::After(50000); // let next thread obtain semaphore |
|
1188 MCOUNT(S,-7); // check 7 waiting |
|
1189 NUMCHECK(1); |
|
1190 IDCHECK(id10); // check t10 got it |
|
1191 Kick(t[10]); // let t10 run and signal |
|
1192 User::After(50000); // let next thread obtain semaphore |
|
1193 MCOUNT(S,-6); // check 6 waiting |
|
1194 NUMCHECK(1); |
|
1195 IDCHECK(id4); // check t4 got it |
|
1196 t[1].SetPriority(EPriorityRealTime); // boost t1 |
|
1197 MCOUNT(S,-6); // check 6 still waiting |
|
1198 User::After(50000); // let next thread obtain semaphore |
|
1199 MCOUNT(S,-6); // check 6 still waiting |
|
1200 NUMCHECK(0); |
|
1201 Kick(t[9]); // make t9 ready to run and signal |
|
1202 MCOUNT(S,-6); // check 6 still waiting |
|
1203 User::After(50000); // let next thread obtain semaphore |
|
1204 MCOUNT(S,-5); // check 5 waiting |
|
1205 NUMCHECK(1); |
|
1206 IDCHECK(id1); // check t1 got it |
|
1207 t[1].SetPriority(EPriorityLess); |
|
1208 Kick(t[1]); // kick all remaining threads |
|
1209 Kick(t[2]); |
|
1210 Kick(t[3]); |
|
1211 Kick(t[4]); |
|
1212 Kick(t[5]); |
|
1213 Kick(t[6]); |
|
1214 Kick(t[7]); |
|
1215 User::After(50000); // let them run and obtain/signal the semaphore |
|
1216 MCOUNT(S,2); // check semaphore now back to initial level |
|
1217 SimpleCheck(5,id,5,6,7,2,3); |
|
1218 |
|
1219 for (i=1; i<=10; ++i) |
|
1220 f[i]=NULL; |
|
1221 RackEmUp2(t,f,id); // set up threads waiting on semaphore again |
|
1222 S.Signal(); |
|
1223 SimpleCheck(7,id,8,9,4,5,6,1,2); // let them go |
|
1224 MCOUNT(S,1); |
|
1225 S.Wait(); |
|
1226 t[3].SetPriority(EPriorityRealTime); // change suspended thread priority |
|
1227 t[7].Resume(); |
|
1228 SimpleCheck(0,id); // t7 should wait for signal |
|
1229 S.Signal(); |
|
1230 SimpleCheck(1,id,7); |
|
1231 MCOUNT(S,1); |
|
1232 t[3].Resume(); |
|
1233 t[10].Resume(); |
|
1234 NUMCHECK(1); |
|
1235 IDCHECK(id3); // t3 should have grabbed semaphore as soon as we resumed it |
|
1236 SimpleCheck(1,id,10); |
|
1237 t[3].SetPriority(EPriorityLess); |
|
1238 S.Signal(); // put level back to 2 |
|
1239 |
|
1240 RackEmUp2(t,f,id); // set up threads waiting on semaphore again |
|
1241 S.Signal(); |
|
1242 SimpleCheck(7,id,8,9,4,5,6,1,2); // let them go |
|
1243 MCOUNT(S,1); |
|
1244 S.Wait(); |
|
1245 t[3].SetPriority(EPriorityRealTime); // change suspended thread priority |
|
1246 t[7].Resume(); |
|
1247 SimpleCheck(0,id); // t7 should wait for signal |
|
1248 S.Signal(); |
|
1249 SimpleCheck(1,id,7); |
|
1250 MCOUNT(S,1); |
|
1251 t[10].Resume(); |
|
1252 t[3].Resume(); // t3 not woken up here since t10 has already been given the semaphore |
|
1253 NUMCHECK(0); |
|
1254 SimpleCheck(2,id,10,3); |
|
1255 t[3].SetPriority(EPriorityLess); |
|
1256 S.Signal(); // put level back to 2 |
|
1257 |
|
1258 RackEmUp2(t,f,id); // set up threads waiting on semaphore again |
|
1259 S.Signal(); |
|
1260 SimpleCheck(7,id,8,9,4,5,6,1,2); // let them go |
|
1261 MCOUNT(S,1); |
|
1262 S.Wait(); |
|
1263 t[3].SetPriority(EPriorityRealTime); // change suspended thread priority |
|
1264 t[7].Resume(); |
|
1265 SimpleCheck(0,id); // t7 should wait for signal |
|
1266 S.Signal(); |
|
1267 S.Signal(); // put level back to 2 |
|
1268 SimpleCheck(1,id,7); |
|
1269 MCOUNT(S,2); |
|
1270 t[10].Resume(); |
|
1271 t[3].Resume(); // t3 and t10 both woken up here, t3 should run and signal |
|
1272 MCOUNT(S,1); |
|
1273 NUMCHECK(1); |
|
1274 IDCHECK(id3); |
|
1275 SimpleCheck(1,id,10); |
|
1276 t[3].SetPriority(EPriorityLess); |
|
1277 |
|
1278 RackEmUp2(t,f,id); // set up threads waiting on semaphore again |
|
1279 t[9].Kill(0); // kill waiting thread |
|
1280 t[9].Close(); |
|
1281 test(!Exists(9)); |
|
1282 t[10].Kill(0); // kill suspended thread |
|
1283 t[10].Close(); |
|
1284 test(!Exists(10)); |
|
1285 MCOUNT(S,-6); |
|
1286 f[5]=&Exit; // get t5 to exit after acquiring semaphore |
|
1287 S.Signal(); |
|
1288 SimpleCheck(3,id,8,4,5); // let them go |
|
1289 MCOUNT(S,-3); // check one signal has been lost due to t5 exiting |
|
1290 t[5].Close(); |
|
1291 test(!Exists(5)); |
|
1292 t[3].Resume(); |
|
1293 t[7].Resume(); |
|
1294 MCOUNT(S,-5); |
|
1295 S.Signal(); |
|
1296 SimpleCheck(5,id,6,7,1,2,3); // let them go |
|
1297 MCOUNT(S,1); |
|
1298 Resurrect2(9,EPriorityMore,t,f,id); |
|
1299 Resurrect2(10,EPriorityMore,t,f,id); |
|
1300 Resurrect2(5,EPriorityNormal,t,f,id); |
|
1301 S.Signal(); |
|
1302 |
|
1303 RackEmUp2(t,f,id); // set up threads waiting on semaphore again |
|
1304 f[5]=&Exit; // get t5 to exit after acquiring semaphore |
|
1305 S.Close(); // close semaphore - threads should panic except for 5 |
|
1306 |
|
1307 TBool jit = User::JustInTime(); |
|
1308 User::SetJustInTime(EFalse); |
|
1309 User::After(1000000); |
|
1310 User::SetJustInTime(jit); |
|
1311 for (i=1; i<=10; ++i) |
|
1312 { |
|
1313 if (i==3 || i==7 || i==10) |
|
1314 { |
|
1315 test(t[i].ExitType()==EExitPending); |
|
1316 } |
|
1317 else if (i!=5) |
|
1318 { |
|
1319 test(t[i].ExitType()==EExitPanic); |
|
1320 test(t[i].ExitReason()==EBadHandle); |
|
1321 test(t[i].ExitCategory()==_L("KERN-EXEC")); |
|
1322 t[i].Close(); |
|
1323 test(!Exists(i)); |
|
1324 } |
|
1325 else |
|
1326 { |
|
1327 test(t[i].ExitType()==EExitKill); |
|
1328 test(t[i].ExitReason()==0); |
|
1329 t[i].Close(); |
|
1330 test(!Exists(i)); |
|
1331 } |
|
1332 } |
|
1333 t[3].Resume(); |
|
1334 t[7].Resume(); |
|
1335 t[10].Resume(); |
|
1336 User::SetJustInTime(EFalse); |
|
1337 User::After(1000000); |
|
1338 User::SetJustInTime(jit); |
|
1339 for (i=1; i<=10; ++i) |
|
1340 { |
|
1341 if (i==3 || i==7 || i==10) |
|
1342 { |
|
1343 test(t[i].ExitType()==EExitPanic); |
|
1344 test(t[i].ExitReason()==EBadHandle); |
|
1345 test(t[i].ExitCategory()==_L("KERN-EXEC")); |
|
1346 t[i].Close(); |
|
1347 test(!Exists(i)); |
|
1348 } |
|
1349 } |
|
1350 |
|
1351 test.End(); |
|
1352 } |
|
1353 |
|
1354 /***************************************************************************** |
|
1355 * Semaphore benchmarks |
|
1356 *****************************************************************************/ |
|
1357 TInt SemSpeed(TAny* aPtr) |
|
1358 { |
|
1359 TInt& count=*(TInt*)aPtr; |
|
1360 RThread().SetPriority(EPriorityMore); |
|
1361 FOREVER |
|
1362 { |
|
1363 S.Wait(); |
|
1364 S.Signal(); |
|
1365 ++count; |
|
1366 } |
|
1367 } |
|
1368 |
|
1369 void TestSemSpeed() |
|
1370 { |
|
1371 test.Start(_L("Test semaphore speed")); |
|
1372 TInt count=0; |
|
1373 TInt r=S.CreateLocal(1); |
|
1374 test(r==KErrNone); |
|
1375 |
|
1376 RThread t; |
|
1377 r=t.Create(_L("SemSpeed"),SemSpeed,0x1000,NULL,&count); |
|
1378 test(r==KErrNone); |
|
1379 t.SetPriority(EPriorityRealTime); |
|
1380 t.Resume(); |
|
1381 User::AfterHighRes(1000000); |
|
1382 t.Kill(0); |
|
1383 t.Close(); |
|
1384 test(!Exists(_L("SemSpeed"))); |
|
1385 test.Printf(_L("%d wait/signal in 1 second\n"),count); |
|
1386 |
|
1387 S.Close(); |
|
1388 test.End(); |
|
1389 } |
|
1390 |
|
1391 |
|
1392 GLDEF_C TInt E32Main() |
|
1393 { |
|
1394 test.Title(); |
|
1395 |
|
1396 test.Start(_L("Test mutexes and semaphores")); |
|
1397 RThread().SetPriority(EPriorityMuchMore); |
|
1398 TInt r=Main.Duplicate(RThread()); |
|
1399 test(r==KErrNone); |
|
1400 |
|
1401 Test0(); |
|
1402 Test1(); |
|
1403 TestMutex1(); |
|
1404 TestMutex2(); |
|
1405 TestSemaphore(); |
|
1406 |
|
1407 TestMutexSpeed(); |
|
1408 TestSemSpeed(); |
|
1409 |
|
1410 Main.Close(); |
|
1411 test.End(); |
|
1412 return KErrNone; |
|
1413 } |
|
1414 |