kerneltest/e32test/misc/t_tmout.cpp
changeset 9 96e5fb8b040d
child 43 c1f20ce4abcf
equal deleted inserted replaced
-1:000000000000 9:96e5fb8b040d
       
     1 // Copyright (c) 1996-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 // e32test\misc\t_tmout.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <e32test.h>
       
    19 #include "d_rndtim.h"
       
    20 
       
    21 RTest test(_L("T_TMOUT"));
       
    22 
       
    23 _LIT(KLitThread1Name,"IsrThread");
       
    24 const TInt KThread1Priority = 56;
       
    25 _LIT(KLitThread2Name,"WaitThread");
       
    26 const TInt KThread2Priority = 55;
       
    27 
       
    28 const TInt KTimeout = 5;	// milliseconds
       
    29 
       
    30 volatile TUint32 RandomSignalInterval;
       
    31 volatile TUint32 RandomSignalCount;
       
    32 volatile TUint32 TotalCount;
       
    33 volatile TUint32 TimeoutCount;
       
    34 volatile TUint32 BadCount;
       
    35 volatile TUint32 Bad0Count;
       
    36 volatile TUint32 Bad1Count;
       
    37 volatile TUint32 Calibration;
       
    38 
       
    39 RThread IsrT;
       
    40 RThread WaitT;
       
    41 RSemaphore Sem;
       
    42 
       
    43 TInt IsrThread(TAny*)
       
    44 	{
       
    45 	RThread me;
       
    46 	RRndTim rt;
       
    47 	TInt r = rt.Open();
       
    48 	if (r!=KErrNone)
       
    49 		return r;
       
    50 	r = rt.SetPriority(me, KThread1Priority);
       
    51 	if (r!=KErrNone)
       
    52 		return r;
       
    53 	rt.StartTimer();
       
    54 	Calibration = rt.Calibrate(1024);
       
    55 	r = rt.SetPriority(WaitT, KThread2Priority);
       
    56 	if (r!=KErrNone)
       
    57 		return r;
       
    58 	WaitT.Resume();
       
    59 	RThread::Rendezvous(KErrNone);
       
    60 	FOREVER
       
    61 		{
       
    62 		rt.Wait();
       
    63 		if (RandomSignalCount && !--RandomSignalCount)
       
    64 			{
       
    65 			RandomSignalCount = RandomSignalInterval;
       
    66 			Sem.Signal();
       
    67 			}
       
    68 		}
       
    69 	}
       
    70 
       
    71 TInt WaitThread(TAny*)
       
    72 	{
       
    73 	TUint32 t1, t2;
       
    74 	FOREVER
       
    75 		{
       
    76 		t1 = User::NTickCount();
       
    77 		TInt r = Sem.Wait(KTimeout*1000);
       
    78 		t2 = User::NTickCount();
       
    79 		++TotalCount;
       
    80 		if (r == KErrTimedOut)
       
    81 			{
       
    82 			++TimeoutCount;
       
    83 			TInt d = (TInt)(t2-t1);
       
    84 			if (d<KTimeout)
       
    85 				++BadCount;
       
    86 			if (d==0)
       
    87 				++Bad0Count;
       
    88 			if (d==1)
       
    89 				++Bad1Count;
       
    90 			}
       
    91 		}
       
    92 	}
       
    93 
       
    94 GLDEF_C TInt E32Main()
       
    95 	{
       
    96 	test.Title();
       
    97 
       
    98 	test.Start(_L("Load device driver"));
       
    99 	TInt r = User::LoadLogicalDevice(_L("d_rndtim.ldd"));
       
   100 	if (r == KErrNotFound)
       
   101 		{
       
   102 		test.Printf(_L("Test not supported on this platform\n"));
       
   103 		test.End();
       
   104 		return 0;
       
   105 		}
       
   106 
       
   107 	test.Next(_L("Create semaphore"));
       
   108 	r = Sem.CreateLocal(0);
       
   109 	test(r==KErrNone);
       
   110 
       
   111 	test.Next(_L("Create ISR thread"));
       
   112 	r=IsrT.Create(KLitThread1Name, &IsrThread, 0x1000, NULL, NULL);
       
   113 	test(r==KErrNone);
       
   114 
       
   115 	test.Next(_L("Create wait thread"));
       
   116 	r=WaitT.Create(KLitThread2Name, &WaitThread, 0x1000, NULL, NULL);
       
   117 	test(r==KErrNone);
       
   118 
       
   119 	TRequestStatus s;
       
   120 	IsrT.Rendezvous(s);
       
   121 	test(s==KRequestPending);
       
   122 
       
   123 	IsrT.Resume();
       
   124 	User::WaitForRequest(s);
       
   125 	test(s==KErrNone);
       
   126 	test(IsrT.ExitType() == EExitPending);
       
   127 	test(WaitT.ExitType() == EExitPending);
       
   128 
       
   129 	test.Printf(_L("%d random timers in 1024ms"), Calibration);
       
   130 	TUint32 interval = (KTimeout * Calibration + 512)/1024;
       
   131 	test.Printf(_L("Interval %d"), interval);
       
   132 	RandomSignalInterval = interval;
       
   133 	RandomSignalCount = interval;
       
   134 
       
   135 	FOREVER
       
   136 		{
       
   137 		test.Printf(_L("Total: %8d Timeout: %8d Bad: %4d Bad0: %4d Bad1: %4d\n"), TotalCount, TimeoutCount, BadCount, Bad0Count, Bad1Count);
       
   138 		User::After(1000000);
       
   139 		}
       
   140 
       
   141 
       
   142 	return 0;
       
   143 	}