Update contrib.
1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\nkernsa\fastmutex.cpp
18 #include <nktest/nkutils.h>
20 const TInt KReadCount = 100000;
21 //const TInt KReadCount = 2000000;
23 const TUint32 KTickLimit = (KReadCount>100000) ? (((TUint32)KReadCount)/10*18) : 180000u;
25 const TUint32 KTickLimit = (KReadCount>100000) ? (((TUint32)KReadCount)/10*6) : 60000u;
45 NFastMutexX::NFastMutexX()
46 : iRefCount(0), iMutex(0)
49 void NFastMutexX::Create()
51 iMutex = new NFastMutex;
53 __e32_atomic_store_rel32(&iRefCount, 1);
56 NFastMutexX::~NFastMutexX()
58 TEST_RESULT2(iRefCount==0, "Bad mutex ref count %d %08x", iRefCount, this);
59 memset(this, 0xbf, sizeof(*this));
62 TBool NFastMutexX::Open()
64 return __e32_atomic_tas_ord32(&iRefCount, 1, 1, 0) > 0;
67 TBool NFastMutexX::Close()
69 TInt r = __e32_atomic_tas_ord32(&iRefCount, 1, -1, 0);
72 memset(iMutex, 0xbf, sizeof(NFastMutex));
79 TBool NFastMutexX::Wait()
83 NKern::FMWait(iMutex);
89 TBool NFastMutexX::Signal()
91 NKern::FMSignal(iMutex);
95 TBool NFastMutexX::WaitFull()
105 TBool NFastMutexX::SignalFull()
107 FMSignalFull(iMutex);
113 TEST_PRINT("Testing non-contention case");
117 TEST_RESULT(!m.HeldByCurrentThread(), "Mutex held by current thread");
118 TEST_RESULT(!NKern::HeldFastMutex(), "Current thread holds a fast mutex");
122 TEST_RESULT(m.HeldByCurrentThread(), "Mutex not held by current thread");
123 TEST_RESULT(NKern::HeldFastMutex()==&m, "HeldFastMutex() incorrect");
127 TEST_RESULT(!m.HeldByCurrentThread(), "Mutex held by current thread");
128 TEST_RESULT(!NKern::HeldFastMutex(), "Current thread holds a fast mutex");
134 volatile TUint32* iBlock;
135 TInt iBlockSize; // words
136 TInt iPriorityThreshold;
137 volatile TBool iStop;
138 NThread* iThreads[3*KMaxCpus];
141 void FMTest1Thread(TAny* a)
143 SFMTest1Info& info = *(SFMTest1Info*)a;
144 NThread* pC = NKern::CurrentThread();
145 TUint32 seed[2] = {(TUint32)pC, 0};
146 TBool wait = (pC->iPriority > info.iPriorityThreshold);
147 TBool thread0 = (pC==info.iThreads[0]);
152 NKern::ThreadSetPriority(pC, 11);
153 NKern::FMWait(&info.iMutex);
154 TBool ok = verify_block((TUint32*)info.iBlock, info.iBlockSize);
155 TEST_RESULT(ok, "Block corrupt");
157 setup_block((TUint32*)info.iBlock, info.iBlockSize);
159 NKern::FMSignal(&info.iMutex);
162 TUint32 x = random(seed) & 1;
166 TEST_PRINT2("Thread %T ran %d times", pC, n);
169 void FMTest1PInterfererThread(TAny* a)
171 SFMTest1Info& info = *(SFMTest1Info*)a;
172 NThread* pC = NKern::CurrentThread();
173 TEST_PRINT1("Thread %T start", pC);
174 TUint32 seed[2] = {(TUint32)pC, 0};
175 NThread* t0 = info.iThreads[0];
177 while (!__e32_atomic_load_acq32(&info.iStop))
179 while (!__e32_atomic_load_acq32(&info.iStop) && t0->iPriority != 11)
181 TUint32 x = random(seed) & 2047;
184 __e32_atomic_add_ord32(&x, TUint32(-1));
186 if (__e32_atomic_load_acq32(&info.iStop))
188 NKern::ThreadSetPriority(t0, 9);
191 TEST_PRINT2("Thread %T ran %d times", pC, n);
196 TEST_PRINT("Testing mutual exclusion");
198 NFastSemaphore exitSem(0);
199 SFMTest1Info* pI = new SFMTest1Info;
201 memclr(pI, sizeof(SFMTest1Info));
202 pI->iBlockSize = 256;
203 pI->iBlock = (TUint32*)malloc(pI->iBlockSize*sizeof(TUint32));
204 TEST_OOM(pI->iBlock);
205 pI->iPriorityThreshold = 10;
207 setup_block((TUint32*)pI->iBlock, pI->iBlockSize);
213 CreateThreadSignalOnExit("FMTest1H", &FMTest1Thread, 11, pI, 0, KSmallTimeslice, &exitSem, cpu);
214 CreateThreadSignalOnExit("FMTest1L0", &FMTest1Thread, 10, pI, 0, KSmallTimeslice, &exitSem, cpu);
215 CreateThreadSignalOnExit("FMTest1L1", &FMTest1Thread, 10, pI, 0, KSmallTimeslice, &exitSem, cpu);
221 TEST_PRINT1("%d", pI->iBlock[0]);
222 if (pI->iBlock[0] > 65536)
229 NKern::FSWait(&exitSem);
230 TEST_PRINT1("Total iterations %d", pI->iBlock[0]);
231 free((TAny*)pI->iBlock);
237 TEST_PRINT("Testing priority change");
238 if (NKern::NumberOfCpus()==1)
241 NFastSemaphore exitSem(0);
242 SFMTest1Info* pI = new SFMTest1Info;
244 memclr(pI, sizeof(SFMTest1Info));
245 TEST_PRINT1("Info@0x%08x", pI);
246 pI->iBlockSize = 256;
247 pI->iBlock = (TUint32*)malloc(pI->iBlockSize*sizeof(TUint32));
248 TEST_OOM(pI->iBlock);
249 pI->iPriorityThreshold = 9;
251 setup_block((TUint32*)pI->iBlock, pI->iBlockSize);
254 TInt threadCount = 0;
256 char name[16] = "FMTest1P.0";
259 name[9] = (char)(threadCount + '0');
261 pI->iThreads[threadCount] = CreateThreadSignalOnExit("FMTest1PInterferer", &FMTest1PInterfererThread, 12, pI, 0, KSmallTimeslice, &exitSem, 1);
263 pI->iThreads[threadCount] = CreateThreadSignalOnExit(name, &FMTest1Thread, pri, pI, 0, KSmallTimeslice, &exitSem, cpu);
267 TUint32 b0 = 0xffffffffu;
271 TUint32 b = pI->iBlock[0];
272 TEST_PRINT1("%d", b);
284 while (threadCount--)
285 NKern::FSWait(&exitSem);
286 TEST_PRINT1("Total iterations %d", pI->iBlock[0]);
287 free((TAny*)pI->iBlock);
300 SFMTest2InfoC* iCommon;
304 TUint32 iBlockTimeMask;
305 TUint32 iBlockTimeOffset;
309 TUint32 iDelayThreshold;
313 TBool StopTest = FALSE;
315 void FMTest2Thread(TAny* a)
317 SFMTest2InfoT& t = *(SFMTest2InfoT*)a;
318 SFMTest2InfoC& c = *t.iCommon;
319 NThreadBase* pC = NKern::CurrentThread();
320 TUint32 seed[2] = {(TUint32)pC, 0};
326 nfcfspin(t.iSpinTime);
330 TUint32 initial = norm_fast_counter();
331 NKern::FMWait(&c.iMutex);
332 TUint32 final = norm_fast_counter();
333 TUint32 delay = final - initial;
334 if (delay > t.iMaxDelay)
337 __e32_atomic_add_ord32(&c.iMax, 1);
338 if (delay > t.iDelayThreshold)
341 nfcfspin(t.iSpinTime);
342 NKern::FMSignal(&c.iMutex);
344 if (t.iBlockTimeMask)
346 TUint32 sleep = (random(seed) & t.iBlockTimeMask) + t.iBlockTimeOffset;
350 TEST_PRINT3("Thread %T %d iterations, max delay %d", pC, t.iIterations, t.iMaxDelay);
353 SFMTest2InfoT* CreateFMTest2Thread( const char* aName,
354 SFMTest2InfoC& aCommon,
356 TUint32 aBlockTimeMask,
357 TUint32 aBlockTimeOffset,
361 NFastSemaphore& aExitSem,
365 SFMTest2InfoT* ti = new SFMTest2InfoT;
367 ti->iCommon = &aCommon;
369 ti->iDelayThreshold = 0xffffffffu;
371 ti->iSpinTime = aSpinTime;
372 ti->iBlockTimeMask = aBlockTimeMask;
373 ti->iBlockTimeOffset = aBlockTimeOffset;
374 ti->iSpoiler = (TUint8)aSpoiler;
377 NThread* t = CreateUnresumedThreadSignalOnExit(aName, &FMTest2Thread, aPri, ti, 0, aTimeslice, &aExitSem, aCpu);
379 DEBUGPRINT("Thread at %08x, Info at %08x", t, ti);
384 extern void DebugPrint(const char*, int);
387 TEST_PRINT("Testing priority inheritance");
389 NFastSemaphore exitSem(0);
390 SFMTest2InfoC common;
392 common.iStop = FALSE;
395 SFMTest2InfoT* tinfo[32];
396 memset(tinfo, 0, sizeof(tinfo));
397 DEBUGPRINT("Common info at %08x", &common);
400 tinfo[threads++] = CreateFMTest2Thread("FMTest2H", common, 500, 7, 7, FALSE, 60-cpu, KSmallTimeslice, exitSem, cpu);
401 tinfo[threads++] = CreateFMTest2Thread("FMTest2L", common, 500, 0, 0, FALSE, 11, KSmallTimeslice, exitSem, cpu);
402 tinfo[threads++] = CreateFMTest2Thread("FMTest2S", common, 10000, 15, 31, TRUE, 32, -1, exitSem, cpu);
404 tinfo[0]->iDelayThreshold = 0x300;
408 for (i=0; i<threads; ++i)
410 NKern::ThreadResume(tinfo[i]->iThread);
415 DebugPrint(".",1); // only print one char since interrupts are disabled for entire time
417 TInt max_now = common.iMax;
430 for (i=0; i<threads; ++i)
431 NKern::FSWait(&exitSem);
432 DebugPrint("\r\n",2);
433 for (i=0; i<threads; ++i)
435 TEST_PRINT3("%d: Iter %10d Max %10d", i, tinfo[i]->iIterations, tinfo[i]->iMaxDelay);
438 TEST_RESULT(tinfo[0]->iMaxDelay < 700, "Thread 0 MaxDelay too high");
442 TEST_RESULT(tinfo[3]->iMaxDelay < 1200, "Thread 1 MaxDelay too high");
445 for (i=0; i<threads; ++i)
451 void DoInOp(TUint aWhich);
452 void DoOutOp(TUint aWhich);
456 volatile TUint32 iWrites;
457 volatile TUint32 iIn;
458 volatile TBool iStop;
461 TUint32 iInSeq; // do nibble 0 followed by nibble 1 followed by nibble 2
462 TUint32 iOutSeq; // 0=nothing, 1=mutex, 2=freeze, 3=CS, 4=mutex the long way
463 // 5 = mutexX, 6 = mutexX the long way, 7=join frozen group
464 // 8 = join mutex-holding group, 9=join idle group
469 NFastSemaphore iHandshake;
470 TUint64 iInitFastCounter;
471 TUint32 iFastCounterDelta;
472 NThread* volatile iIvThrd;
475 NThreadGroup* iGroup;
479 void SWriterInfo::DoInOp(TUint aWhich)
481 switch ((iInSeq>>(aWhich*4))&0xf)
484 case 1: NKern::FMWait(iM); break;
485 case 2: iFrz=NKern::FreezeCpu(); break;
486 case 3: NKern::ThreadEnterCS(); break;
487 case 4: FMWaitFull(iM); break;
488 case 5: iMX->Wait(); break;
489 case 6: iMX->WaitFull(); break;
493 case 9: NKern::JoinGroup(iGroup); break;
498 void SWriterInfo::DoOutOp(TUint aWhich)
500 switch ((iOutSeq>>(aWhich*4))&0xf)
503 case 1: NKern::FMSignal(iM); break;
504 case 2: NKern::EndFreezeCpu(iFrz); break;
505 case 3: NKern::ThreadLeaveCS(); break;
506 case 4: FMSignalFull(iM); break;
507 case 5: iMX->Signal(); break;
508 case 6: iMX->SignalFull(); break;
512 case 9: NKern::LeaveGroup(); break;
533 volatile TUint32 iReads;
534 volatile TUint32 iFails[7];
535 volatile TBool iStop;
537 NThread* volatile iWriter;
538 NThread* volatile iReader;
539 NThread* volatile iIvThrd;
541 SWriterInfo* iWriterInfo;
543 NFastSemaphore iExitSem;
544 volatile TUint32 iCapturedIn;
545 volatile TBool iSuspendResult;
548 void WriterThread(TAny* a)
550 SWriterInfo& info = *(SWriterInfo*)a;
551 // TEST_PRINT(">WR");
555 NThread* t = (NThread*)__e32_atomic_swp_ord_ptr(&info.iIvThrd, 0);
557 NKern::ThreadRequestSignal(t);
558 if (!info.iFastCounterDelta)
559 info.iInitFastCounter = fast_counter();
560 TInt n = ++info.iWrites;
565 setup_block_cpu(info.iBuf[0], info.iWords);
570 setup_block_cpu(info.iBuf[1], info.iWords);
574 if (NKern::CurrentCpu() == info.iCpu)
577 setup_block_cpu(info.iBuf[2], info.iWords);
582 setup_block_cpu(info.iBuf[3], info.iWords);
587 setup_block_cpu(info.iBuf[4], info.iWords);
592 setup_block_cpu(info.iBuf[5], info.iWords);
594 if (!info.iFastCounterDelta)
595 info.iFastCounterDelta = (TUint32)(fast_counter() - info.iInitFastCounter);
596 if (NKern::CurrentCpu() != info.iCpu)
598 NKern::FSSignal(&info.iHandshake);
599 NKern::WaitForAnyRequest();
602 // TEST_PRINT("<WR");
605 void ReaderThread(TAny* a)
607 SReaderInfo& info = *(SReaderInfo*)a;
608 SWriterInfo& winfo = *info.iWriterInfo;
609 TInt this_cpu = NKern::CurrentCpu();
610 NThread* pC = NKern::CurrentThread();
612 // TInt my_pri = pC->i_NThread_BasePri;
613 TBool create_writer = TRUE;
614 NKern::FSSetOwner(&winfo.iHandshake, 0);
615 NFastSemaphore exitSem(0);
616 TUint32 seed[2] = {0,7};
619 // TEST_PRINT1(">RD%d",info.iTestType);
625 goto do_create_writer;
626 if (info.iTestType==SReaderInfo::EMigrate || info.iTestType==SReaderInfo::EInterlockedMigrate)
628 NKern::FSWait(&winfo.iHandshake);
632 TInt cpu = verify_block_cpu_no_trace(info.iBuf[i], info.iWords);
637 switch (info.iTestType)
639 case SReaderInfo::ETimeslice:
640 NKern::ThreadSetTimeslice(info.iWriter, (random(seed) % modulus + offset) );
641 NKern::YieldTimeslice();
643 case SReaderInfo::ESuspend:
644 winfo.iIvThrd = info.iIvThrd;
645 NKern::ThreadResume(info.iWriter);
647 case SReaderInfo::EKill:
648 NKern::FSWait(&exitSem);
649 create_writer = TRUE;
651 case SReaderInfo::EMigrate:
652 NKern::ThreadSetCpuAffinity(info.iWriter, this_cpu);
654 NKern::ThreadSetCpuAffinity(info.iGroupThrd, this_cpu);
655 NKern::ThreadRequestSignal(info.iIvThrd);
656 NKern::ThreadRequestSignal(info.iWriter);
658 case SReaderInfo::EInterlockedSuspend:
659 NKern::WaitForAnyRequest();
660 NKern::FMWait(winfo.iM);
661 if (winfo.iIn != info.iCapturedIn && info.iSuspendResult)
663 winfo.iIvThrd = info.iIvThrd;
664 NKern::ThreadResume(info.iWriter, winfo.iM);
666 case SReaderInfo::EInterlockedKill:
667 NKern::WaitForAnyRequest();
668 NKern::FSWait(&exitSem);
669 if (winfo.iIn != info.iCapturedIn)
671 create_writer = TRUE;
673 case SReaderInfo::EInterlockedMigrate:
674 NKern::WaitForAnyRequest();
675 if (winfo.iIn != info.iCapturedIn)
677 NKern::ThreadSetCpuAffinity(info.iWriter, this_cpu);
679 NKern::ThreadSetCpuAffinity(info.iGroupThrd, this_cpu);
680 NKern::ThreadRequestSignal(info.iIvThrd);
681 NKern::ThreadRequestSignal(info.iWriter);
687 create_writer = FALSE;
688 winfo.iCpu = this_cpu;
689 info.iWriter = CreateUnresumedThreadSignalOnExit("Writer", &WriterThread, winfo.iPriority, &winfo, 0, winfo.iTimeslice, &exitSem, this_cpu);
690 TEST_OOM(info.iWriter);
691 winfo.iIvThrd = info.iIvThrd;
692 NKern::ThreadResume(info.iWriter);
693 while (!winfo.iFastCounterDelta)
695 modulus = __fast_counter_to_timeslice_ticks(3*winfo.iFastCounterDelta);
696 // offset = __microseconds_to_timeslice_ticks(64);
701 NKern::FSWait(&exitSem);
702 // TEST_PRINT1("<RD%d",info.iTestType);
705 void InterventionThread(TAny* a)
707 SReaderInfo& info = *(SReaderInfo*)a;
708 SWriterInfo& winfo = *info.iWriterInfo;
709 TInt this_cpu = NKern::CurrentCpu();
710 TUint32 seed[2] = {1,0};
711 while (!winfo.iFastCounterDelta)
713 TUint32 modulus = 3*winfo.iFastCounterDelta;
714 TUint32 offset = TUint32(fast_counter_freq() / TUint64(100000));
715 NThread* w = info.iWriter;
717 TUint32 tc = NKern::TickCount();
718 NKern::FSSetOwner(&info.iExitSem, 0);
720 TEST_PRINT3(">IV%d %d %d", info.iTestType, modulus, offset);
723 if (this_cpu == winfo.iCpu)
729 TUint32 count = random(seed) % modulus;
733 if (info.iReads >= info.iReadLimit)
737 NKern::FSWait(&info.iExitSem);
740 if (winfo.iWrites >= lw + 3*info.iReadLimit)
742 lw += 3*info.iReadLimit;
743 TEST_PRINT1("#W=%d",winfo.iWrites);
745 TUint32 tc2 = NKern::TickCount();
746 if ( (tc2 - (tc+KTickLimit)) < 0x80000000 )
749 TEST_PRINT1("##W=%d",winfo.iWrites);
750 DumpMemory("WriterThread", w, 0x400);
752 switch (info.iTestType)
754 case SReaderInfo::ETimeslice:
756 case SReaderInfo::ESuspend:
757 NKern::ThreadSuspend(info.iWriter, 1);
758 NKern::WaitForAnyRequest();
760 case SReaderInfo::EKill:
764 NKern::ThreadKill(w);
765 NKern::WaitForAnyRequest();
768 case SReaderInfo::EMigrate:
769 NKern::ThreadSetCpuAffinity(info.iWriter, this_cpu);
771 NKern::ThreadSetCpuAffinity(info.iGroupThrd, this_cpu);
772 NKern::WaitForAnyRequest();
774 case SReaderInfo::EInterlockedSuspend:
777 extern TLinAddr __LastIrqRet;
778 extern TLinAddr __LastSSP;
779 extern TLinAddr __SSTop;
780 extern TUint32 __CaptureStack[1024];
781 extern TLinAddr __InterruptedThread;
782 extern TUint32 __CaptureThread[1024];
784 NKern::FMWait(winfo.iM);
785 info.iCapturedIn = winfo.iIn;
786 info.iSuspendResult = NKern::ThreadSuspend(info.iWriter, 1);
787 NKern::FMSignal(winfo.iM);
788 NKern::ThreadRequestSignal(info.iReader);
790 NThread* pC = NKern::CurrentThread();
791 TUint32 tc0 = NKern::TickCount();
795 TUint32 tc1 = NKern::TickCount();
796 if ((tc1-tc0)<0x80000000u)
798 DEBUGPRINT("__LastIrqRet = %08x", __LastIrqRet);
799 DEBUGPRINT("__LastSSP = %08x", __LastSSP);
800 DEBUGPRINT("__SSTop = %08x", __SSTop);
802 DumpMemory("WriterStack", __CaptureStack, __SSTop - __LastSSP);
804 DumpMemory("CaptureThread", __CaptureThread, sizeof(NThread));
806 DumpMemory("Writer", info.iWriter, sizeof(NThread));
808 DumpMemory("Reader", info.iReader, sizeof(NThread));
810 DumpMemory("SubSched0", &TheSubSchedulers[0], sizeof(TSubScheduler));
812 if (pC->iRequestSemaphore.iCount>0)
816 NKern::WaitForAnyRequest();
819 case SReaderInfo::EInterlockedKill:
821 NKern::FMWait(winfo.iM);
822 info.iCapturedIn = winfo.iIn;
825 NKern::ThreadKill(w, winfo.iM);
826 NKern::ThreadRequestSignal(info.iReader);
827 NKern::WaitForAnyRequest();
830 case SReaderInfo::EInterlockedMigrate:
831 NKern::FMWait(winfo.iM);
832 info.iCapturedIn = winfo.iIn;
833 NKern::ThreadSetCpuAffinity(info.iWriter, this_cpu);
835 NKern::ThreadSetCpuAffinity(info.iGroupThrd, this_cpu);
836 NKern::FMSignal(winfo.iM);
837 NKern::ThreadRequestSignal(info.iReader);
838 NKern::WaitForAnyRequest();
842 TEST_PRINT1("<IV%d",info.iTestType);
845 // State bits 0-7 show how many times timeslices are blocked
846 // State bits 8-15 show how many times suspend/kill are blocked
847 // State bits 16-23 show how many times migration is blocked
848 // State bit 24 set if in CS when fast mutex held
849 // State bit 25 set if CPU frozen when fast mutex held
850 TUint32 UpdateState(TUint32 aS, TUint32 aOp, TBool aOut)
890 return (aS+0x010101)|x;
896 void CheckResults(SReaderInfo& info)
898 SWriterInfo& winfo = *info.iWriterInfo;
901 memset(c, 32, sizeof(c)), c[71]=0;
902 state[0] = UpdateState(0, (winfo.iInSeq)&0xf, FALSE);
903 state[1] = UpdateState(state[0], (winfo.iInSeq>>4)&0xf, FALSE);
904 state[2] = UpdateState(state[1], (winfo.iInSeq>>8)&0xf, FALSE);
905 state[3] = UpdateState(state[2], (winfo.iOutSeq)&0xf, TRUE);
906 state[4] = UpdateState(state[3], (winfo.iOutSeq>>4)&0xf, TRUE);
907 state[5] = UpdateState(state[4], (winfo.iOutSeq>>8)&0xf, TRUE);
908 state[6] = (state[5] & 0xff000000) ^ 0x07000000;
912 state[i] &= 0x00ffffff;
914 TEST_PRINT2("Reads %d Writes %d", info.iReads, winfo.iWrites);
917 if (state[i] & 0xff00)
919 if (state[i] & 0xff0000)
925 TEST_PRINT7("F0 %6d F1 %6d F2 %6d F3 %6d F4 %6d F5 %6d F6 %6d", info.iFails[0], info.iFails[1], info.iFails[2], info.iFails[3], info.iFails[4], info.iFails[5], info.iFails[6]);
926 memset(c, 32, sizeof(c)), c[71]=0;
928 switch(info.iTestType)
930 case SReaderInfo::ETimeslice: mask = 0x040000ff; break;
931 case SReaderInfo::ESuspend: mask = 0x0400ff00; break;
932 case SReaderInfo::EKill: mask = 0x0400ff00; break;
933 case SReaderInfo::EMigrate: mask = 0x04ff0000; break;
934 case SReaderInfo::EInterlockedSuspend: mask = 0x0400ff00; break;
935 case SReaderInfo::EInterlockedKill: mask = 0x0100ff00; break;
936 case SReaderInfo::EInterlockedMigrate: mask = 0x02ff0000; break;
938 TUint32 limit = info.iReads/10;
944 bad = (info.iFails[i] > 0);
946 bad = (info.iFails[i] < limit);
965 TEST_RESULT(0,"FAILED");
969 struct SGroupThreadInfo
975 void GroupThread(TAny* a)
977 SGroupThreadInfo& info = *(SGroupThreadInfo*)a;
980 for (i = 0; i<3; ++i)
982 // Find the first nibble that asks for a group option
983 // and do what it asks for.
984 switch ((info.iInSeq>>(i*4))&0xf)
987 frz = NKern::FreezeCpu();
988 NKern::WaitForAnyRequest();
989 NKern::EndFreezeCpu(frz);
992 NKern::FMWait(&mutex);
993 while (__e32_atomic_load_acq32(&info.iRun))
995 NKern::FMSignal(&mutex);
999 // We weren't needed, but we have to wait to die anyway to avoid lifetime issues
1000 NKern::WaitForAnyRequest();
1003 void DoRWTest(TInt aTestType, TUint32 aReadLimit, TUint32 aInSeq, TUint32 aOutSeq, TInt aRWCpu, TInt aICpu)
1006 SWriterInfo* winfo = new SWriterInfo;
1008 memclr(winfo, sizeof(SWriterInfo));
1009 SReaderInfo* info = new SReaderInfo;
1011 memclr(info, sizeof(SReaderInfo));
1012 SGroupThreadInfo* gtinfo = new SGroupThreadInfo;
1014 memclr(gtinfo, sizeof(SGroupThreadInfo));
1015 TUint32 bufwords = 256;
1016 TUint32* buf = (TUint32*)malloc(6 * bufwords * sizeof(TUint32));
1018 memclr(buf, 6 * bufwords * sizeof(TUint32));
1022 info->iBuf[i] = buf + i * bufwords;
1023 winfo->iBuf[i] = buf + i * bufwords;
1025 winfo->iWords = bufwords;
1027 winfo->iInSeq = aInSeq;
1028 winfo->iOutSeq = aOutSeq;
1029 winfo->iPriority = 11;
1030 winfo->iTimeslice = __microseconds_to_timeslice_ticks(10000);
1031 winfo->iCpu = aRWCpu;
1033 NFastSemaphore localExit(0);
1037 SNThreadGroupCreateInfo ginfo;
1038 ginfo.iCpuAffinity = aRWCpu;
1039 TInt r = NKern::GroupCreate(&group, ginfo);
1040 TEST_RESULT(r==KErrNone, "");
1041 winfo->iGroup = &group;
1043 gtinfo->iInSeq = aInSeq;
1044 NThread* groupThrd = CreateThreadSignalOnExit("GroupThrd", &GroupThread, 1, gtinfo, 0, KSmallTimeslice, &localExit, aRWCpu, &group);
1045 TEST_OOM(groupThrd);
1046 info->iGroupThrd = groupThrd;
1050 info->iWords = bufwords;
1051 info->iReadLimit = aReadLimit;
1052 info->iWriterInfo = winfo;
1053 info->iTestType = aTestType;
1055 TInt rpri = (aTestType == SReaderInfo::ETimeslice) ? 11 : 10;
1056 NThread* reader = CreateThreadSignalOnExit("Reader", &ReaderThread, rpri, info, 0, -1, &info->iExitSem, aRWCpu);
1058 info->iReader = reader;
1060 NThread* ivt = CreateThreadSignalOnExit("Intervention", &InterventionThread, 12, info, 0, KSmallTimeslice, &localExit, aICpu);
1062 info->iIvThrd = ivt;
1064 NKern::FSWait(&localExit);
1066 NKern::ThreadRequestSignal(groupThrd);
1068 __e32_atomic_store_rel32(>info->iRun, 0);
1069 NKern::FSWait(&localExit);
1072 NKern::GroupDestroy(&group);
1077 TEST_PRINT6("Type %d RL %d ISEQ %03x OSEQ %03x RWCPU %d ICPU %d", aTestType, aReadLimit, aInSeq, aOutSeq, aRWCpu, aICpu);
1078 CheckResults(*info);
1086 void TestFastMutex()
1088 TEST_PRINT("Testing Fast Mutexes...");
1096 void TestSuspendKillMigrate()
1098 TEST_PRINT("Testing Suspend/Kill/Migrate...");
1100 DoRWTest(SReaderInfo::ETimeslice, KReadCount, 0x000, 0x000, 0, 1);
1101 DoRWTest(SReaderInfo::ETimeslice, KReadCount, 0x010, 0x100, 0, 1);
1102 DoRWTest(SReaderInfo::ETimeslice, KReadCount, 0x040, 0x400, 0, 1);
1103 DoRWTest(SReaderInfo::ETimeslice, KReadCount, 0x132, 0x231, 0, 1);
1104 DoRWTest(SReaderInfo::ETimeslice, KReadCount, 0x432, 0x234, 0, 1);
1105 DoRWTest(SReaderInfo::ETimeslice, KReadCount, 0x310, 0x310, 0, 1);
1106 DoRWTest(SReaderInfo::ETimeslice, KReadCount, 0x340, 0x340, 0, 1);
1108 DoRWTest(SReaderInfo::ESuspend, KReadCount, 0x000, 0x000, 0, 1);
1109 DoRWTest(SReaderInfo::ESuspend, KReadCount, 0x010, 0x100, 0, 1);
1110 DoRWTest(SReaderInfo::ESuspend, KReadCount, 0x040, 0x400, 0, 1);
1111 DoRWTest(SReaderInfo::ESuspend, KReadCount, 0x132, 0x231, 0, 1);
1112 DoRWTest(SReaderInfo::ESuspend, KReadCount, 0x432, 0x234, 0, 1);
1113 DoRWTest(SReaderInfo::ESuspend, KReadCount, 0x310, 0x310, 0, 1);
1114 DoRWTest(SReaderInfo::ESuspend, KReadCount, 0x340, 0x340, 0, 1);
1116 DoRWTest(SReaderInfo::EKill, KReadCount, 0x000, 0x000, 0, 1);
1117 DoRWTest(SReaderInfo::EKill, KReadCount, 0x010, 0x100, 0, 1);
1118 DoRWTest(SReaderInfo::EKill, KReadCount, 0x040, 0x400, 0, 1);
1119 DoRWTest(SReaderInfo::EKill, KReadCount, 0x132, 0x231, 0, 1);
1120 DoRWTest(SReaderInfo::EKill, KReadCount, 0x432, 0x234, 0, 1);
1121 DoRWTest(SReaderInfo::EKill, KReadCount, 0x310, 0x310, 0, 1);
1122 DoRWTest(SReaderInfo::EKill, KReadCount, 0x340, 0x340, 0, 1);
1124 DoRWTest(SReaderInfo::EMigrate, KReadCount, 0x000, 0x000, 0, 1);
1125 DoRWTest(SReaderInfo::EMigrate, KReadCount, 0x132, 0x231, 0, 1);
1126 DoRWTest(SReaderInfo::EMigrate, KReadCount, 0x432, 0x234, 0, 1);
1127 DoRWTest(SReaderInfo::EMigrate, KReadCount, 0x020, 0x200, 0, 1);
1128 DoRWTest(SReaderInfo::EMigrate, KReadCount, 0x010, 0x100, 0, 1);
1129 DoRWTest(SReaderInfo::EMigrate, KReadCount, 0x040, 0x400, 0, 1);
1130 DoRWTest(SReaderInfo::EMigrate, KReadCount, 0x030, 0x300, 0, 1);
1132 DoRWTest(SReaderInfo::EInterlockedSuspend, KReadCount, 0x432, 0x234, 0, 1);
1133 DoRWTest(SReaderInfo::EInterlockedSuspend, KReadCount, 0x040, 0x400, 0, 1);
1134 DoRWTest(SReaderInfo::EInterlockedSuspend, KReadCount, 0x010, 0x100, 0, 1);
1135 DoRWTest(SReaderInfo::EInterlockedSuspend, KReadCount, 0x340, 0x340, 0, 1);
1136 DoRWTest(SReaderInfo::EInterlockedSuspend, KReadCount, 0x310, 0x310, 0, 1);
1137 DoRWTest(SReaderInfo::EInterlockedSuspend, KReadCount, 0x132, 0x231, 0, 1);
1139 DoRWTest(SReaderInfo::EInterlockedKill, KReadCount, 0x040, 0x400, 0, 1);
1140 DoRWTest(SReaderInfo::EInterlockedKill, KReadCount, 0x010, 0x100, 0, 1);
1141 DoRWTest(SReaderInfo::EInterlockedKill, KReadCount, 0x432, 0x234, 0, 1);
1142 DoRWTest(SReaderInfo::EInterlockedKill, KReadCount, 0x340, 0x340, 0, 1);
1143 DoRWTest(SReaderInfo::EInterlockedKill, KReadCount, 0x132, 0x231, 0, 1);
1144 DoRWTest(SReaderInfo::EInterlockedKill, KReadCount, 0x310, 0x310, 0, 1);
1146 DoRWTest(SReaderInfo::EInterlockedMigrate, KReadCount, 0x040, 0x400, 0, 1);
1147 DoRWTest(SReaderInfo::EInterlockedMigrate, KReadCount, 0x010, 0x100, 0, 1);
1148 DoRWTest(SReaderInfo::EInterlockedMigrate, KReadCount, 0x310, 0x310, 0, 1);
1149 DoRWTest(SReaderInfo::EInterlockedMigrate, KReadCount, 0x340, 0x340, 0, 1);
1150 DoRWTest(SReaderInfo::EInterlockedMigrate, KReadCount, 0x132, 0x231, 0, 1);
1151 DoRWTest(SReaderInfo::EInterlockedMigrate, KReadCount, 0x432, 0x234, 0, 1);
1152 DoRWTest(SReaderInfo::EInterlockedMigrate, KReadCount, 0x120, 0x210, 0, 1);
1153 DoRWTest(SReaderInfo::EInterlockedMigrate, KReadCount, 0x420, 0x240, 0, 1);
1156 // Tests from above that involve freezing, except by joining a frozen group instead
1157 DoRWTest(SReaderInfo::ETimeslice, KReadCount, 0x137, 0x731, 0, 1);
1158 DoRWTest(SReaderInfo::ETimeslice, KReadCount, 0x437, 0x734, 0, 1);
1160 DoRWTest(SReaderInfo::ESuspend, KReadCount, 0x137, 0x731, 0, 1);
1161 DoRWTest(SReaderInfo::ESuspend, KReadCount, 0x437, 0x734, 0, 1);
1163 DoRWTest(SReaderInfo::EKill, KReadCount, 0x137, 0x731, 0, 1);
1164 DoRWTest(SReaderInfo::EKill, KReadCount, 0x437, 0x734, 0, 1);
1166 DoRWTest(SReaderInfo::EMigrate, KReadCount, 0x137, 0x731, 0, 1);
1167 DoRWTest(SReaderInfo::EMigrate, KReadCount, 0x437, 0x734, 0, 1);
1168 DoRWTest(SReaderInfo::EMigrate, KReadCount, 0x070, 0x700, 0, 1);
1170 DoRWTest(SReaderInfo::EInterlockedSuspend, KReadCount, 0x437, 0x734, 0, 1);
1171 DoRWTest(SReaderInfo::EInterlockedSuspend, KReadCount, 0x137, 0x731, 0, 1);
1173 DoRWTest(SReaderInfo::EInterlockedKill, KReadCount, 0x437, 0x734, 0, 1);
1174 DoRWTest(SReaderInfo::EInterlockedKill, KReadCount, 0x137, 0x731, 0, 1);
1176 DoRWTest(SReaderInfo::EInterlockedMigrate, KReadCount, 0x137, 0x731, 0, 1);
1177 DoRWTest(SReaderInfo::EInterlockedMigrate, KReadCount, 0x437, 0x734, 0, 1);
1178 DoRWTest(SReaderInfo::EInterlockedMigrate, KReadCount, 0x170, 0x710, 0, 1);
1179 DoRWTest(SReaderInfo::EInterlockedMigrate, KReadCount, 0x470, 0x740, 0, 1);
1181 // Tests from above that involve freezing, except by joining a group with a mutex-holder instead
1182 DoRWTest(SReaderInfo::ETimeslice, KReadCount, 0x138, 0x831, 0, 1);
1183 DoRWTest(SReaderInfo::ETimeslice, KReadCount, 0x438, 0x834, 0, 1);
1185 DoRWTest(SReaderInfo::ESuspend, KReadCount, 0x138, 0x831, 0, 1);
1186 DoRWTest(SReaderInfo::ESuspend, KReadCount, 0x438, 0x834, 0, 1);
1188 DoRWTest(SReaderInfo::EKill, KReadCount, 0x138, 0x831, 0, 1);
1189 DoRWTest(SReaderInfo::EKill, KReadCount, 0x438, 0x834, 0, 1);
1191 DoRWTest(SReaderInfo::EMigrate, KReadCount, 0x138, 0x831, 0, 1);
1192 DoRWTest(SReaderInfo::EMigrate, KReadCount, 0x438, 0x834, 0, 1);
1193 DoRWTest(SReaderInfo::EMigrate, KReadCount, 0x080, 0x800, 0, 1);
1195 DoRWTest(SReaderInfo::EInterlockedSuspend, KReadCount, 0x438, 0x834, 0, 1);
1196 DoRWTest(SReaderInfo::EInterlockedSuspend, KReadCount, 0x138, 0x831, 0, 1);
1198 DoRWTest(SReaderInfo::EInterlockedKill, KReadCount, 0x438, 0x834, 0, 1);
1199 DoRWTest(SReaderInfo::EInterlockedKill, KReadCount, 0x138, 0x831, 0, 1);
1201 DoRWTest(SReaderInfo::EInterlockedMigrate, KReadCount, 0x138, 0x831, 0, 1);
1202 DoRWTest(SReaderInfo::EInterlockedMigrate, KReadCount, 0x438, 0x834, 0, 1);
1203 DoRWTest(SReaderInfo::EInterlockedMigrate, KReadCount, 0x180, 0x810, 0, 1);
1204 DoRWTest(SReaderInfo::EInterlockedMigrate, KReadCount, 0x480, 0x840, 0, 1);
1206 // Tests from above that have a noop, except join a group that's doing nothing instead
1207 // Most of these do "join group, other op, leave group, undo other op" - this is
1208 // supposed to work, even though you can't *join* a group while frozen or holding a mutex
1209 DoRWTest(SReaderInfo::ETimeslice, KReadCount, 0x090, 0x900, 0, 1);
1210 DoRWTest(SReaderInfo::ETimeslice, KReadCount, 0x019, 0x190, 0, 1);
1211 DoRWTest(SReaderInfo::ETimeslice, KReadCount, 0x049, 0x490, 0, 1);
1212 DoRWTest(SReaderInfo::ETimeslice, KReadCount, 0x319, 0x319, 0, 1);
1213 DoRWTest(SReaderInfo::ETimeslice, KReadCount, 0x349, 0x349, 0, 1);
1215 DoRWTest(SReaderInfo::ESuspend, KReadCount, 0x090, 0x900, 0, 1);
1216 DoRWTest(SReaderInfo::ESuspend, KReadCount, 0x019, 0x190, 0, 1);
1217 DoRWTest(SReaderInfo::ESuspend, KReadCount, 0x049, 0x490, 0, 1);
1218 DoRWTest(SReaderInfo::ESuspend, KReadCount, 0x319, 0x319, 0, 1);
1219 DoRWTest(SReaderInfo::ESuspend, KReadCount, 0x349, 0x349, 0, 1);
1221 DoRWTest(SReaderInfo::EKill, KReadCount, 0x090, 0x900, 0, 1);
1222 DoRWTest(SReaderInfo::EKill, KReadCount, 0x019, 0x190, 0, 1);
1223 DoRWTest(SReaderInfo::EKill, KReadCount, 0x049, 0x490, 0, 1);
1224 DoRWTest(SReaderInfo::EKill, KReadCount, 0x319, 0x319, 0, 1);
1225 DoRWTest(SReaderInfo::EKill, KReadCount, 0x349, 0x349, 0, 1);
1227 DoRWTest(SReaderInfo::EMigrate, KReadCount, 0x090, 0x900, 0, 1);
1228 DoRWTest(SReaderInfo::EMigrate, KReadCount, 0x029, 0x290, 0, 1);
1229 DoRWTest(SReaderInfo::EMigrate, KReadCount, 0x019, 0x190, 0, 1);
1230 DoRWTest(SReaderInfo::EMigrate, KReadCount, 0x049, 0x490, 0, 1);
1231 DoRWTest(SReaderInfo::EMigrate, KReadCount, 0x039, 0x390, 0, 1);
1233 DoRWTest(SReaderInfo::EInterlockedSuspend, KReadCount, 0x049, 0x490, 0, 1);
1234 DoRWTest(SReaderInfo::EInterlockedSuspend, KReadCount, 0x019, 0x190, 0, 1);
1235 DoRWTest(SReaderInfo::EInterlockedSuspend, KReadCount, 0x349, 0x349, 0, 1);
1236 DoRWTest(SReaderInfo::EInterlockedSuspend, KReadCount, 0x319, 0x319, 0, 1);
1238 DoRWTest(SReaderInfo::EInterlockedKill, KReadCount, 0x049, 0x490, 0, 1);
1239 DoRWTest(SReaderInfo::EInterlockedKill, KReadCount, 0x019, 0x190, 0, 1);
1240 DoRWTest(SReaderInfo::EInterlockedKill, KReadCount, 0x349, 0x349, 0, 1);
1241 DoRWTest(SReaderInfo::EInterlockedKill, KReadCount, 0x319, 0x319, 0, 1);
1243 DoRWTest(SReaderInfo::EInterlockedMigrate, KReadCount, 0x049, 0x490, 0, 1);
1244 DoRWTest(SReaderInfo::EInterlockedMigrate, KReadCount, 0x019, 0x190, 0, 1);
1245 DoRWTest(SReaderInfo::EInterlockedMigrate, KReadCount, 0x319, 0x319, 0, 1);
1246 DoRWTest(SReaderInfo::EInterlockedMigrate, KReadCount, 0x349, 0x349, 0, 1);
1247 DoRWTest(SReaderInfo::EInterlockedMigrate, KReadCount, 0x129, 0x219, 0, 1);
1248 DoRWTest(SReaderInfo::EInterlockedMigrate, KReadCount, 0x429, 0x249, 0, 1);
1250 // Test freezing or acquiring a mutex while in a group that also does one of those things
1251 // and then leave the group.
1252 DoRWTest(SReaderInfo::ETimeslice, KReadCount, 0x017, 0x170, 0, 1);
1253 DoRWTest(SReaderInfo::ETimeslice, KReadCount, 0x018, 0x180, 0, 1);
1254 DoRWTest(SReaderInfo::ETimeslice, KReadCount, 0x027, 0x270, 0, 1);
1255 DoRWTest(SReaderInfo::ETimeslice, KReadCount, 0x028, 0x280, 0, 1);
1257 DoRWTest(SReaderInfo::ESuspend, KReadCount, 0x017, 0x170, 0, 1);
1258 DoRWTest(SReaderInfo::ESuspend, KReadCount, 0x018, 0x180, 0, 1);
1259 DoRWTest(SReaderInfo::ESuspend, KReadCount, 0x027, 0x270, 0, 1);
1260 DoRWTest(SReaderInfo::ESuspend, KReadCount, 0x028, 0x280, 0, 1);
1262 DoRWTest(SReaderInfo::EKill, KReadCount, 0x017, 0x170, 0, 1);
1263 DoRWTest(SReaderInfo::EKill, KReadCount, 0x018, 0x180, 0, 1);
1264 DoRWTest(SReaderInfo::EKill, KReadCount, 0x027, 0x270, 0, 1);
1265 DoRWTest(SReaderInfo::EKill, KReadCount, 0x028, 0x280, 0, 1);
1267 DoRWTest(SReaderInfo::EMigrate, KReadCount, 0x017, 0x170, 0, 1);
1268 DoRWTest(SReaderInfo::EMigrate, KReadCount, 0x018, 0x180, 0, 1);
1269 DoRWTest(SReaderInfo::EMigrate, KReadCount, 0x027, 0x270, 0, 1);
1270 DoRWTest(SReaderInfo::EMigrate, KReadCount, 0x028, 0x280, 0, 1);
1272 DoRWTest(SReaderInfo::EInterlockedSuspend, KReadCount, 0x017, 0x170, 0, 1);
1273 DoRWTest(SReaderInfo::EInterlockedSuspend, KReadCount, 0x018, 0x180, 0, 1);
1275 DoRWTest(SReaderInfo::EInterlockedKill, KReadCount, 0x017, 0x170, 0, 1);
1276 DoRWTest(SReaderInfo::EInterlockedKill, KReadCount, 0x018, 0x180, 0, 1);
1278 DoRWTest(SReaderInfo::EInterlockedMigrate, KReadCount, 0x017, 0x170, 0, 1);
1279 DoRWTest(SReaderInfo::EInterlockedMigrate, KReadCount, 0x018, 0x180, 0, 1);