|
1 // Copyright (c) 2003-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 "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 // |
|
15 |
|
16 #include "ThreadBase.h" |
|
17 _LIT(KSemaphoreNameOne, "SemaphoneNameOne"); |
|
18 _LIT(KSemaphoreNameTwo, "SemaphoneNameTwo"); |
|
19 const TInt KMaxHeapSize=0x400; // 1024 |
|
20 |
|
21 // |
|
22 /* CThreadBase - Implementation */ |
|
23 CThreadBase::CThreadBase() |
|
24 { |
|
25 } |
|
26 |
|
27 CThreadBase::~CThreadBase() |
|
28 { |
|
29 iThread.Close(); |
|
30 CloseSemaphore(); |
|
31 } |
|
32 |
|
33 void CThreadBase::Go() |
|
34 { |
|
35 iThread.Create( ThreadName(), CThreadBase::ThreadFunction, KDefaultStackSize, 0x2000, 0x20000, this ); |
|
36 iThread.Logon( iThreadStatus ); |
|
37 iThread.Resume(); |
|
38 } |
|
39 |
|
40 void CThreadBase::WaitForCompetion() |
|
41 { |
|
42 User::WaitForRequest( iThreadStatus ); |
|
43 } |
|
44 |
|
45 void CThreadBase::SetUpTestL( const TDesC& aThreadName ) |
|
46 { |
|
47 // Set's up extra resources. |
|
48 CConsoleBase* console = NULL; |
|
49 TSize size; |
|
50 |
|
51 // Create and name an RTest |
|
52 iTest = new(ELeave) RTest(aThreadName); |
|
53 iTest->Start(_L("Starting test")); |
|
54 |
|
55 // Position our console window |
|
56 size = iTest->Console()->ScreenSize(); |
|
57 size.iWidth = size.iWidth - 4; |
|
58 size.iHeight = (size.iHeight / 2) - 3; |
|
59 |
|
60 console = Console::NewL(aThreadName, size); |
|
61 delete const_cast<RTest*>(iTest)->Console(); |
|
62 |
|
63 iTest->SetConsole(console); |
|
64 console->ClearScreen(); |
|
65 } |
|
66 |
|
67 void CThreadBase::TakeDownTest() |
|
68 { |
|
69 iTest->Close(); |
|
70 delete iTest; |
|
71 iTest = NULL; |
|
72 } |
|
73 |
|
74 |
|
75 void CThreadBase::OpenSemaphore() |
|
76 { |
|
77 TInt success = KErrNone; |
|
78 |
|
79 success = iSemaphoreSignal.OpenGlobal( KSemaphoreNameOne ); |
|
80 if ( success == KErrNotFound ) |
|
81 { |
|
82 iSemaphoreSignal.CreateGlobal( KSemaphoreNameOne, 0 ); |
|
83 success = KErrNone; |
|
84 } |
|
85 |
|
86 |
|
87 success = iSemaphoreWait.OpenGlobal( KSemaphoreNameTwo ); |
|
88 if ( success == KErrNotFound ) |
|
89 { |
|
90 iSemaphoreWait.CreateGlobal( KSemaphoreNameTwo, 0 ); |
|
91 } |
|
92 |
|
93 iSemaphoreOpen = ETrue; |
|
94 } |
|
95 |
|
96 void CThreadBase::CloseSemaphore() |
|
97 { |
|
98 if (iSemaphoreOpen) |
|
99 { |
|
100 iSemaphoreSignal.Close(); |
|
101 iSemaphoreWait.Close(); |
|
102 } |
|
103 |
|
104 iSemaphoreOpen = EFalse; |
|
105 } |
|
106 |
|
107 void CThreadBase::SyncronizeSignal() |
|
108 { |
|
109 iSemaphoreSignal.Signal(); |
|
110 iSemaphoreWait.Wait(); |
|
111 iSemaphoreSignal.Signal(); |
|
112 } |
|
113 |
|
114 void CThreadBase::SyncronizeWait() |
|
115 { |
|
116 iSemaphoreSignal.Wait(); |
|
117 iSemaphoreWait.Signal(); |
|
118 iSemaphoreSignal.Wait(); |
|
119 } |
|
120 |
|
121 |
|
122 void CThreadBase::RandomDelay(TInt aMax, TInt aMin) const |
|
123 { |
|
124 TInt delay; |
|
125 TTime timeNow; |
|
126 TInt64 seed; |
|
127 TReal randomNumber; |
|
128 |
|
129 timeNow.UniversalTime(); |
|
130 seed = timeNow.Int64(); |
|
131 randomNumber = Math::FRand( seed ); |
|
132 delay = static_cast<TInt>( (randomNumber * static_cast<TReal>(aMax)) + aMin ) ; |
|
133 delay = delay % aMax; |
|
134 |
|
135 User::After( delay ); |
|
136 } |
|
137 |
|
138 void CThreadBase::DoRunThreadL() |
|
139 { |
|
140 CActiveScheduler* scheduler = NULL; |
|
141 scheduler = new (ELeave) CActiveScheduler; |
|
142 CleanupStack::PushL(scheduler); |
|
143 CActiveScheduler::Install(scheduler); |
|
144 |
|
145 OpenSemaphore(); |
|
146 SetUpTestL( ThreadName() ); |
|
147 |
|
148 ThreadL(); |
|
149 |
|
150 TakeDownTest(); |
|
151 CloseSemaphore(); |
|
152 |
|
153 CleanupStack::PopAndDestroy(scheduler); |
|
154 } |
|
155 |
|
156 TInt CThreadBase::RunThread() |
|
157 { |
|
158 |
|
159 TInt retval = KErrNone; |
|
160 |
|
161 iCleanup = CTrapCleanup::New(); |
|
162 |
|
163 if (iCleanup) |
|
164 { |
|
165 TRAP(retval, DoRunThreadL()); |
|
166 } |
|
167 else |
|
168 { |
|
169 retval = KErrNoMemory; |
|
170 } |
|
171 |
|
172 delete iCleanup; |
|
173 iCleanup = NULL; |
|
174 return retval; |
|
175 } |
|
176 |
|
177 TInt CThreadBase::ThreadFunction(TAny* aMe) |
|
178 { |
|
179 CThreadBase* me = static_cast<CThreadBase*>(aMe); |
|
180 return me->RunThread(); |
|
181 } |