sl@0: // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // e32test\prime\t_mutex.cpp sl@0: // Test mutexes sl@0: // sl@0: // sl@0: sl@0: #include sl@0: #include "u32std.h" sl@0: #include "../misc/prbs.h" sl@0: sl@0: RTest test(_L("T_MUTEX")); sl@0: sl@0: TInt Count1=0; sl@0: TInt Count2=0; sl@0: TInt TId=-1; sl@0: RMutex Mutex; sl@0: sl@0: void BusyWait(TInt aMicroseconds) sl@0: { sl@0: TTime begin; sl@0: begin.HomeTime(); sl@0: FOREVER sl@0: { sl@0: TTime now; sl@0: now.HomeTime(); sl@0: TTimeIntervalMicroSeconds iv=now.MicroSecondsFrom(begin); sl@0: if (iv.Int64()>=TInt64(aMicroseconds)) sl@0: return; sl@0: } sl@0: } sl@0: sl@0: TInt ThreadFunction(TAny* aPtr) sl@0: { sl@0: TInt id=(TInt)aPtr; sl@0: FOREVER sl@0: { sl@0: Mutex.Wait(); sl@0: TId=id; sl@0: if (Count1!=Count2) sl@0: { sl@0: RProcess me; sl@0: me.Panic(_L("FAIL!"),0); sl@0: } sl@0: ++Count1; sl@0: BusyWait(50000); sl@0: ++Count2; sl@0: TId=-1; sl@0: Mutex.Signal(); sl@0: } sl@0: } sl@0: sl@0: void CreateThread(RThread& t, TInt aId) sl@0: { sl@0: TInt r=t.Create(KNullDesC,ThreadFunction,0x2000,NULL,(TAny*)aId); sl@0: test(r==KErrNone); sl@0: t.Resume(); sl@0: } sl@0: sl@0: sl@0: GLDEF_C TInt E32Main() sl@0: { sl@0: test.Title(); sl@0: test.Start(_L("Create mutex")); sl@0: TInt r=Mutex.CreateLocal(); sl@0: test(r==KErrNone); sl@0: sl@0: test.Next(_L("Create threads")); sl@0: RThread().SetPriority(EPriorityMuchMore); sl@0: RThread t[4]; sl@0: TInt i; sl@0: for (i=0; i<4; ++i) sl@0: CreateThread(t[i],i); sl@0: sl@0: TUint seed[2]; sl@0: seed[0]=0xb17217f8; sl@0: seed[1]=0; sl@0: sl@0: FOREVER sl@0: { sl@0: TUint ms=Random(seed); sl@0: TUint action=(ms>>30); sl@0: i=(ms>>28)&3; sl@0: ms&=63; sl@0: ms+=15; sl@0: User::AfterHighRes(ms*1000); sl@0: switch(action) sl@0: { sl@0: case 0: sl@0: { sl@0: t[i].Suspend(); sl@0: break; sl@0: } sl@0: case 1: sl@0: { sl@0: t[i].Resume(); sl@0: break; sl@0: } sl@0: case 2: sl@0: { sl@0: TRequestStatus s; sl@0: t[i].Logon(s); sl@0: t[i].Kill(0); sl@0: if (Count1!=Count2) sl@0: { sl@0: if (Count1-Count2!=1) sl@0: { sl@0: RProcess me; sl@0: me.Panic(_L("FAIL!"),1); sl@0: } sl@0: else if (TId==i) sl@0: ++Count2; sl@0: } sl@0: User::AfterHighRes(50000); sl@0: User::WaitForRequest(s); sl@0: t[i].Close(); sl@0: CreateThread(t[i],i); sl@0: break; sl@0: } sl@0: case 3: sl@0: { sl@0: break; sl@0: } sl@0: } sl@0: } sl@0: sl@0: // test.End(); sl@0: }