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