os/kernelhwsrv/kerneltest/e32test/nkernsa/fastsem.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/nkernsa/fastsem.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,198 @@
     1.4 +// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// e32test\nkernsa\fastsem.cpp
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +#define __INCLUDE_NTHREADBASE_DEFINES__
    1.22 +
    1.23 +#include <nktest/nkutils.h>
    1.24 +
    1.25 +void CheckSemaphoreCount(NFastSemaphore* aS, TInt aExpected)
    1.26 +	{
    1.27 +	TInt w = 0;
    1.28 +	TInt r;
    1.29 +	do	{
    1.30 +		r = WaitWithTimeout(aS, KMinTimeout);
    1.31 +		TEST_RESULT1(r==KErrNone || r==KErrTimedOut, "Invalid return code %d", r);
    1.32 +		if (r == KErrNone)
    1.33 +			++w;
    1.34 +		} while(r == KErrNone);
    1.35 +	TEST_RESULT2(w==aExpected, "Signalled %d, Waited %d", aExpected, w);
    1.36 +	}
    1.37 +
    1.38 +void FSTest1(TAny* a)
    1.39 +	{
    1.40 +	TInt n = (TInt)a;
    1.41 +	NFastSemaphore s(0);
    1.42 +
    1.43 +	TInt i;
    1.44 +	for (i=0; i<n; ++i) NKern::FSSignal(&s);
    1.45 +	CheckSemaphoreCount(&s, n);
    1.46 +	NKern::FSSignalN(&s, n);
    1.47 +	CheckSemaphoreCount(&s, n);
    1.48 +	}
    1.49 +
    1.50 +#define DO_FS_TEST1(n,a)	CreateThreadAndWaitForExit("FSTest1", &FSTest1, 12, (TAny*)(n), 0, -1, (a))
    1.51 +
    1.52 +struct SFSTest2Info
    1.53 +	{
    1.54 +	NFastSemaphore iSem;
    1.55 +	volatile TInt iBlockCount;
    1.56 +	volatile TInt iSignals;
    1.57 +	volatile TInt iWaits;
    1.58 +	volatile TBool iStart;
    1.59 +	volatile TBool iStop;
    1.60 +	};
    1.61 +
    1.62 +void FSTest2Signaller0(TAny* a)
    1.63 +	{
    1.64 +	SFSTest2Info& info = *(SFSTest2Info*)a;
    1.65 +	while (!info.iStart)
    1.66 +		{
    1.67 +		}
    1.68 +//	NThreadBase* t = info.iSem.iOwningThread;
    1.69 +	while (!info.iStop)
    1.70 +		{
    1.71 +		++info.iBlockCount;
    1.72 +		__e32_atomic_add_ord32(&info.iSignals, 1);
    1.73 +		NKern::FSSignal(&info.iSem);
    1.74 +		}
    1.75 +	TEST_PRINT1("Ran %d times", info.iBlockCount);
    1.76 +	}
    1.77 +
    1.78 +#ifdef __SMP__
    1.79 +class NKTest
    1.80 +	{
    1.81 +public:
    1.82 +	static TBool ThreadIsBlocked(NThreadBase* aT)
    1.83 +		{ return aT->iWaitState.ThreadIsBlocked(); }
    1.84 +	};
    1.85 +#endif
    1.86 +
    1.87 +void FSTest2Signaller(TAny* a)
    1.88 +	{
    1.89 +	SFSTest2Info& info = *(SFSTest2Info*)a;
    1.90 +	while (!info.iStart)
    1.91 +		{
    1.92 +		}
    1.93 +	NThreadBase* t = info.iSem.iOwningThread;
    1.94 +	TInt count0=0;
    1.95 +	TInt countneg=0;
    1.96 +	TInt blocked=0;
    1.97 +	TInt prev_block_count = info.iBlockCount;
    1.98 +	TInt tries = 1;
    1.99 +	TUint32 seed[2];
   1.100 +	seed[0] = NKern::CurrentCpu()+1;
   1.101 +	seed[1] = 0;
   1.102 +	while (!info.iStop)
   1.103 +		{
   1.104 +		TInt c = info.iSem.iCount;
   1.105 +		if (c>=1)
   1.106 +			continue;
   1.107 +		if (--tries==0)
   1.108 +			{
   1.109 +			TInt bc;
   1.110 +			do	{
   1.111 +				bc = info.iBlockCount;
   1.112 +				} while (bc<=prev_block_count);
   1.113 +			prev_block_count = bc;
   1.114 +			tries = random(seed) & 127;
   1.115 +			tries += 71;
   1.116 +			}
   1.117 +		TUint32 x = random(seed) & 63;
   1.118 +		while (x)
   1.119 +			--x;
   1.120 +		c = info.iSem.iCount;
   1.121 +		NKern::FSSignal(&info.iSem);
   1.122 +		__e32_atomic_add_ord32(&info.iSignals, 1);
   1.123 +		if (c==0) ++count0;
   1.124 +		if (c<0) ++countneg;
   1.125 +#ifdef __SMP__
   1.126 +		if (NKTest::ThreadIsBlocked(t)) ++blocked;
   1.127 +#else
   1.128 +		if (t->iNState == NThread::EWaitFastSemaphore) ++blocked;
   1.129 +#endif
   1.130 +		}
   1.131 +	TEST_PRINT1("Count =0 %d times", count0);
   1.132 +	TEST_PRINT1("Count <0 %d times", countneg);
   1.133 +	TEST_PRINT1("Blocked  %d times", blocked);
   1.134 +	}
   1.135 +
   1.136 +void FSTest2(TAny* a)
   1.137 +	{
   1.138 +	SFSTest2Info& info = *(SFSTest2Info*)a;
   1.139 +	NFastSemaphore exitSem(0);
   1.140 +	NKern::FSSetOwner(&info.iSem, 0);
   1.141 +	info.iBlockCount = 0;
   1.142 +	info.iWaits = 0;
   1.143 +	info.iSignals = 0;
   1.144 +	info.iStart = FALSE;
   1.145 +	info.iStop = FALSE;
   1.146 +	TInt cpu;
   1.147 +	TInt threads = 0;
   1.148 +	TInt this_cpu = NKern::CurrentCpu();
   1.149 +	for_each_cpu(cpu)
   1.150 +		{
   1.151 +		if (cpu==this_cpu)
   1.152 +			CreateThreadSignalOnExit("FSTest2Sig0", &FSTest2Signaller0, 11, a, 0, KSmallTimeslice, &exitSem, cpu);
   1.153 +		else
   1.154 +			CreateThreadSignalOnExit("FSTest2Sig", &FSTest2Signaller, 12, a, 0, KSmallTimeslice, &exitSem, cpu);
   1.155 +		++threads;
   1.156 +		}
   1.157 +
   1.158 +	info.iStart = TRUE;
   1.159 +	while(info.iWaits < 1048576)
   1.160 +		{
   1.161 +		NKern::FSWait(&info.iSem);
   1.162 +		++info.iWaits;
   1.163 +		}
   1.164 +
   1.165 +	info.iStop = TRUE;
   1.166 +	while (threads--)
   1.167 +		NKern::FSWait(&exitSem);
   1.168 +	TEST_PRINT1("Leftover signals %d", info.iSignals-info.iWaits);
   1.169 +	TInt r;
   1.170 +	do	{
   1.171 +		r = WaitWithTimeout(&info.iSem, KMinTimeout);
   1.172 +		TEST_RESULT1(r==KErrNone || r==KErrTimedOut, "Invalid return code %d", r);
   1.173 +		if (r == KErrNone)
   1.174 +			++info.iWaits;
   1.175 +		} while(r == KErrNone);
   1.176 +	TEST_PRINT2("Signalled %d, Waited %d", info.iSignals, info.iWaits);
   1.177 +	TEST_RESULT(info.iWaits==info.iSignals, "MISMATCH!");
   1.178 +	}
   1.179 +
   1.180 +void DoFsTest2()
   1.181 +	{
   1.182 +	SFSTest2Info info;
   1.183 +	CreateThreadAndWaitForExit("FSTest2", &FSTest2, 12, (TAny*)&info, 0, KSmallTimeslice, 0);
   1.184 +	}
   1.185 +
   1.186 +
   1.187 +void TestFastSemaphore()
   1.188 +	{
   1.189 +	TEST_PRINT("Testing Fast Semaphores...");
   1.190 +
   1.191 +	TInt cpu;
   1.192 +	for_each_cpu(cpu)
   1.193 +		{
   1.194 +		DO_FS_TEST1(0,cpu);
   1.195 +		DO_FS_TEST1(1,cpu);
   1.196 +		DO_FS_TEST1(2,cpu);
   1.197 +		DO_FS_TEST1(13,cpu);
   1.198 +		}
   1.199 +
   1.200 +	DoFsTest2();
   1.201 +	}