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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\prime\t_sem.cpp
16 // Test the RSemaphore type.
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:
30 // Assumptions/Requirement/Pre-requisites:
31 // Failures and causes:
32 // Base Port information:
39 const TInt KMaxIterations=0x100;
40 const TInt KHeapSize=0x2000;
42 LOCAL_D RTest test(_L("T_SEM"));
43 LOCAL_D RSemaphore consumer;
44 LOCAL_D RSemaphore producer;
46 LOCAL_C TInt producerThreadEntryPoint(TAny* anArg)
48 // The entry point for the producer thread.
53 for (TInt i=0;i<KMaxIterations;i++)
61 enum {EFuncCreateNeg,EFuncSignalWithoutCreate,EFuncSignalNeg};
63 TInt CreateNegativeThread(TAny* aFunc)
71 case EFuncSignalWithoutCreate:
79 test.Panic(_L("Hit default label"));
84 GLDEF_C TInt E32Main()
86 // Test the RSemaphore type.
91 test.Start(_L("Create semaphores"));
92 TInt r=consumer.CreateLocal(1);
94 r=producer.CreateLocal(0);
96 test.Next(_L("Create thread"));
97 RThread producerThread;
98 r=producerThread.Create(_L("Producer"),producerThreadEntryPoint,KDefaultStackSize,KHeapSize,KHeapSize,NULL);
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();
107 test.Next(_L("Run semaphores"));
108 for (TInt i=0;i<KMaxIterations;i++)
110 test.Printf(_L("\r%03d"),i);
114 test.Printf(_L("\n"));
115 test.Next(_L("Wait for thread to die"));
116 User::WaitForRequest(tStat);
118 test.Next(_L("Kill thread"));
119 producerThread.Kill(0);
120 TName c=producerThread.ExitCategory();
121 TInt reason=producerThread.ExitReason();
122 switch (producerThread.ExitType())
125 test.Printf(_L("KILL: %d [%S]\n"),reason,&c);
128 test.Printf(_L("TERMINATE: %d [%S]\n"),reason,&c);
131 test.Printf(_L("PANIC: %d [%S]\n"),reason,&c);
134 test.Panic(_L("UNKNOWN: %d [%S]\n"),reason,&c);
137 test.Next(_L("Cleanup"));
138 producerThread.Close();
139 CLOSE_AND_WAIT(producer);
140 CLOSE_AND_WAIT(consumer);
142 test.Next(_L("Create(-1)"));
144 r=thread.Create(_L("SemTests"),CreateNegativeThread,0x1000,NULL,(TAny*)EFuncCreateNeg);
148 TBool justInTime=User::JustInTime();
149 User::SetJustInTime(EFalse);
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);
159 test.Next(_L("Signal without create"));
160 r=thread.Create(_L("SemTests"),CreateNegativeThread,0x1000,NULL,(TAny*)EFuncSignalWithoutCreate);
163 User::SetJustInTime(EFalse);
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);
173 test.Next(_L("Signal(-1)"));
174 r=thread.Create(_L("SemTests"),CreateNegativeThread,0x1000,NULL,(TAny*)EFuncSignalNeg);
177 User::SetJustInTime(EFalse);
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);