kerneltest/e32test/property/t_race.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "t_property.h"
       
    17 
       
    18 _LIT(KPropSetGetRaceName, "RProperty::Set/Get() Race");
       
    19  
       
    20 CPropSetGetRace::CPropSetGetRace(TUid aCategory, TUint aKey) : 
       
    21 	  CTestProgram(KPropSetGetRaceName), iCategory(aCategory), iKey(aKey), 
       
    22 		  iBuf1(RProperty::KMaxPropertySize), iBuf2(RProperty::KMaxPropertySize)
       
    23 	{
       
    24 	}
       
    25 
       
    26 void CPropSetGetRace::Trublemaker(TDes8& aBuf)
       
    27 	{
       
    28 	RProperty prop;	
       
    29 	TInt r = prop.Attach(iCategory, iKey, EOwnerThread);
       
    30 	TF_ERROR(r, r == KErrNone);
       
    31 	TBool attached = ETrue;
       
    32 	for(;;)
       
    33 		{
       
    34 		TBuf8<RProperty::KMaxPropertySize> buf;
       
    35 		if (attached)
       
    36 			{
       
    37 			r = prop.Set(aBuf);
       
    38 			TF_ERROR(r, r == KErrNone);
       
    39 			r = prop.Get(buf);
       
    40 			TF_ERROR(r, r == KErrNone);
       
    41 			}
       
    42 		else 
       
    43 			{
       
    44 			r = prop.Set(iCategory, iKey, aBuf);
       
    45 			TF_ERROR(r, r == KErrNone);
       
    46 			r = prop.Get(iCategory, iKey, buf);
       
    47 			TF_ERROR(r, r == KErrNone);
       
    48 			}
       
    49 		TF_ERROR(KErrGeneral, !(buf.Compare(iBuf1) && buf.Compare(iBuf2)));
       
    50 		User::After(1);
       
    51 		attached = !attached;
       
    52 		}
       
    53 	}
       
    54 
       
    55 TInt CPropSetGetRace::TrublemakerThreadEntry(TAny* ptr)
       
    56 	{
       
    57 	CPropSetGetRace* prog = (CPropSetGetRace*) ptr;
       
    58 	prog->Trublemaker(prog->iBuf2);
       
    59 	return KErrNone;
       
    60 	}
       
    61 
       
    62 void CPropSetGetRace::Run(TUint aCount)
       
    63 	{
       
    64 	RProperty prop;	
       
    65 
       
    66 	TInt r = prop.Define(iCategory, iKey, RProperty::EByteArray, KPassPolicy, KPassPolicy);
       
    67 	TF_ERROR(r, r == KErrNone);
       
    68 	r = prop.Attach(iCategory,iKey);
       
    69 	TF_ERROR(r, r == KErrNone);
       
    70 
       
    71 	TUint i;
       
    72 	for(i = 0; i < RProperty::KMaxPropertySize; ++i)
       
    73 		{
       
    74 		iBuf1[i] = '1';
       
    75 		iBuf2[i] = '2';
       
    76 		}
       
    77 
       
    78 	TRequestStatus status;
       
    79 	RThread thr;
       
    80 	r = thr.Create(KNullDesC, TrublemakerThreadEntry, 0x2000, NULL, this);
       
    81 	TF_ERROR(r, r == KErrNone);
       
    82 	thr.Logon(status);
       
    83 	thr.SetPriority(EPriorityMore);
       
    84 	thr.Resume();
       
    85 
       
    86 	TBool attached = ETrue;
       
    87 	for (i = 0; i < aCount; ++i)
       
    88 		{
       
    89 		TBuf8<RProperty::KMaxPropertySize> buf;
       
    90 		if (attached)
       
    91 			{
       
    92 			r = prop.Set(iBuf1);
       
    93 			TF_ERROR(r, r == KErrNone);
       
    94 			r = prop.Get(buf);
       
    95 			TF_ERROR(r, r == KErrNone);
       
    96 			}
       
    97 		else 
       
    98 			{
       
    99 			r = prop.Set(iCategory, iKey, iBuf1);
       
   100 			TF_ERROR(r, r == KErrNone);
       
   101 			r = prop.Get(iCategory, iKey, buf);
       
   102 			TF_ERROR(r, r == KErrNone);
       
   103 			}
       
   104 		TF_ERROR(KErrGeneral, !(buf.Compare(iBuf1) && buf.Compare(iBuf2)));
       
   105 		attached = !attached;
       
   106 		}
       
   107 
       
   108 	thr.Kill(EExitKill);
       
   109 	thr.Close();
       
   110 
       
   111 	User::WaitForRequest(status);
       
   112 	TF_ERROR(status.Int(), status.Int() == EExitKill);
       
   113 
       
   114 	prop.Delete(iCategory, iKey);
       
   115 	prop.Close();
       
   116 	}
       
   117 
       
   118 
       
   119 _LIT(KPropCancelRaceName, "RProperty::Cancel() Race");
       
   120  
       
   121 CPropCancelRace::CPropCancelRace(TUid aCategory, TUint aKey) : 
       
   122 	  CTestProgram(KPropCancelRaceName), iCategory(aCategory), iKey(aKey) 
       
   123 	{
       
   124 	}
       
   125 
       
   126 void CPropCancelRace::Trublemaker()
       
   127 	{
       
   128 	for(;;)
       
   129 		{
       
   130 		iProp.Cancel();
       
   131 		User::After(1);
       
   132 		}
       
   133 	}
       
   134 
       
   135 TInt CPropCancelRace::TrublemakerThreadEntry(TAny* ptr)
       
   136 	{
       
   137 	CPropCancelRace* prog = (CPropCancelRace*) ptr;
       
   138 	prog->Trublemaker();
       
   139 	return KErrNone;
       
   140 	}
       
   141 
       
   142 void CPropCancelRace::Run(TUint aCount)
       
   143 	{
       
   144 	TInt r = iProp.Define(iCategory, iKey, RProperty::EInt, KPassPolicy, KPassPolicy);
       
   145 	TF_ERROR(r, r == KErrNone);
       
   146 	r = iProp.Attach(iCategory,iKey);
       
   147 	TF_ERROR(r, r == KErrNone);
       
   148 
       
   149 	TRequestStatus thrStatus;
       
   150 	RThread thr;
       
   151 	r = thr.Create(KNullDesC, TrublemakerThreadEntry, 0x2000, NULL, this);
       
   152 	TF_ERROR(r, r == KErrNone);
       
   153 	thr.Logon(thrStatus);
       
   154 	thr.SetPriority(EPriorityMore);
       
   155 	thr.Resume();
       
   156 
       
   157 	for (TUint i = 0; i < aCount; ++i)
       
   158 		{
       
   159 		TRequestStatus status;
       
   160 		iProp.Subscribe(status);
       
   161 		TInt st = status.Int();
       
   162 		TF_ERROR(st, (st == KRequestPending) || (st == KErrCancel));
       
   163 		iProp.Set(1);
       
   164 		User::WaitForRequest(status);
       
   165 		st = status.Int();
       
   166 		TF_ERROR(st, (st == KErrNone) || (st == KErrCancel));
       
   167 		}
       
   168 
       
   169 	thr.Kill(EExitKill);
       
   170 	User::WaitForRequest(thrStatus);
       
   171 	TF_ERROR(thrStatus.Int(), thrStatus.Int() == EExitKill);
       
   172 	thr.Close();
       
   173 
       
   174 	iProp.Delete(iCategory, iKey);
       
   175 	iProp.Close();
       
   176 	}