1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/prime/t_sem.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,190 @@
1.4 +// Copyright (c) 1994-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// e32test\prime\t_sem.cpp
1.18 +// Overview:
1.19 +// Test the RSemaphore type.
1.20 +// API Information:
1.21 +// RSemaphore
1.22 +// Details:
1.23 +// - Create two local semaphores, create a new thread, signal and wait on
1.24 +// the semaphores numerous time. Kill the thread and verify results.
1.25 +// - Test creating a local semaphore with an initial count of -1. Verify
1.26 +// failure results are as expected.
1.27 +// - Attempt to signal a semaphore without having done a create. Verify
1.28 +// failure results are as expected.
1.29 +// - Attempt to signal a semaphore with signal count of -1. Verify failure
1.30 +// results are as expected.
1.31 +// Platforms/Drives/Compatibility:
1.32 +// All.
1.33 +// Assumptions/Requirement/Pre-requisites:
1.34 +// Failures and causes:
1.35 +// Base Port information:
1.36 +//
1.37 +//
1.38 +
1.39 +#include <e32test.h>
1.40 +#include <e32panic.h>
1.41 +
1.42 +const TInt KMaxIterations=0x100;
1.43 +const TInt KHeapSize=0x2000;
1.44 +
1.45 +LOCAL_D RTest test(_L("T_SEM"));
1.46 +LOCAL_D RSemaphore consumer;
1.47 +LOCAL_D RSemaphore producer;
1.48 +
1.49 +LOCAL_C TInt producerThreadEntryPoint(TAny* anArg)
1.50 +//
1.51 +// The entry point for the producer thread.
1.52 +//
1.53 + {
1.54 +
1.55 + anArg=anArg;
1.56 + for (TInt i=0;i<KMaxIterations;i++)
1.57 + {
1.58 + consumer.Signal();
1.59 + producer.Wait();
1.60 + }
1.61 + return(KErrNone);
1.62 + }
1.63 +
1.64 +enum {EFuncCreateNeg,EFuncSignalWithoutCreate,EFuncSignalNeg};
1.65 +
1.66 +TInt CreateNegativeThread(TAny* aFunc)
1.67 + {
1.68 + RSemaphore sem;
1.69 + switch((TInt)aFunc)
1.70 + {
1.71 + case EFuncCreateNeg:
1.72 + sem.CreateLocal(-1);
1.73 + break;
1.74 + case EFuncSignalWithoutCreate:
1.75 + sem.Signal();
1.76 + break;
1.77 + case EFuncSignalNeg:
1.78 + sem.CreateLocal(1);
1.79 + sem.Signal(-1);
1.80 + break;
1.81 + default:
1.82 + test.Panic(_L("Hit default label"));
1.83 + }
1.84 + return KErrNone;
1.85 + }
1.86 +
1.87 +GLDEF_C TInt E32Main()
1.88 +//
1.89 +// Test the RSemaphore type.
1.90 +//
1.91 + {
1.92 +
1.93 + test.Title();
1.94 + test.Start(_L("Create semaphores"));
1.95 + TInt r=consumer.CreateLocal(1);
1.96 + test(r==KErrNone);
1.97 + r=producer.CreateLocal(0);
1.98 + test(r==KErrNone);
1.99 + test.Next(_L("Create thread"));
1.100 + RThread producerThread;
1.101 + r=producerThread.Create(_L("Producer"),producerThreadEntryPoint,KDefaultStackSize,KHeapSize,KHeapSize,NULL);
1.102 + test(r==KErrNone);
1.103 + test.Next(_L("Logon thread"));
1.104 + TRequestStatus tStat;
1.105 + producerThread.Logon(tStat);
1.106 + test(tStat==KRequestPending);
1.107 + test.Next(_L("Resume thread"));
1.108 + producerThread.Resume();
1.109 + test(ETrue);
1.110 + test.Next(_L("Run semaphores"));
1.111 + for (TInt i=0;i<KMaxIterations;i++)
1.112 + {
1.113 + test.Printf(_L("\r%03d"),i);
1.114 + consumer.Wait();
1.115 + producer.Signal();
1.116 + }
1.117 + test.Printf(_L("\n"));
1.118 + test.Next(_L("Wait for thread to die"));
1.119 + User::WaitForRequest(tStat);
1.120 + test(r==KErrNone);
1.121 + test.Next(_L("Kill thread"));
1.122 + producerThread.Kill(0);
1.123 + TName c=producerThread.ExitCategory();
1.124 + TInt reason=producerThread.ExitReason();
1.125 + switch (producerThread.ExitType())
1.126 + {
1.127 + case EExitKill:
1.128 + test.Printf(_L("KILL: %d [%S]\n"),reason,&c);
1.129 + break;
1.130 + case EExitTerminate:
1.131 + test.Printf(_L("TERMINATE: %d [%S]\n"),reason,&c);
1.132 + break;
1.133 + case EExitPanic:
1.134 + test.Printf(_L("PANIC: %d [%S]\n"),reason,&c);
1.135 + break;
1.136 + default:
1.137 + test.Panic(_L("UNKNOWN: %d [%S]\n"),reason,&c);
1.138 + break;
1.139 + }
1.140 + test.Next(_L("Cleanup"));
1.141 + producerThread.Close();
1.142 + CLOSE_AND_WAIT(producer);
1.143 + CLOSE_AND_WAIT(consumer);
1.144 +
1.145 + test.Next(_L("Create(-1)"));
1.146 + RThread thread;
1.147 + r=thread.Create(_L("SemTests"),CreateNegativeThread,0x1000,NULL,(TAny*)EFuncCreateNeg);
1.148 + test(r==KErrNone);
1.149 + TRequestStatus stat;
1.150 + thread.Logon(stat);
1.151 + TBool justInTime=User::JustInTime();
1.152 + User::SetJustInTime(EFalse);
1.153 + thread.Resume();
1.154 + User::WaitForRequest(stat);
1.155 + test(stat==ESemCreateCountNegative);
1.156 + User::SetJustInTime(justInTime);
1.157 + test(thread.ExitReason()==ESemCreateCountNegative);
1.158 + test(thread.ExitCategory()==_L("USER"));
1.159 + test(thread.ExitType()==EExitPanic);
1.160 + CLOSE_AND_WAIT(thread);
1.161 +
1.162 + test.Next(_L("Signal without create"));
1.163 + r=thread.Create(_L("SemTests"),CreateNegativeThread,0x1000,NULL,(TAny*)EFuncSignalWithoutCreate);
1.164 + test(r==KErrNone);
1.165 + thread.Logon(stat);
1.166 + User::SetJustInTime(EFalse);
1.167 + thread.Resume();
1.168 + User::WaitForRequest(stat);
1.169 + test(stat==EBadHandle);
1.170 + User::SetJustInTime(justInTime);
1.171 + test(thread.ExitReason()==EBadHandle);
1.172 + test(thread.ExitCategory()==_L("KERN-EXEC"));
1.173 + test(thread.ExitType()==EExitPanic);
1.174 + CLOSE_AND_WAIT(thread);
1.175 +
1.176 + test.Next(_L("Signal(-1)"));
1.177 + r=thread.Create(_L("SemTests"),CreateNegativeThread,0x1000,NULL,(TAny*)EFuncSignalNeg);
1.178 + test(r==KErrNone);
1.179 + thread.Logon(stat);
1.180 + User::SetJustInTime(EFalse);
1.181 + thread.Resume();
1.182 + User::WaitForRequest(stat);
1.183 + test(stat==ESemSignalCountNegative);
1.184 + User::SetJustInTime(justInTime);
1.185 + test(thread.ExitReason()==ESemSignalCountNegative);
1.186 + test(thread.ExitCategory()==_L("USER"));
1.187 + test(thread.ExitType()==EExitPanic);
1.188 + CLOSE_AND_WAIT(thread);
1.189 + test.End();
1.190 + return(KErrNone);
1.191 + }
1.192 +
1.193 +