00001 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). 00002 // All rights reserved. 00003 // This component and the accompanying materials are made available 00004 // under the terms of "Eclipse Public License v1.0" 00005 // which accompanies this distribution, and is available 00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html". 00007 // 00008 // Initial Contributors: 00009 // Nokia Corporation - initial contribution. 00010 // 00011 // Contributors: 00012 // 00013 // Description: 00014 // Contains the definition of member functions of the CProducer class. 00015 // 00016 00017 00018 00023 #include "producer.h" 00024 00031 CProducer* CProducer::NewL(CConsoleBase* aConsole,CQueue* aTokens) 00032 { 00033 CProducer* self = new (ELeave)CProducer; 00034 CleanupStack::PushL(self); 00035 self->ConstructL(aConsole,aTokens); 00036 CleanupStack::Pop(self); 00037 return self; 00038 } 00039 00051 void CProducer::ConstructL(CConsoleBase* aConsole,CQueue* aTokens) 00052 { 00053 iConsole = aConsole; 00054 iQueue = aTokens; 00055 00056 // Create an object of the CPeriodic class. 00057 iPeriodicProd = CPeriodic::NewL(CActive::EPriorityUserInput); 00058 00059 // Create the producer thread. 00060 _LIT(KProducer,"ProducerThread"); 00061 User::LeaveIfError(iProdThread.Create(KProducer,ProdThreadFunc,KDefaultStackSize,KMinHeapSize,256*KMinHeapSize,iQueue,EOwnerThread)); 00062 iProdThread.SetPriority(EPriorityMore); 00063 00064 // The producer thread invokes the InsertFunction() function every two seconds. 00065 // Associate InsertFunction() as the call back function for the iPeriodicCons object. 00066 iPeriodicProd->Start(0,3000000,TCallBack(InsertFunction,this)); 00067 } 00068 00072 CProducer::CProducer() 00073 { 00074 } 00075 00079 void CProducer::Display() 00080 { 00081 RArray<TInt> array; 00082 // Initialize the array. 00083 // The array reflects the contents of the CQueue queue. 00084 iQueue->GetTokens(array); 00085 // Print the array. 00086 iConsole->Printf(KNewLine); 00087 _LIT(KTextQueue,"Queue Contents:\n[ "); 00088 iConsole->Printf(KTextQueue); 00089 00090 _LIT(KTextIntFormat,"%d "); 00091 00092 for(TInt ix = 0 ; ix < array.Count() ; ix++) 00093 { 00094 iConsole->Printf(KTextIntFormat,array[ix]); 00095 } 00096 00097 _LIT(KTextQueueEnd,"]"); 00098 iConsole->Printf(KTextQueueEnd); 00099 iConsole->Printf(KNewLine); 00100 iConsole->Printf(KNewLine); 00101 array.Close(); 00102 } 00103 00109 TInt CProducer::ProdThreadFunc(TAny* aPtr) 00110 { 00111 // The while loop mechanism ensures that the thread performs the CQueue::Insert() operation each time RThread::Resume() is invoked on the thread handle. 00112 while(true) 00113 { 00114 CQueue* ptr = (CQueue*)aPtr; 00115 __ASSERT_ALWAYS(ptr,User::Panic(KTxtPanic,-1)); 00116 // Insert a token into the queue. 00117 ptr->Insert(); 00118 // Suspend the thread. 00119 RThread().Suspend(); 00120 } 00121 return KErrNone; 00122 } 00123 00127 void CProducer::ResumeProd() 00128 { 00129 // Call the RThread::Resume() function on the producer thread. 00130 // Control goes to the CProducer::ProdThreadFunc() function after this statement. 00131 iProdThread.Resume(); 00132 } 00133 00137 void CProducer::Produce() 00138 { 00139 // Call the CQueue::Insert() function. 00140 iQueue->Insert(); 00141 } 00142 00146 CProducer::~CProducer() 00147 { 00148 iProdThread.Kill(KErrCancel); 00149 delete iPeriodicProd; 00150 } 00151 00152 00158 TInt CProducer::InsertFunction(TAny* aPtr) 00159 { 00160 CProducer* ptr = static_cast<CProducer*> (aPtr); 00161 __ASSERT_ALWAYS(ptr,User::Panic(KTxtPanic,-1)); 00162 // Invoke the RThread::Resume() function on the producer thread repeatedly. 00163 ptr->ResumeProd(); 00164 return KErrNone; 00165 }
Copyright ©2010 Nokia Corporation and/or its subsidiary(-ies).
All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the Eclipse Public License
v1.0.