examples/Base/IPC/condvar/condvarlocal/src/consumer.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 CConsumer class.  
00028 */
00029 
00030 
00031 
00036 #include "consumer.h"
00037 
00044 CConsumer* CConsumer::NewL(CConsoleBase* aConsole,CQueue* aTokens)
00045         {
00046         CConsumer* self = new (ELeave)CConsumer;
00047         CleanupStack::PushL(self);
00048         self->ConstructL(aConsole,aTokens);
00049         CleanupStack::Pop(self);
00050         return self;
00051         }
00052 
00064 void CConsumer::ConstructL(CConsoleBase* aConsole,CQueue* aTokens)
00065         {
00066         iConsole = aConsole;
00067         iQueue = aTokens;
00068 
00069         // Create an object of the CPeriodic class.
00070         iPeriodicCons = CPeriodic::NewL(CActive::EPriorityUserInput);
00071 
00072         // Create the Consumer thread.
00073         _LIT(KConsumer,"ConsumerThread");
00074         User::LeaveIfError(iConsThread.Create(KConsumer,ConsThreadFunc,KDefaultStackSize,KMinHeapSize,256*KMinHeapSize,iQueue,EOwnerThread));
00075         iConsThread.SetPriority(EPriorityMore);
00076 
00077         // Make the Consumer thread more intensive than the Producer thread.
00078         // Consumer thread frequency is twice the Producer thread frequency.
00079         // Associate RemoveFunction() as the call back function for the iPeriodicCons object.
00080         iPeriodicCons->Start(2000000,2000000,TCallBack(RemoveFunction,this));
00081         }
00082 
00086 CConsumer::CConsumer()
00087         {
00088         }
00089 
00095 TInt CConsumer::ConsThreadFunc(TAny* aPtr)
00096         {
00097         // The while loop mechanism ensures that the thread performs the CQueue::Remove() operation each time RThread::Resume() is invoked on the thread handle.
00098         while(true)
00099                 {
00100                 CQueue* ptr = (CQueue*)aPtr;
00101                 __ASSERT_ALWAYS(ptr,User::Panic(KTxtPanic,-1));
00102                 // Remove a token from the queue.
00103                 ptr->Remove();
00104                 // Suspend the thread.
00105                 RThread().Suspend();
00106                 }
00107         return KErrNone;
00108         }
00109 
00110 
00114 void CConsumer::ResumeCons()
00115         {
00116         // Call the RThread::Resume() function on the consumer thread.
00117         // Control goes to the CConsumer::ConsThreadFunc() function after this statement.
00118         iConsThread.Resume();
00119         }
00120 
00124 CConsumer::~CConsumer()
00125         {
00126         iConsThread.Kill(KErrCancel);
00127         delete iPeriodicCons;
00128         }
00129 
00130 
00136 TInt CConsumer::RemoveFunction(TAny* aPtr)
00137         {
00138         CConsumer* ptr = static_cast<CConsumer*> (aPtr);
00139         __ASSERT_ALWAYS(ptr,User::Panic(KTxtPanic,-1));
00140         // Invoke the RThread::Resume() function on the consumer thread repeatedly.
00141         ptr->ResumeCons();
00142         return KErrNone;
00143         }

Generated by  doxygen 1.6.2