os/kernelhwsrv/kerneltest/e32test/prime/t_mutex.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32test\prime\t_mutex.cpp
sl@0
    15
// Test mutexes
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
#include <e32test.h>
sl@0
    20
#include "u32std.h"
sl@0
    21
#include "../misc/prbs.h"
sl@0
    22
sl@0
    23
RTest test(_L("T_MUTEX"));
sl@0
    24
sl@0
    25
TInt Count1=0;
sl@0
    26
TInt Count2=0;
sl@0
    27
TInt TId=-1;
sl@0
    28
RMutex Mutex;
sl@0
    29
sl@0
    30
void BusyWait(TInt aMicroseconds)
sl@0
    31
	{
sl@0
    32
	TTime begin;
sl@0
    33
	begin.HomeTime();
sl@0
    34
	FOREVER
sl@0
    35
		{
sl@0
    36
		TTime now;
sl@0
    37
		now.HomeTime();
sl@0
    38
		TTimeIntervalMicroSeconds iv=now.MicroSecondsFrom(begin);
sl@0
    39
		if (iv.Int64()>=TInt64(aMicroseconds))
sl@0
    40
			return;
sl@0
    41
		}
sl@0
    42
	}
sl@0
    43
sl@0
    44
TInt ThreadFunction(TAny* aPtr)
sl@0
    45
	{
sl@0
    46
	TInt id=(TInt)aPtr;
sl@0
    47
	FOREVER
sl@0
    48
		{
sl@0
    49
		Mutex.Wait();
sl@0
    50
		TId=id;
sl@0
    51
		if (Count1!=Count2)
sl@0
    52
			{
sl@0
    53
			RProcess me;
sl@0
    54
			me.Panic(_L("FAIL!"),0);
sl@0
    55
			}
sl@0
    56
		++Count1;
sl@0
    57
		BusyWait(50000);
sl@0
    58
		++Count2;
sl@0
    59
		TId=-1;
sl@0
    60
		Mutex.Signal();
sl@0
    61
		}
sl@0
    62
	}
sl@0
    63
sl@0
    64
void CreateThread(RThread& t, TInt aId)
sl@0
    65
	{
sl@0
    66
	TInt r=t.Create(KNullDesC,ThreadFunction,0x2000,NULL,(TAny*)aId);
sl@0
    67
	test(r==KErrNone);
sl@0
    68
	t.Resume();
sl@0
    69
	}
sl@0
    70
sl@0
    71
sl@0
    72
GLDEF_C TInt E32Main()
sl@0
    73
	{
sl@0
    74
	test.Title();
sl@0
    75
	test.Start(_L("Create mutex"));
sl@0
    76
	TInt r=Mutex.CreateLocal();
sl@0
    77
	test(r==KErrNone);
sl@0
    78
sl@0
    79
	test.Next(_L("Create threads"));
sl@0
    80
	RThread().SetPriority(EPriorityMuchMore);
sl@0
    81
	RThread t[4];
sl@0
    82
	TInt i;
sl@0
    83
	for (i=0; i<4; ++i)
sl@0
    84
		CreateThread(t[i],i);
sl@0
    85
sl@0
    86
	TUint seed[2];
sl@0
    87
	seed[0]=0xb17217f8;
sl@0
    88
	seed[1]=0;
sl@0
    89
sl@0
    90
	FOREVER
sl@0
    91
		{
sl@0
    92
		TUint ms=Random(seed);
sl@0
    93
		TUint action=(ms>>30);
sl@0
    94
		i=(ms>>28)&3;
sl@0
    95
		ms&=63;
sl@0
    96
		ms+=15;
sl@0
    97
		User::AfterHighRes(ms*1000);
sl@0
    98
		switch(action)
sl@0
    99
			{
sl@0
   100
			case 0:
sl@0
   101
				{
sl@0
   102
				t[i].Suspend();
sl@0
   103
				break;
sl@0
   104
				}
sl@0
   105
			case 1:
sl@0
   106
				{
sl@0
   107
				t[i].Resume();
sl@0
   108
				break;
sl@0
   109
				}
sl@0
   110
			case 2:
sl@0
   111
				{
sl@0
   112
				TRequestStatus s;
sl@0
   113
				t[i].Logon(s);
sl@0
   114
				t[i].Kill(0);
sl@0
   115
				if (Count1!=Count2)
sl@0
   116
					{
sl@0
   117
					if (Count1-Count2!=1)
sl@0
   118
						{
sl@0
   119
						RProcess me;
sl@0
   120
						me.Panic(_L("FAIL!"),1);
sl@0
   121
						}
sl@0
   122
					else if (TId==i)
sl@0
   123
						++Count2;
sl@0
   124
					}
sl@0
   125
				User::AfterHighRes(50000);
sl@0
   126
				User::WaitForRequest(s);
sl@0
   127
				t[i].Close();
sl@0
   128
				CreateThread(t[i],i);
sl@0
   129
				break;
sl@0
   130
				}
sl@0
   131
			case 3:
sl@0
   132
				{
sl@0
   133
				break;
sl@0
   134
				}
sl@0
   135
			}
sl@0
   136
		}
sl@0
   137
sl@0
   138
//	test.End();
sl@0
   139
	}