00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00057 iPeriodicProd = CPeriodic::NewL(CActive::EPriorityUserInput);
00058
00059
00060 _LIT(KProducer,"ProducerThread");
00061 User::LeaveIfError(iProdThread.Create(KProducer,ProdThreadFunc,KDefaultStackSize,KMinHeapSize,256*KMinHeapSize,iQueue,EOwnerThread));
00062 iProdThread.SetPriority(EPriorityMore);
00063
00064
00065
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
00083
00084 iQueue->GetTokens(array);
00085
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
00112 while(true)
00113 {
00114 CQueue* ptr = (CQueue*)aPtr;
00115 __ASSERT_ALWAYS(ptr,User::Panic(KTxtPanic,-1));
00116
00117 ptr->Insert();
00118
00119 RThread().Suspend();
00120 }
00121 return KErrNone;
00122 }
00123
00127 void CProducer::ResumeProd()
00128 {
00129
00130
00131 iProdThread.Resume();
00132 }
00133
00137 void CProducer::Produce()
00138 {
00139
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
00163 ptr->ResumeProd();
00164 return KErrNone;
00165 }