00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00036 #include "producer.h"
00037
00044 CProducer* CProducer::NewL(CConsoleBase* aConsole,CQueue* aTokens)
00045 {
00046 CProducer* self = new (ELeave)CProducer;
00047 CleanupStack::PushL(self);
00048 self->ConstructL(aConsole,aTokens);
00049 CleanupStack::Pop(self);
00050 return self;
00051 }
00052
00064 void CProducer::ConstructL(CConsoleBase* aConsole,CQueue* aTokens)
00065 {
00066 iConsole = aConsole;
00067 iQueue = aTokens;
00068
00069
00070 iPeriodicProd = CPeriodic::NewL(CActive::EPriorityUserInput);
00071
00072
00073 _LIT(KProducer,"ProducerThread");
00074 User::LeaveIfError(iProdThread.Create(KProducer,ProdThreadFunc,KDefaultStackSize,KMinHeapSize,256*KMinHeapSize,iQueue,EOwnerThread));
00075 iProdThread.SetPriority(EPriorityMore);
00076
00077
00078
00079 iPeriodicProd->Start(0,3000000,TCallBack(InsertFunction,this));
00080 }
00081
00085 CProducer::CProducer()
00086 {
00087 }
00088
00092 void CProducer::Display()
00093 {
00094 RArray<TInt> array;
00095
00096
00097 iQueue->GetTokens(array);
00098
00099 iConsole->Printf(KNewLine);
00100 _LIT(KTextQueue,"Queue Contents:\n[ ");
00101 iConsole->Printf(KTextQueue);
00102
00103 _LIT(KTextIntFormat,"%d ");
00104
00105 for(TInt ix = 0 ; ix < array.Count() ; ix++)
00106 {
00107 iConsole->Printf(KTextIntFormat,array[ix]);
00108 }
00109
00110 _LIT(KTextQueueEnd,"]");
00111 iConsole->Printf(KTextQueueEnd);
00112 iConsole->Printf(KNewLine);
00113 iConsole->Printf(KNewLine);
00114 array.Close();
00115 }
00116
00122 TInt CProducer::ProdThreadFunc(TAny* aPtr)
00123 {
00124
00125 while(true)
00126 {
00127 CQueue* ptr = (CQueue*)aPtr;
00128 __ASSERT_ALWAYS(ptr,User::Panic(KTxtPanic,-1));
00129
00130 ptr->Insert();
00131
00132 RThread().Suspend();
00133 }
00134 return KErrNone;
00135 }
00136
00140 void CProducer::ResumeProd()
00141 {
00142
00143
00144 iProdThread.Resume();
00145 }
00146
00150 void CProducer::Produce()
00151 {
00152
00153 iQueue->Insert();
00154 }
00155
00159 CProducer::~CProducer()
00160 {
00161 iProdThread.Kill(KErrCancel);
00162 delete iPeriodicProd;
00163 }
00164
00165
00171 TInt CProducer::InsertFunction(TAny* aPtr)
00172 {
00173 CProducer* ptr = static_cast<CProducer*> (aPtr);
00174 __ASSERT_ALWAYS(ptr,User::Panic(KTxtPanic,-1));
00175
00176 ptr->ResumeProd();
00177 return KErrNone;
00178 }