os/kernelhwsrv/kerneltest/e32test/prime/t_sem.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.
     1 // Copyright (c) 1994-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\prime\t_sem.cpp
    15 // Overview:
    16 // Test the RSemaphore type.
    17 // API Information:
    18 // RSemaphore
    19 // Details:
    20 // - Create two local semaphores, create a new thread, signal and wait on 
    21 // the semaphores numerous time. Kill the thread and verify results.
    22 // - Test creating a local semaphore with an initial count of -1. Verify 
    23 // failure results are as expected.
    24 // - Attempt to signal a semaphore without having done a create. Verify 
    25 // failure results are as expected.
    26 // - Attempt to signal a semaphore with signal count of -1. Verify failure
    27 // results are as expected.
    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 #include <e32panic.h>
    38 
    39 const TInt KMaxIterations=0x100;
    40 const TInt KHeapSize=0x2000;
    41 
    42 LOCAL_D RTest test(_L("T_SEM"));
    43 LOCAL_D RSemaphore consumer;
    44 LOCAL_D RSemaphore producer;
    45 
    46 LOCAL_C TInt producerThreadEntryPoint(TAny* anArg)
    47 //
    48 // The entry point for the producer thread.
    49 //
    50 	{
    51 
    52 	anArg=anArg;
    53 	for (TInt i=0;i<KMaxIterations;i++)
    54 		{
    55 		consumer.Signal();
    56 		producer.Wait();
    57 		}
    58 	return(KErrNone);
    59 	}
    60 
    61 enum {EFuncCreateNeg,EFuncSignalWithoutCreate,EFuncSignalNeg};
    62 
    63 TInt CreateNegativeThread(TAny* aFunc)
    64 	{
    65 	RSemaphore sem;
    66 	switch((TInt)aFunc)
    67 		{
    68 	case EFuncCreateNeg:
    69 		sem.CreateLocal(-1);
    70 		break;
    71 	case EFuncSignalWithoutCreate:
    72 		sem.Signal();
    73 		break;
    74 	case EFuncSignalNeg:
    75 		sem.CreateLocal(1);
    76 		sem.Signal(-1);
    77 		break;
    78 	default:
    79 		test.Panic(_L("Hit default label"));
    80 		}
    81 	return KErrNone;
    82 	}
    83 
    84 GLDEF_C TInt E32Main()
    85 //
    86 // Test the RSemaphore type.
    87 //
    88     {
    89 
    90 	test.Title();
    91 	test.Start(_L("Create semaphores"));
    92 	TInt r=consumer.CreateLocal(1);
    93 	test(r==KErrNone);
    94 	r=producer.CreateLocal(0);
    95 	test(r==KErrNone);
    96 	test.Next(_L("Create thread"));
    97 	RThread producerThread;
    98 	r=producerThread.Create(_L("Producer"),producerThreadEntryPoint,KDefaultStackSize,KHeapSize,KHeapSize,NULL);
    99 	test(r==KErrNone);
   100 	test.Next(_L("Logon thread"));
   101 	TRequestStatus tStat;
   102 	producerThread.Logon(tStat);
   103 	test(tStat==KRequestPending);
   104 	test.Next(_L("Resume thread"));
   105 	producerThread.Resume();
   106 	test(ETrue);
   107 	test.Next(_L("Run semaphores"));
   108 	for (TInt i=0;i<KMaxIterations;i++)
   109 		{
   110 		test.Printf(_L("\r%03d"),i);
   111 		consumer.Wait();
   112 		producer.Signal();
   113 		}
   114 	test.Printf(_L("\n"));
   115 	test.Next(_L("Wait for thread to die"));
   116 	User::WaitForRequest(tStat);
   117 	test(r==KErrNone);
   118 	test.Next(_L("Kill thread"));
   119 	producerThread.Kill(0);
   120 	TName c=producerThread.ExitCategory();
   121 	TInt reason=producerThread.ExitReason();
   122 	switch (producerThread.ExitType())
   123 		{
   124 	case EExitKill:
   125 		test.Printf(_L("KILL: %d [%S]\n"),reason,&c);
   126 		break;
   127 	case EExitTerminate:
   128 		test.Printf(_L("TERMINATE: %d [%S]\n"),reason,&c);
   129 		break;
   130 	case EExitPanic:
   131 		test.Printf(_L("PANIC: %d [%S]\n"),reason,&c);
   132 		break;
   133 	default:
   134 		test.Panic(_L("UNKNOWN: %d [%S]\n"),reason,&c);
   135 		break;
   136 		}
   137 	test.Next(_L("Cleanup"));
   138 	producerThread.Close();
   139 	CLOSE_AND_WAIT(producer);
   140 	CLOSE_AND_WAIT(consumer);
   141 	
   142 	test.Next(_L("Create(-1)"));
   143 	RThread thread;
   144 	r=thread.Create(_L("SemTests"),CreateNegativeThread,0x1000,NULL,(TAny*)EFuncCreateNeg);
   145 	test(r==KErrNone);
   146 	TRequestStatus stat;
   147 	thread.Logon(stat);
   148 	TBool justInTime=User::JustInTime();
   149 	User::SetJustInTime(EFalse);
   150 	thread.Resume();
   151 	User::WaitForRequest(stat);
   152 	test(stat==ESemCreateCountNegative);
   153 	User::SetJustInTime(justInTime);
   154 	test(thread.ExitReason()==ESemCreateCountNegative);
   155 	test(thread.ExitCategory()==_L("USER"));
   156 	test(thread.ExitType()==EExitPanic);
   157 	CLOSE_AND_WAIT(thread);
   158 	
   159 	test.Next(_L("Signal without create"));
   160 	r=thread.Create(_L("SemTests"),CreateNegativeThread,0x1000,NULL,(TAny*)EFuncSignalWithoutCreate);
   161 	test(r==KErrNone);
   162 	thread.Logon(stat);
   163 	User::SetJustInTime(EFalse);
   164 	thread.Resume();
   165 	User::WaitForRequest(stat);
   166 	test(stat==EBadHandle);
   167 	User::SetJustInTime(justInTime);
   168 	test(thread.ExitReason()==EBadHandle);
   169 	test(thread.ExitCategory()==_L("KERN-EXEC"));
   170 	test(thread.ExitType()==EExitPanic);
   171 	CLOSE_AND_WAIT(thread);
   172 
   173 	test.Next(_L("Signal(-1)"));
   174 	r=thread.Create(_L("SemTests"),CreateNegativeThread,0x1000,NULL,(TAny*)EFuncSignalNeg);
   175 	test(r==KErrNone);
   176 	thread.Logon(stat);
   177 	User::SetJustInTime(EFalse);
   178 	thread.Resume();
   179 	User::WaitForRequest(stat);
   180 	test(stat==ESemSignalCountNegative);
   181 	User::SetJustInTime(justInTime);
   182 	test(thread.ExitReason()==ESemSignalCountNegative);
   183 	test(thread.ExitCategory()==_L("USER"));
   184 	test(thread.ExitType()==EExitPanic);
   185 	CLOSE_AND_WAIT(thread);
   186 	test.End();
   187 	return(KErrNone);
   188     }
   189 
   190