os/kernelhwsrv/kerneltest/e32test/prime/t_semutx.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/prime/t_semutx.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,609 @@
     1.4 +// Copyright (c) 1995-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\prime\t_semutx.cpp
    1.18 +// Tests the RSemaphore, RMutex and RCriticalSection classes
    1.19 +// Overview:
    1.20 +// Tests the RSemaphore, RMutex and RCriticalSection classes
    1.21 +// API Information:
    1.22 +// RSemaphore, RMutex, RCriticalSection
    1.23 +// Details:
    1.24 +// - Test RSemaphore and RMutex with the producer/consumer scenario.
    1.25 +// Create two threads, use signal and wait to coordinate the
    1.26 +// threads. Verify results are as expected.
    1.27 +// - Calculate the time required to create, resume and close a thread.
    1.28 +// - Test RSemaphore::Wait(timeout) in a variety ways and timeout 
    1.29 +// values. Verify results are as expected.
    1.30 +// - Test RMutex via two threads which write to an array. The writing
    1.31 +// and updating of the index is wrapped within a mutex pair. Verify 
    1.32 +// results are as expected.
    1.33 +// - Test RCriticalSection via two threads which write to an array. The 
    1.34 +// writing and updating of the index is wrapped within a critical section
    1.35 +// pair. Verify results are as expected.
    1.36 +// Platforms/Drives/Compatibility:
    1.37 +// All.
    1.38 +// Assumptions/Requirement/Pre-requisites:
    1.39 +// Failures and causes:
    1.40 +// Base Port information:
    1.41 +// 
    1.42 +//
    1.43 +
    1.44 +#include <e32test.h>
    1.45 +
    1.46 +const TInt KMaxBufferSize=10;
    1.47 +const TInt KMaxArraySize=10;
    1.48 +const TInt KNumProducerItems=100;
    1.49 +
    1.50 +enum {EThread1ID=1,EThread2ID};
    1.51 +
    1.52 +RTest test(_L("T_SEMUTX"));
    1.53 +RMutex mutex;
    1.54 +RCriticalSection criticalSn;	
    1.55 +TInt thread1Count,thread2Count;
    1.56 +TInt arrayIndex;
    1.57 +TInt array[KMaxArraySize];  
    1.58 +TInt consumerArray[KNumProducerItems];
    1.59 +RSemaphore slotAvailable,itemAvailable;  
    1.60 +			 
    1.61 +class CStack
    1.62 +	{
    1.63 +public:	   
    1.64 +	CStack() {iCount=0;};
    1.65 +	void Push(TInt aItem) {iStack[iCount++]=aItem;};
    1.66 +	TInt Pop(void) {return(iStack[--iCount]);};
    1.67 +private:
    1.68 +	TInt iStack[KMaxBufferSize];
    1.69 +	TInt iCount;
    1.70 +	};
    1.71 +CStack stack;
    1.72 +
    1.73 +
    1.74 +TInt Producer(TAny*)
    1.75 +	{
    1.76 +	for(TInt ii=0;ii<KNumProducerItems;ii++)
    1.77 +		{
    1.78 +		slotAvailable.Wait();
    1.79 +		mutex.Wait();
    1.80 +		stack.Push(ii);
    1.81 +		mutex.Signal();
    1.82 +		itemAvailable.Signal();
    1.83 +		}
    1.84 +	return(KErrNone);
    1.85 +	}
    1.86 +
    1.87 +TInt Consumer(TAny*)
    1.88 +	{
    1.89 +	TInt item;
    1.90 +	for(TInt ii=0;ii<KNumProducerItems;ii++)
    1.91 +		{
    1.92 +		itemAvailable.Wait();
    1.93 +		mutex.Wait();
    1.94 +		item=stack.Pop();
    1.95 +		mutex.Signal();
    1.96 +		slotAvailable.Signal();
    1.97 +		consumerArray[item]=item;
    1.98 +		}
    1.99 +	return(KErrNone);
   1.100 +	}
   1.101 +
   1.102 +void BusyWait(TInt aMicroseconds)
   1.103 +	{
   1.104 +	TTime begin;
   1.105 +	begin.HomeTime();
   1.106 +	FOREVER
   1.107 +		{
   1.108 +		TTime now;
   1.109 +		now.HomeTime();
   1.110 +		TTimeIntervalMicroSeconds iv=now.MicroSecondsFrom(begin);
   1.111 +		if (iv.Int64()>=TInt64(aMicroseconds))
   1.112 +			return;
   1.113 +		}
   1.114 +	}
   1.115 +
   1.116 +TInt MutexThreadEntryPoint1(TAny*)
   1.117 +//
   1.118 +// Mutex test thread 1
   1.119 +//
   1.120 +	{	
   1.121 +
   1.122 +	thread1Count=0;
   1.123 +	TBool running=ETrue;
   1.124 +	do
   1.125 +		{
   1.126 +		mutex.Wait();
   1.127 +		BusyWait(100000);
   1.128 +		if (arrayIndex<KMaxArraySize)
   1.129 +			{
   1.130 +			array[arrayIndex++]=EThread1ID;
   1.131 +			thread1Count++;
   1.132 +			}
   1.133 +		else
   1.134 +			running=EFalse;
   1.135 +		mutex.Signal();
   1.136 +		} while (running);
   1.137 +	return(KErrNone);
   1.138 +	}
   1.139 +
   1.140 +TInt MutexThreadEntryPoint2(TAny*)
   1.141 +//
   1.142 +// Mutex test thread 2
   1.143 +//
   1.144 +	{
   1.145 +
   1.146 +	thread2Count=0;
   1.147 +	TBool running=ETrue;
   1.148 +	do
   1.149 +		{
   1.150 +		mutex.Wait();
   1.151 +		BusyWait(200000);
   1.152 +		if (arrayIndex<KMaxArraySize)
   1.153 +			{
   1.154 +			array[arrayIndex++]=EThread2ID;
   1.155 +			thread2Count++;
   1.156 +			}
   1.157 +		else
   1.158 +			running=EFalse;
   1.159 +		mutex.Signal();
   1.160 +		} while (running);
   1.161 +	return(KErrNone);
   1.162 +	}
   1.163 +
   1.164 +TInt CriticalSnThreadEntryPoint1(TAny*)
   1.165 +//
   1.166 +// Critical Section test thread 1
   1.167 +//
   1.168 +	{	
   1.169 +
   1.170 +	thread1Count=0;
   1.171 +	TBool running=ETrue;
   1.172 +	do
   1.173 +		{
   1.174 +		criticalSn.Wait();
   1.175 +		User::After(100000);
   1.176 +		if (arrayIndex<KMaxArraySize)
   1.177 +			{
   1.178 +			array[arrayIndex++]=EThread1ID;
   1.179 +			thread1Count++;
   1.180 +			}
   1.181 +		else
   1.182 +			running=EFalse;
   1.183 +		criticalSn.Signal();
   1.184 +		} while (running);
   1.185 +	return(KErrNone);
   1.186 +	}
   1.187 +
   1.188 +TInt CriticalSnThreadEntryPoint2(TAny*)
   1.189 +//
   1.190 +// Critical Section test thread 2
   1.191 +//
   1.192 +	{
   1.193 +
   1.194 +	thread2Count=0;
   1.195 +	TBool running=ETrue;
   1.196 +	do
   1.197 +		{
   1.198 +		criticalSn.Wait();
   1.199 +		User::After(200000);
   1.200 +		if (arrayIndex<KMaxArraySize)
   1.201 +			{
   1.202 +			array[arrayIndex++]=EThread2ID;
   1.203 +			thread2Count++;
   1.204 +			}
   1.205 +		else
   1.206 +			running=EFalse;
   1.207 +		criticalSn.Signal();
   1.208 +		} while (running);
   1.209 +	return(KErrNone);
   1.210 +	}
   1.211 +
   1.212 +struct SWaitSem
   1.213 +	{
   1.214 +	RSemaphore iSem;
   1.215 +	TInt iTimeout;
   1.216 +	};
   1.217 +
   1.218 +TInt WaitSemThread(TAny* a)
   1.219 +	{
   1.220 +	SWaitSem& ws = *(SWaitSem*)a;
   1.221 +	return ws.iSem.Wait(ws.iTimeout);
   1.222 +	}
   1.223 +
   1.224 +void StartWaitSemThread(RThread& aT, SWaitSem& aW, TThreadPriority aP=EPriorityLess)
   1.225 +	{
   1.226 +	TInt r = aT.Create(KNullDesC, &WaitSemThread, 0x1000, 0x1000, 0x1000, &aW);
   1.227 +	test(r==KErrNone);
   1.228 +	aT.SetPriority(aP);
   1.229 +	aT.Resume();
   1.230 +	}
   1.231 +
   1.232 +void WaitForWaitSemThread(RThread& aT, TInt aResult)
   1.233 +	{
   1.234 +	TRequestStatus s;
   1.235 +	aT.Logon(s);
   1.236 +	User::WaitForRequest(s);
   1.237 +	test(aT.ExitType()==EExitKill);
   1.238 +	test(aT.ExitReason()==aResult);
   1.239 +	test(s.Int()==aResult);
   1.240 +	CLOSE_AND_WAIT(aT);
   1.241 +	}
   1.242 +
   1.243 +TInt DummyThread(TAny*)
   1.244 +	{
   1.245 +	return 0;
   1.246 +	}
   1.247 +
   1.248 +void TestSemaphore2()
   1.249 +	{
   1.250 +	test.Start(_L("Test semaphore wait with timeout"));
   1.251 +	SWaitSem ws;
   1.252 +	RThread t;
   1.253 +	TTime initial;
   1.254 +	TTime final;
   1.255 +	TInt elapsed=0;
   1.256 +	TInt r = ws.iSem.CreateLocal(0);
   1.257 +	test(r==KErrNone);
   1.258 +
   1.259 +	RThread().SetPriority(EPriorityAbsoluteVeryLow);
   1.260 +	TInt threadcount=0;
   1.261 +	initial.HomeTime();
   1.262 +	while (elapsed<1000000)
   1.263 +		{
   1.264 +		r = t.Create(KNullDesC, &DummyThread, 0x1000, NULL, NULL);
   1.265 +		test(r==KErrNone);
   1.266 +		t.SetPriority(EPriorityMore);
   1.267 +		t.Resume();
   1.268 +		t.Close();
   1.269 +		++threadcount;
   1.270 +		final.HomeTime();
   1.271 +		elapsed = I64INT(final.Int64()-initial.Int64());
   1.272 +		}
   1.273 +	RThread().SetPriority(EPriorityNormal);
   1.274 +	test.Printf(_L("%d threads in 1 sec\n"),threadcount);
   1.275 +	TInt overhead = 1000000/threadcount;
   1.276 +	test.Printf(_L("overhead = %dus\n"),overhead);
   1.277 +
   1.278 +	ws.iTimeout=1000000;
   1.279 +	initial.HomeTime();
   1.280 +	StartWaitSemThread(t, ws);
   1.281 +	WaitForWaitSemThread(t, KErrTimedOut);
   1.282 +	final.HomeTime();
   1.283 +	elapsed = I64INT(final.Int64()-initial.Int64());
   1.284 +	test.Printf(_L("Time taken = %dus\n"), elapsed);
   1.285 +	test(elapsed>=900000+overhead && elapsed<1500000+overhead);
   1.286 +
   1.287 +	ws.iTimeout=-1;
   1.288 +	initial.HomeTime();
   1.289 +	StartWaitSemThread(t, ws);
   1.290 +	WaitForWaitSemThread(t, KErrArgument);
   1.291 +	final.HomeTime();
   1.292 +	elapsed = I64INT(final.Int64()-initial.Int64());
   1.293 +	test.Printf(_L("Time taken = %dus\n"), elapsed);
   1.294 +
   1.295 +	ws.iTimeout=2000000;
   1.296 +	initial.HomeTime();
   1.297 +	StartWaitSemThread(t, ws);
   1.298 +	User::After(1000000);
   1.299 +	ws.iSem.Signal();
   1.300 +	WaitForWaitSemThread(t, KErrNone);
   1.301 +	final.HomeTime();
   1.302 +	elapsed = I64INT(final.Int64()-initial.Int64());
   1.303 +	test.Printf(_L("Time taken = %dus\n"), elapsed);
   1.304 +	test(elapsed>=900000+overhead && elapsed<1500000+overhead);
   1.305 +
   1.306 +	ws.iTimeout=100000;
   1.307 +	StartWaitSemThread(t, ws, EPriorityMore);
   1.308 +	t.Suspend();
   1.309 +	ws.iSem.Signal();
   1.310 +	User::After(200000);
   1.311 +	t.Resume();
   1.312 +	WaitForWaitSemThread(t, KErrTimedOut);
   1.313 +	test(ws.iSem.Wait(1)==KErrNone);
   1.314 +
   1.315 +	ws.iTimeout=100000;
   1.316 +	StartWaitSemThread(t, ws, EPriorityMore);
   1.317 +	t.Suspend();
   1.318 +	ws.iSem.Signal();
   1.319 +	User::After(50000);
   1.320 +	t.Resume();
   1.321 +	WaitForWaitSemThread(t, KErrNone);
   1.322 +	test(ws.iSem.Wait(1)==KErrTimedOut);
   1.323 +
   1.324 +	RThread t2;
   1.325 +	ws.iTimeout=100000;
   1.326 +	StartWaitSemThread(t, ws, EPriorityMuchMore);
   1.327 +	StartWaitSemThread(t2, ws, EPriorityMore);
   1.328 +	t.Suspend();
   1.329 +	ws.iSem.Signal();
   1.330 +	test(t2.ExitType()==EExitKill);
   1.331 +	test(t.ExitType()==EExitPending);
   1.332 +	t.Resume();
   1.333 +	WaitForWaitSemThread(t, KErrTimedOut);
   1.334 +	WaitForWaitSemThread(t2, KErrNone);
   1.335 +	test(ws.iSem.Wait(1)==KErrTimedOut);
   1.336 +
   1.337 +	ws.iTimeout=1000000;
   1.338 +	initial.HomeTime();
   1.339 +	StartWaitSemThread(t2, ws, EPriorityMore);
   1.340 +	StartWaitSemThread(t, ws, EPriorityMuchMore);
   1.341 +	ws.iSem.Signal();
   1.342 +	WaitForWaitSemThread(t, KErrNone);
   1.343 +	final.HomeTime();
   1.344 +	elapsed = I64INT(final.Int64()-initial.Int64());
   1.345 +	test.Printf(_L("Time taken = %dus\n"), elapsed);
   1.346 +	WaitForWaitSemThread(t2, KErrTimedOut);
   1.347 +	final.HomeTime();
   1.348 +	elapsed = I64INT(final.Int64()-initial.Int64());
   1.349 +	test.Printf(_L("Time taken = %dus\n"), elapsed);
   1.350 +	test(elapsed>=900000+2*overhead && elapsed<1500000+2*overhead);
   1.351 +
   1.352 +	ws.iTimeout=1000000;
   1.353 +	initial.HomeTime();
   1.354 +	StartWaitSemThread(t2, ws, EPriorityMore);
   1.355 +	StartWaitSemThread(t, ws, EPriorityMuchMore);
   1.356 +	WaitForWaitSemThread(t, KErrTimedOut);
   1.357 +	final.HomeTime();
   1.358 +	elapsed = I64INT(final.Int64()-initial.Int64());
   1.359 +	test.Printf(_L("Time taken = %dus\n"), elapsed);
   1.360 +	WaitForWaitSemThread(t2, KErrTimedOut);
   1.361 +	final.HomeTime();
   1.362 +	elapsed = I64INT(final.Int64()-initial.Int64());
   1.363 +	test.Printf(_L("Time taken = %dus\n"), elapsed);
   1.364 +	test(elapsed>=900000+2*overhead && elapsed<1500000+2*overhead);
   1.365 +
   1.366 +	ws.iTimeout=1000000;
   1.367 +	initial.HomeTime();
   1.368 +	StartWaitSemThread(t2, ws, EPriorityMore);
   1.369 +	StartWaitSemThread(t, ws, EPriorityMuchMore);
   1.370 +	t.Kill(299792458);
   1.371 +	WaitForWaitSemThread(t2, KErrTimedOut);
   1.372 +	WaitForWaitSemThread(t, 299792458);
   1.373 +	final.HomeTime();
   1.374 +	elapsed = I64INT(final.Int64()-initial.Int64());
   1.375 +	test.Printf(_L("Time taken = %dus\n"), elapsed);
   1.376 +	test(elapsed>=900000+2*overhead && elapsed<1500000+2*overhead);
   1.377 +
   1.378 +	ws.iTimeout=1000000;
   1.379 +	initial.HomeTime();
   1.380 +	StartWaitSemThread(t, ws, EPriorityMore);
   1.381 +	StartWaitSemThread(t2, ws, EPriorityMuchMore);
   1.382 +	test(t.ExitType()==EExitPending);
   1.383 +	test(t2.ExitType()==EExitPending);
   1.384 +	ws.iSem.Close();
   1.385 +	test(t.ExitType()==EExitKill);
   1.386 +	test(t2.ExitType()==EExitKill);
   1.387 +	WaitForWaitSemThread(t2, KErrGeneral);
   1.388 +	WaitForWaitSemThread(t, KErrGeneral);
   1.389 +	final.HomeTime();
   1.390 +	elapsed = I64INT(final.Int64()-initial.Int64());
   1.391 +	test.Printf(_L("Time taken = %dus\n"), elapsed);
   1.392 +	test(elapsed<=50000+3*overhead);
   1.393 +
   1.394 +	test.End();
   1.395 +	}
   1.396 +
   1.397 +void TestSemaphore()
   1.398 +	{
   1.399 +/*********** TO DO ************/
   1.400 +// Check it panics if the count <0
   1.401 +
   1.402 +	test.Start(_L("Create"));
   1.403 +	RSemaphore semaphore;
   1.404 +	RThread thread1, thread2;
   1.405 +
   1.406 +	semaphore.CreateLocal(0); 	// creates a DPlatSemaphore but casts it to a pointer to a DSemaphore
   1.407 +								// sets semaphore count to the value of the parameter, 
   1.408 +								// adds object to the K::Semaphores container, sets iHandle
   1.409 +								// Local sets DSemaphore.iName to NULL & iOwner to Kern::CurrentProcess()
   1.410 +								// Global sets iName to that passed and iOwner to NULL
   1.411 +								// Adds a record into CObjectIx containing a pointer to the semaphore object
   1.412 +/*	test.Next(_L("Find"));
   1.413 +	fullName=semaphore.FullName();	
   1.414 +	find.Find(fullName);	// sets iMatch to fullName	(misleadingly named method as it doesn't find anything)
   1.415 +	test(find.Next(fullName)== KErrNone);	
   1.416 +*/
   1.417 +	test.Next(_L("Producer/Consumer scenario"));
   1.418 +	// Test Rsemaphore with the producer/consumer scenario	RThread thread1, thread2;
   1.419 +	TRequestStatus stat1, stat2;
   1.420 +	test(mutex.CreateLocal()==KErrNone);
   1.421 +	test(slotAvailable.CreateLocal(KMaxBufferSize)==KErrNone);
   1.422 +	test(itemAvailable.CreateLocal(0)==KErrNone);
   1.423 +	test(thread1.Create(_L("Thread1"),Producer,KDefaultStackSize,0x200,0x200,NULL)==KErrNone);
   1.424 +	test(thread2.Create(_L("Thread2"),Consumer,KDefaultStackSize,0x200,0x200,NULL)==KErrNone);
   1.425 +	thread1.Logon(stat1);
   1.426 +	thread2.Logon(stat2);
   1.427 +	test(stat1==KRequestPending);
   1.428 +	test(stat2==KRequestPending);
   1.429 +	thread1.Resume(); 
   1.430 +	thread2.Resume();
   1.431 +	User::WaitForRequest(stat1);
   1.432 +	User::WaitForRequest(stat2);
   1.433 +	test(stat1==KErrNone);
   1.434 +	test(stat2==KErrNone);
   1.435 +	for(TInt jj=0;jj<KNumProducerItems;jj++)
   1.436 +		test(consumerArray[jj]==jj);		
   1.437 +	
   1.438 +	test.Next(_L("Close"));
   1.439 +	mutex.Close();
   1.440 +	CLOSE_AND_WAIT(thread1);
   1.441 +	CLOSE_AND_WAIT(thread2);
   1.442 +	test.End();
   1.443 +	}
   1.444 +
   1.445 +void TestMutex2()
   1.446 +	{
   1.447 +	RMutex m;
   1.448 +	test.Start(_L("Create"));
   1.449 +	test(m.CreateLocal()==KErrNone);
   1.450 +
   1.451 +	// Test RMutex::IsHeld()
   1.452 +	test.Next(_L("IsHeld ?"));
   1.453 +	test(!m.IsHeld());
   1.454 +	test.Next(_L("Wait"));
   1.455 +	m.Wait();
   1.456 +	test.Next(_L("IsHeld ?"));
   1.457 +	test(m.IsHeld());
   1.458 +	test.Next(_L("Signal"));
   1.459 +	m.Signal();
   1.460 +	test.Next(_L("IsHeld ?"));
   1.461 +	test(!m.IsHeld());
   1.462 +
   1.463 +	test.End();
   1.464 +	}
   1.465 +
   1.466 +void TestMutex()
   1.467 +	{
   1.468 +	test.Start(_L("Create"));
   1.469 +	test(mutex.CreateLocal()==KErrNone);
   1.470 +	
   1.471 +	test.Next(_L("Threads writing to arrays test"));
   1.472 +//
   1.473 +// Create two threads which write to two arrays. The arrays and indexs
   1.474 +// are global and each thread writes an identifier to the arrays. For
   1.475 +// one array the writing and updating of the index is wrapped in a mutex
   1.476 +// pair. The other array is a control and is not wrapaped within mutex.
   1.477 +// Each thread records the number of instances it "thinks" it wrote to
   1.478 +// each array. For the mutex controlled array the actual instances
   1.479 +// written to the array should always be the same as the threads think.
   1.480 +//
   1.481 +	arrayIndex=0;
   1.482 +	RThread thread1,thread2;	
   1.483 +	test(thread1.Create(_L("Thread1"),MutexThreadEntryPoint1,KDefaultStackSize,0x2000,0x2000,NULL)==KErrNone);
   1.484 +	test(thread2.Create(_L("Thread2"),MutexThreadEntryPoint2,KDefaultStackSize,0x2000,0x2000,NULL)==KErrNone);			 
   1.485 +	TRequestStatus stat1,stat2;
   1.486 +	thread1.Logon(stat1);
   1.487 +	thread2.Logon(stat2);
   1.488 +	test(stat1==KRequestPending);
   1.489 +	test(stat2==KRequestPending);
   1.490 +	thread1.Resume(); 
   1.491 +	thread2.Resume();
   1.492 +	User::WaitForRequest(stat1);
   1.493 +	User::WaitForRequest(stat2);
   1.494 +	test(stat1==KErrNone);
   1.495 +	test(stat2==KErrNone); 
   1.496 +	TInt thread1ActualCount=0; 
   1.497 +	TInt thread2ActualCount=0;
   1.498 +	TInt ii=0;
   1.499 +	while(ii<KMaxArraySize)
   1.500 +		{
   1.501 +		if (array[ii]==EThread1ID)
   1.502 +			thread1ActualCount++;
   1.503 +		if (array[ii]==EThread2ID)
   1.504 +			thread2ActualCount++;
   1.505 +		ii++;
   1.506 +		}
   1.507 +	test.Printf(_L("T1 %d T1ACT %d T2 %d T2ACT %d"),thread1Count,thread1ActualCount,thread2Count,thread2ActualCount);
   1.508 +	test(thread1ActualCount==thread1Count);
   1.509 +	test(thread2ActualCount==thread2Count);
   1.510 +	test(thread1Count==thread2Count);
   1.511 +	test(thread1Count==(KMaxArraySize>>1));
   1.512 +	
   1.513 +	test.Next(_L("Close"));
   1.514 +	CLOSE_AND_WAIT(thread1);
   1.515 +	CLOSE_AND_WAIT(thread2);
   1.516 +	mutex.Close();
   1.517 +	test.End();
   1.518 +	}
   1.519 +
   1.520 +void TestCriticalSection()
   1.521 +//
   1.522 +//As TestMutex, but for RCriticalSection
   1.523 +//
   1.524 +	{
   1.525 +	
   1.526 +	test.Start(_L("Create"));
   1.527 +	test(criticalSn.CreateLocal()==KErrNone);
   1.528 +
   1.529 +/***************** TO DO ***********************
   1.530 +
   1.531 +	test.Next(_L("Find"));
   1.532 +//
   1.533 +// Test finding the RCriticalSection
   1.534 +//
   1.535 +	TFindCriticalSection find;
   1.536 +	TFullName fullName;
   1.537 +	fullName=criticalSn.FullName();
   1.538 +	find.Find(fullName);
   1.539 +	test(find.Next(fullName)==KErrNone);
   1.540 +	test(fullName==criticalSn.FullName());
   1.541 +
   1.542 +************************************************/
   1.543 +
   1.544 +	test.Next(_L("Threads writing to arrays test"));
   1.545 +//
   1.546 +// Create two threads which write to two arrays. The arrays and indexs
   1.547 +// are global and each thread writes an identifier to the arrays. For
   1.548 +// one array the writing and updating of the index is wrapped in a critical
   1.549 +// section pair. The other array is a control and is not wrapaped within
   1.550 +// a critical section. Each thread records the number of instances it
   1.551 +// "thinks" it wrote to each array. For the mutex controlled array the
   1.552 +// actual instances written to the array should always be the same as the
   1.553 +// threads think.
   1.554 +//
   1.555 +	arrayIndex=0;
   1.556 +	RThread thread1,thread2;	
   1.557 +	test(thread1.Create(_L("Thread1"),CriticalSnThreadEntryPoint1,KDefaultStackSize,0x2000,0x2000,NULL)==KErrNone);
   1.558 +	test(thread2.Create(_L("Thread2"),CriticalSnThreadEntryPoint2,KDefaultStackSize,0x2000,0x2000,NULL)==KErrNone);			 
   1.559 +	TRequestStatus stat1,stat2;
   1.560 +	thread1.Logon(stat1);
   1.561 +	thread2.Logon(stat2);
   1.562 +	test(stat1==KRequestPending);
   1.563 +	test(stat2==KRequestPending);
   1.564 +	thread1.Resume(); 
   1.565 +	thread2.Resume();
   1.566 +	User::WaitForRequest(stat1);
   1.567 +	User::WaitForRequest(stat2);
   1.568 +	test(stat1==KErrNone);
   1.569 +	test(stat2==KErrNone); 
   1.570 +	TInt thread1ActualCount=0; 
   1.571 +	TInt thread2ActualCount=0;
   1.572 +	TInt ii=0;
   1.573 +	while(ii<KMaxArraySize)
   1.574 +		{
   1.575 +		if (array[ii]==EThread1ID)
   1.576 +			thread1ActualCount++;
   1.577 +		if (array[ii]==EThread2ID)
   1.578 +			thread2ActualCount++;
   1.579 +		ii++;
   1.580 +		}
   1.581 +	test(thread1ActualCount==thread1Count);
   1.582 +	test(thread2ActualCount==thread2Count);
   1.583 +	test(thread1Count==thread2Count);
   1.584 +	test(thread1Count==(KMaxArraySize>>1));
   1.585 +
   1.586 +	test.Next(_L("Close"));
   1.587 +	CLOSE_AND_WAIT(thread1);
   1.588 +	CLOSE_AND_WAIT(thread2);
   1.589 +	criticalSn.Close();
   1.590 +	test.End();
   1.591 +	}
   1.592 +
   1.593 +
   1.594 +GLDEF_C TInt E32Main()
   1.595 +	{	
   1.596 +
   1.597 +	test.Title();
   1.598 + 	__UHEAP_MARK;
   1.599 +	test.Start(_L("Test RSemaphore"));
   1.600 +	TestSemaphore();
   1.601 +	TestSemaphore2();
   1.602 +	test.Next(_L("Test RMutex"));
   1.603 +	TestMutex();
   1.604 +	TestMutex2();
   1.605 +	test.Next(_L("Test RCriticalSection"));
   1.606 +	TestCriticalSection();
   1.607 +	test.End();
   1.608 +	__UHEAP_MARKEND;
   1.609 +	return(KErrNone);
   1.610 +	}
   1.611 +
   1.612 +