os/kernelhwsrv/kerneltest/e32test/prime/t_mutex.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_mutex.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,139 @@
     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_mutex.cpp
    1.18 +// Test mutexes
    1.19 +// 
    1.20 +//
    1.21 +
    1.22 +#include <e32test.h>
    1.23 +#include "u32std.h"
    1.24 +#include "../misc/prbs.h"
    1.25 +
    1.26 +RTest test(_L("T_MUTEX"));
    1.27 +
    1.28 +TInt Count1=0;
    1.29 +TInt Count2=0;
    1.30 +TInt TId=-1;
    1.31 +RMutex Mutex;
    1.32 +
    1.33 +void BusyWait(TInt aMicroseconds)
    1.34 +	{
    1.35 +	TTime begin;
    1.36 +	begin.HomeTime();
    1.37 +	FOREVER
    1.38 +		{
    1.39 +		TTime now;
    1.40 +		now.HomeTime();
    1.41 +		TTimeIntervalMicroSeconds iv=now.MicroSecondsFrom(begin);
    1.42 +		if (iv.Int64()>=TInt64(aMicroseconds))
    1.43 +			return;
    1.44 +		}
    1.45 +	}
    1.46 +
    1.47 +TInt ThreadFunction(TAny* aPtr)
    1.48 +	{
    1.49 +	TInt id=(TInt)aPtr;
    1.50 +	FOREVER
    1.51 +		{
    1.52 +		Mutex.Wait();
    1.53 +		TId=id;
    1.54 +		if (Count1!=Count2)
    1.55 +			{
    1.56 +			RProcess me;
    1.57 +			me.Panic(_L("FAIL!"),0);
    1.58 +			}
    1.59 +		++Count1;
    1.60 +		BusyWait(50000);
    1.61 +		++Count2;
    1.62 +		TId=-1;
    1.63 +		Mutex.Signal();
    1.64 +		}
    1.65 +	}
    1.66 +
    1.67 +void CreateThread(RThread& t, TInt aId)
    1.68 +	{
    1.69 +	TInt r=t.Create(KNullDesC,ThreadFunction,0x2000,NULL,(TAny*)aId);
    1.70 +	test(r==KErrNone);
    1.71 +	t.Resume();
    1.72 +	}
    1.73 +
    1.74 +
    1.75 +GLDEF_C TInt E32Main()
    1.76 +	{
    1.77 +	test.Title();
    1.78 +	test.Start(_L("Create mutex"));
    1.79 +	TInt r=Mutex.CreateLocal();
    1.80 +	test(r==KErrNone);
    1.81 +
    1.82 +	test.Next(_L("Create threads"));
    1.83 +	RThread().SetPriority(EPriorityMuchMore);
    1.84 +	RThread t[4];
    1.85 +	TInt i;
    1.86 +	for (i=0; i<4; ++i)
    1.87 +		CreateThread(t[i],i);
    1.88 +
    1.89 +	TUint seed[2];
    1.90 +	seed[0]=0xb17217f8;
    1.91 +	seed[1]=0;
    1.92 +
    1.93 +	FOREVER
    1.94 +		{
    1.95 +		TUint ms=Random(seed);
    1.96 +		TUint action=(ms>>30);
    1.97 +		i=(ms>>28)&3;
    1.98 +		ms&=63;
    1.99 +		ms+=15;
   1.100 +		User::AfterHighRes(ms*1000);
   1.101 +		switch(action)
   1.102 +			{
   1.103 +			case 0:
   1.104 +				{
   1.105 +				t[i].Suspend();
   1.106 +				break;
   1.107 +				}
   1.108 +			case 1:
   1.109 +				{
   1.110 +				t[i].Resume();
   1.111 +				break;
   1.112 +				}
   1.113 +			case 2:
   1.114 +				{
   1.115 +				TRequestStatus s;
   1.116 +				t[i].Logon(s);
   1.117 +				t[i].Kill(0);
   1.118 +				if (Count1!=Count2)
   1.119 +					{
   1.120 +					if (Count1-Count2!=1)
   1.121 +						{
   1.122 +						RProcess me;
   1.123 +						me.Panic(_L("FAIL!"),1);
   1.124 +						}
   1.125 +					else if (TId==i)
   1.126 +						++Count2;
   1.127 +					}
   1.128 +				User::AfterHighRes(50000);
   1.129 +				User::WaitForRequest(s);
   1.130 +				t[i].Close();
   1.131 +				CreateThread(t[i],i);
   1.132 +				break;
   1.133 +				}
   1.134 +			case 3:
   1.135 +				{
   1.136 +				break;
   1.137 +				}
   1.138 +			}
   1.139 +		}
   1.140 +
   1.141 +//	test.End();
   1.142 +	}