examples/Base/IPC/condvar/condvarlocal/src/producer.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  Contains the definition of member functions of the CProducer class. 
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         // Create an object of the CPeriodic class.
00070         iPeriodicProd = CPeriodic::NewL(CActive::EPriorityUserInput);
00071 
00072         // Create the producer thread.
00073         _LIT(KProducer,"ProducerThread");
00074         User::LeaveIfError(iProdThread.Create(KProducer,ProdThreadFunc,KDefaultStackSize,KMinHeapSize,256*KMinHeapSize,iQueue,EOwnerThread));
00075         iProdThread.SetPriority(EPriorityMore);
00076 
00077         // The producer thread invokes the InsertFunction() function every two seconds.
00078         // Associate InsertFunction() as the call back function for the iPeriodicCons object.
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         // Initialize the array.
00096         // The array reflects the contents of the CQueue queue.
00097         iQueue->GetTokens(array);
00098         // Print the array.
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         // The while loop mechanism ensures that the thread performs the CQueue::Insert() operation each time RThread::Resume() is invoked on the thread handle.
00125         while(true)
00126                 {
00127                 CQueue* ptr = (CQueue*)aPtr;
00128                 __ASSERT_ALWAYS(ptr,User::Panic(KTxtPanic,-1));
00129                 // Insert a token into the queue.
00130                 ptr->Insert();
00131                 // Suspend the thread.
00132                 RThread().Suspend();
00133                 }
00134         return KErrNone;
00135         }
00136 
00140 void CProducer::ResumeProd()
00141         {
00142         // Call the RThread::Resume() function on the producer thread.
00143         // Control goes to the CProducer::ProdThreadFunc() function after this statement.
00144         iProdThread.Resume();
00145         }
00146 
00150 void CProducer::Produce()
00151         {
00152         // Call the CQueue::Insert() function.
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         // Invoke the RThread::Resume() function on the producer thread repeatedly.
00176         ptr->ResumeProd();
00177         return KErrNone;
00178         }

Generated by  doxygen 1.6.2