First public contribution.
1 // Copyright (c) 2007-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\fastsem.cpp
18 #define __INCLUDE_NTHREADBASE_DEFINES__
20 #include <nktest/nkutils.h>
22 void CheckSemaphoreCount(NFastSemaphore* aS, TInt aExpected)
27 r = WaitWithTimeout(aS, KMinTimeout);
28 TEST_RESULT1(r==KErrNone || r==KErrTimedOut, "Invalid return code %d", r);
31 } while(r == KErrNone);
32 TEST_RESULT2(w==aExpected, "Signalled %d, Waited %d", aExpected, w);
41 for (i=0; i<n; ++i) NKern::FSSignal(&s);
42 CheckSemaphoreCount(&s, n);
43 NKern::FSSignalN(&s, n);
44 CheckSemaphoreCount(&s, n);
47 #define DO_FS_TEST1(n,a) CreateThreadAndWaitForExit("FSTest1", &FSTest1, 12, (TAny*)(n), 0, -1, (a))
52 volatile TInt iBlockCount;
53 volatile TInt iSignals;
55 volatile TBool iStart;
59 void FSTest2Signaller0(TAny* a)
61 SFSTest2Info& info = *(SFSTest2Info*)a;
65 // NThreadBase* t = info.iSem.iOwningThread;
69 __e32_atomic_add_ord32(&info.iSignals, 1);
70 NKern::FSSignal(&info.iSem);
72 TEST_PRINT1("Ran %d times", info.iBlockCount);
79 static TBool ThreadIsBlocked(NThreadBase* aT)
80 { return aT->iWaitState.ThreadIsBlocked(); }
84 void FSTest2Signaller(TAny* a)
86 SFSTest2Info& info = *(SFSTest2Info*)a;
90 NThreadBase* t = info.iSem.iOwningThread;
94 TInt prev_block_count = info.iBlockCount;
97 seed[0] = NKern::CurrentCpu()+1;
101 TInt c = info.iSem.iCount;
108 bc = info.iBlockCount;
109 } while (bc<=prev_block_count);
110 prev_block_count = bc;
111 tries = random(seed) & 127;
114 TUint32 x = random(seed) & 63;
117 c = info.iSem.iCount;
118 NKern::FSSignal(&info.iSem);
119 __e32_atomic_add_ord32(&info.iSignals, 1);
123 if (NKTest::ThreadIsBlocked(t)) ++blocked;
125 if (t->iNState == NThread::EWaitFastSemaphore) ++blocked;
128 TEST_PRINT1("Count =0 %d times", count0);
129 TEST_PRINT1("Count <0 %d times", countneg);
130 TEST_PRINT1("Blocked %d times", blocked);
133 void FSTest2(TAny* a)
135 SFSTest2Info& info = *(SFSTest2Info*)a;
136 NFastSemaphore exitSem(0);
137 NKern::FSSetOwner(&info.iSem, 0);
138 info.iBlockCount = 0;
145 TInt this_cpu = NKern::CurrentCpu();
149 CreateThreadSignalOnExit("FSTest2Sig0", &FSTest2Signaller0, 11, a, 0, KSmallTimeslice, &exitSem, cpu);
151 CreateThreadSignalOnExit("FSTest2Sig", &FSTest2Signaller, 12, a, 0, KSmallTimeslice, &exitSem, cpu);
156 while(info.iWaits < 1048576)
158 NKern::FSWait(&info.iSem);
164 NKern::FSWait(&exitSem);
165 TEST_PRINT1("Leftover signals %d", info.iSignals-info.iWaits);
168 r = WaitWithTimeout(&info.iSem, KMinTimeout);
169 TEST_RESULT1(r==KErrNone || r==KErrTimedOut, "Invalid return code %d", r);
172 } while(r == KErrNone);
173 TEST_PRINT2("Signalled %d, Waited %d", info.iSignals, info.iWaits);
174 TEST_RESULT(info.iWaits==info.iSignals, "MISMATCH!");
180 CreateThreadAndWaitForExit("FSTest2", &FSTest2, 12, (TAny*)&info, 0, KSmallTimeslice, 0);
184 void TestFastSemaphore()
186 TEST_PRINT("Testing Fast Semaphores...");