kerneltest/e32test/bench/t_ctxsw3.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 1997-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\bench\t_ctxsw3.cpp
       
    15 // Overview:
       
    16 // Benchmark the inter-thread context switching. 
       
    17 // API Information:
       
    18 // RThread, RRSemaphore.
       
    19 // Details:
       
    20 // - Create a semaphore.
       
    21 // - Create a thread (thread1) with a handle to the Semaphore and 
       
    22 // priority EPriorityLess. Verify the successful creation.
       
    23 // - Create another thread (thread2) with a handle to the Semaphore 
       
    24 // and priority EPriorityMuchLess. Verify the successful creation.
       
    25 // - thread1 counts the number of times the semaphore count is decremented.
       
    26 // - thread2 counts the number of times the semaphore count is incremented.
       
    27 // - Display the number of times the semaphore is signalled per second.
       
    28 // Platforms/Drives/Compatibility:
       
    29 // All.
       
    30 // Assumptions/Requirement/Pre-requisites:
       
    31 // Failures and causes:
       
    32 // Base Port information:
       
    33 // 
       
    34 //
       
    35 
       
    36 #include <e32test.h>
       
    37 
       
    38 RTest test(_L("T_CTXSW3"));
       
    39 
       
    40 LOCAL_D TInt Count;
       
    41 
       
    42 LOCAL_D RSemaphore StartSem;
       
    43 
       
    44 LOCAL_C TInt Thread1(TAny* aSemaphore)
       
    45 	{
       
    46 	RSemaphore s;
       
    47 	s.SetHandle((TInt)aSemaphore);
       
    48 	StartSem.Wait();
       
    49 	FOREVER
       
    50 		{
       
    51 		s.Wait();
       
    52 		Count++;
       
    53 		}
       
    54 	}
       
    55 
       
    56 LOCAL_C TInt Thread2(TAny* aSemaphore)
       
    57 	{
       
    58 	RSemaphore s;
       
    59 	s.SetHandle((TInt)aSemaphore);
       
    60 	StartSem.Wait();
       
    61 	FOREVER
       
    62 		{
       
    63 		s.Signal();
       
    64 		Count++;
       
    65 		}
       
    66 	}
       
    67 
       
    68 const TInt KHeapSize=4096;
       
    69 
       
    70 GLDEF_C TInt E32Main()
       
    71 	{
       
    72 	test.Title();
       
    73 	test.Start(_L("Timing inter-thread context switches..."));
       
    74 
       
    75 	TInt r=StartSem.CreateLocal(0);
       
    76 	test(r==KErrNone);
       
    77 
       
    78 	RSemaphore sem;
       
    79 	r=sem.CreateLocal(0);
       
    80 	test(r==KErrNone);
       
    81 	RThread t1,t2;
       
    82 	r=t1.Create(_L("Thread1"),Thread1,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)sem.Handle());
       
    83 	test(r==KErrNone);
       
    84 	t1.SetPriority(EPriorityLess);
       
    85 	r=t2.Create(_L("Thread2"),Thread2,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)sem.Handle());
       
    86 	test(r==KErrNone);
       
    87 	t2.SetPriority(EPriorityMuchLess);
       
    88 
       
    89 	TRequestStatus s1,s2;
       
    90 	t1.Logon(s1);
       
    91 	t2.Logon(s2);
       
    92 	t1.Resume();
       
    93 	t2.Resume();
       
    94 
       
    95 	Count=0;
       
    96 	User::After(20000);
       
    97 	StartSem.Signal(2);
       
    98 	User::After(2000000);
       
    99 	TInt count=Count;
       
   100 
       
   101 	t2.Kill(0);
       
   102 	User::WaitForRequest(s2);
       
   103 	t1.Kill(0);
       
   104 	User::WaitForRequest(s1);
       
   105 	t2.Close();
       
   106 	t1.Close();
       
   107 	sem.Close();
       
   108 
       
   109 	test.Printf(_L("%d per second\n"),count/2);
       
   110 
       
   111 	test.End();
       
   112 	return 0;
       
   113 	}