00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "..\inc\threadengine.h"
00013 #include "threadappview.h"
00014 #include "threadanimation.h"
00015 #include <thread.rsg>
00016 #include <StringLoader.h>
00017
00018
00019 const TInt KThreadUpdateInterval = 200000;
00020
00021
00022 const TInt KThreadWatchdogInterval = 5000000;
00023
00024
00025
00026
00027
00028
00029 CThreadEngine::CThreadEngine(void): CTimer(CActive::EPriorityStandard), iNotSynchonizedCounter(0),
00030 iSynchronizedCounter(0), iCreatedThreads(EFalse)
00031 {
00032
00033 iMutex.CreateLocal();
00034
00035 }
00036
00037
00038
00039
00040
00041
00042 CThreadEngine::~CThreadEngine(void)
00043 {
00044
00045 iThreadOne.Kill(KErrNone);
00046 iThreadTwo.Kill(KErrNone);
00047 iThreadThree.Kill(KErrNone);
00048
00049
00050 iThreadOne.Close();
00051 iThreadTwo.Close();
00052 iThreadThree.Close();
00053 }
00054
00055
00056 CThreadEngine* CThreadEngine::NewL(CThreadAppView* aView)
00057 {
00058 CThreadEngine* self = CThreadEngine::NewLC(aView);
00059 CleanupStack::Pop(self);
00060 return self;
00061 }
00062
00063 CThreadEngine* CThreadEngine::NewLC(CThreadAppView* aView)
00064 {
00065 CThreadEngine* self = new (ELeave) CThreadEngine;
00066 CleanupStack::PushL(self);
00067 self->ConstructL(aView);
00068 return self;
00069 }
00070
00071
00072 void CThreadEngine::ConstructL(CThreadAppView* aView)
00073 {
00074 CTimer::ConstructL();
00075
00076 iView = aView;
00077 CActiveScheduler::Add( this );
00078 }
00079
00080
00081
00082
00083
00084
00085 void CThreadEngine::StartL()
00086 {
00087
00088 if ( iCreatedThreads == EFalse )
00089 {
00090 CreateThreadsL();
00091 After( KThreadWatchdogInterval );
00092 }
00093 }
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 void CThreadEngine::RunL()
00104 {
00105
00106
00107 if ( iThreadOne.ExitType() == EExitKill )
00108 {
00109 HBufC* threadWasKilled = StringLoader::LoadLC( R_THREAD1_WAS_KILLED );
00110 iView->DrawText( *threadWasKilled );
00111 CleanupStack::PopAndDestroy( threadWasKilled );
00112
00113
00114
00115
00116 iThreadOne.Close();
00117
00118
00119 if ( iThreadOne.Create( _L("thread1"), ExecuteThreadOne, 4096, KMinHeapSize,
00120 256*KMinHeapSize, &iNotSynchonizedCounter) != KErrNone )
00121 {
00122
00123 HBufC* threadCreateFailureText = StringLoader::LoadLC( R_THREAD1_CREATE_FAILURE );
00124 iView->DrawText( *threadCreateFailureText );
00125 CleanupStack::PopAndDestroy( threadCreateFailureText);
00126 }
00127 else
00128 {
00129
00130 iThreadOne.Resume();
00131
00132 iView->iAnimationOne->StartAnimationL();
00133 }
00134 }
00135
00136
00137 if ( iThreadTwo.ExitType() == EExitKill )
00138 {
00139 HBufC* threadWasKilled = StringLoader::LoadLC( R_THREAD2_WAS_KILLED );
00140 iView->DrawText( *threadWasKilled );
00141 CleanupStack::PopAndDestroy( threadWasKilled );
00142
00143 iThreadTwo.Close();
00144
00145
00146 if ( KErrNone != iThreadTwo.Create( _L("thread2") , ExecuteThreadTwo, 4096, KMinHeapSize,
00147 256*KMinHeapSize, this ) )
00148 {
00149
00150 HBufC* threadCreateFailureText = StringLoader::LoadLC( R_THREAD2_CREATE_FAILURE );
00151 iView->DrawText( *threadCreateFailureText );
00152 CleanupStack::PopAndDestroy( threadCreateFailureText);
00153 }
00154 else
00155 {
00156
00157 iThreadTwo.Resume();
00158 iView->iAnimationTwo->StartAnimationL();
00159 }
00160 }
00161
00162
00163 if ( iThreadThree.ExitType() == EExitKill )
00164 {
00165 HBufC* threadWasKilled = StringLoader::LoadLC( R_THREAD3_WAS_KILLED );
00166 iView->DrawText( *threadWasKilled );
00167 CleanupStack::PopAndDestroy( threadWasKilled );
00168
00169 iThreadThree.Close();
00170
00171
00172 if ( KErrNone != iThreadThree.Create( _L("thread3") , ExecuteThreadThree, 4096, KMinHeapSize,
00173 256*KMinHeapSize, this) )
00174 {
00175
00176 HBufC* threadCreateFailureText = StringLoader::LoadLC( R_THREAD1_CREATE_FAILURE );
00177 iView->DrawText( *threadCreateFailureText );
00178 CleanupStack::PopAndDestroy( threadCreateFailureText );
00179 }
00180 else
00181 {
00182
00183 iThreadThree.Resume();
00184 iView->iAnimationThree->StartAnimationL();
00185 }
00186 }
00187
00188
00189 After( KThreadWatchdogInterval );
00190 }
00191
00192
00193
00194
00195
00196
00197 void CThreadEngine::DoCancel()
00198 {
00199 Cancel();
00200 }
00201
00202
00203
00204
00205
00206
00207 TInt CThreadEngine::RunError(TInt)
00208 {
00209 return KErrNone;
00210 }
00211
00212
00213
00214
00215
00216
00217
00218
00219 void CThreadEngine::ThreadKilledText(const TDesC& aText, TInt aCount)
00220 {
00221 TBuf<50> tempBuf;
00222
00223 tempBuf.Append( aText );
00224 tempBuf.AppendNum( aCount );
00225 iView->DrawText( tempBuf );
00226 }
00227
00228
00229
00230
00231
00232
00233
00234
00235 void CThreadEngine::KillThread(TInt aThreadCount)
00236 {
00237
00238 if ( iCreatedThreads == EFalse )
00239 {
00240 return;
00241 }
00242
00243 HBufC* killedText;
00244
00245 switch( aThreadCount )
00246 {
00247 case 1:
00248
00249 iThreadOne.Kill(KErrNone);
00250
00251 killedText = StringLoader::LoadLC(R_KILLED_THREAD1);
00252 ThreadKilledText( *killedText, iNotSynchonizedCounter);
00253 CleanupStack::PopAndDestroy(killedText);
00254
00255 iNotSynchonizedCounter = 0;
00256
00257 iView->iAnimationOne->StopAnimation();
00258
00259 break;
00260 case 2:
00261 iThreadTwo.Kill(KErrNone);
00262
00263 killedText = StringLoader::LoadLC(R_KILLED_THREAD2);
00264 ThreadKilledText( *killedText, iSynchronizedCounter);
00265 CleanupStack::PopAndDestroy(killedText);
00266
00267
00268 SetSyncValue(0);
00269
00270 iView->iAnimationTwo->StopAnimation();
00271
00272 break;
00273 case 3:
00274 iThreadThree.Kill(KErrNone);
00275
00276 killedText = StringLoader::LoadLC(R_KILLED_THREAD3);
00277 ThreadKilledText( *killedText, iSynchronizedCounter );
00278 CleanupStack::PopAndDestroy( killedText );
00279
00280
00281 SetSyncValue(0);
00282
00283 iView->iAnimationThree->StopAnimation();
00284 break;
00285 }
00286 }
00287
00288
00289
00290
00291
00292
00293 TInt CThreadEngine::ExecuteThreadOne(TAny *aPtr)
00294 {
00295
00296 TInt* notSync = static_cast<TInt*>(aPtr);
00297
00298 TBool loopConditionOne = ETrue;
00299 while ( loopConditionOne )
00300 {
00301
00302
00303 *notSync = *notSync + 1;
00304
00305 User::After( KThreadUpdateInterval );
00306 }
00307
00308 return KErrNone;
00309 }
00310
00311
00312
00313
00314
00315
00316 TInt CThreadEngine::ExecuteThreadTwo(TAny *aPtr)
00317 {
00318
00319 CThreadEngine* engine = static_cast<CThreadEngine*>(aPtr);
00320
00321 TBool loopConditionTwo = ETrue;
00322 while ( loopConditionTwo )
00323 {
00324
00325
00326
00327 engine->SetSyncValue(engine->GetSyncValue()+1);
00328 User::After( KThreadUpdateInterval );
00329 }
00330
00331 return KErrNone;
00332 }
00333
00334
00335
00336
00337
00338
00339 TInt CThreadEngine::ExecuteThreadThree(TAny *aPtr)
00340 {
00341 CThreadEngine* engine = static_cast<CThreadEngine*>(aPtr);
00342
00343 TBool loopConditionThree = ETrue;
00344 while ( loopConditionThree )
00345 {
00346
00347
00348
00349 engine->SetSyncValue(engine->GetSyncValue()+1);
00350 User::After( KThreadUpdateInterval );
00351 }
00352
00353 return KErrNone;
00354 }
00355
00356
00357
00358
00359
00360
00361
00362 void CThreadEngine::SetSyncValue(TInt aValue)
00363 {
00364 iMutex.Wait();
00365 iSynchronizedCounter = aValue;
00366 iMutex.Signal();
00367 }
00368
00369
00370
00371
00372
00373
00374
00375 TInt CThreadEngine::GetSyncValue() const
00376 {
00377 return iSynchronizedCounter;
00378 }
00379
00380
00381
00382
00383
00384
00385
00386 void CThreadEngine::CreateThreadsL()
00387 {
00388
00389 iThreadOne.Create( _L("thread1") , ExecuteThreadOne, 4096, KMinHeapSize, 256*KMinHeapSize,
00390 &iNotSynchonizedCounter);
00391 iThreadOne.Resume();
00392 iView->iAnimationOne->StartAnimationL();
00393
00394
00395
00396
00397
00398
00399 iThreadTwo.Create( _L("thread2") , ExecuteThreadTwo, 4096, KMinHeapSize, 256*KMinHeapSize,
00400 this);
00401 iThreadTwo.Resume();
00402 iView->iAnimationTwo->StartAnimationL();
00403
00404
00405 iThreadThree.Create(_L("thread3") , ExecuteThreadThree, 4096, KMinHeapSize, 256*KMinHeapSize,
00406 this );
00407 iThreadThree.Resume();
00408 iView->iAnimationThree->StartAnimationL();
00409
00410
00411 iCreatedThreads = ETrue;
00412 }