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 CQueue class. 00028 */ 00029 00030 00031 00036 #include "queue.h" 00037 00042 CQueue* CQueue::NewL() 00043 { 00044 CQueue* self = new (ELeave)CQueue; 00045 CleanupStack::PushL(self); 00046 self->ConstructL(); 00047 CleanupStack::Pop(self); 00048 return self; 00049 } 00050 00056 void CQueue::ConstructL() 00057 { 00058 // Create the local RMutex variable. 00059 User::LeaveIfError(iMutex.CreateLocal()); 00060 // Create the local RCondVar variable. 00061 User::LeaveIfError(iCondVar.CreateLocal()); 00062 } 00063 00067 CQueue::CQueue() 00068 { 00069 } 00070 00075 void CQueue::Insert() 00076 { 00077 // Wait for the mutex. 00078 // The mutex is required to ensure that only one thread updates iArray at a time. 00079 iMutex.Wait(); 00080 // Insertion operation. 00081 // Insert some random number as a token into the queue. 00082 iArray.Append(Math::Random()%10); 00083 // Signal the local condition variable to indicate that queue has new data. 00084 iCondVar.Signal(); 00085 // release the mutex so that the other thread has access to iArray. 00086 iMutex.Signal(); 00087 // RCondVar::Broadcast() can be used as an alternative to the RCondVar::Signal() function. 00088 // It is more relevant to use Broadcast() when more than two threads are blocking on single condition variable. 00089 // The following code block illustrates the use of the Broadcast() function. 00090 // 00091 //if(!IsEmpty()) 00092 // { 00093 // iCondVar.Broadcast(); 00094 // iMutex.Signal(); 00095 // } 00096 // 00097 } 00098 00103 void CQueue::Remove() 00104 { 00105 // Ensures that mutex is held by the current thread. 00106 iMutex.Wait(); 00107 // Test to see whether there is anything in the queue before attempting to remove an item. 00108 // If the queue is empty wait for a signal on the Condition Variable before proceeding 00109 while(!IsEmpty()) 00110 { 00111 // The following call blocks this thread and releases the mutex so that the other thread can update the shared data. 00112 // When the other thread signals iCondVar and releases the mutex this thread continues. 00113 iCondVar.Wait(iMutex); 00114 // If there were more than one consumer waiting for the same signal 00115 // it would be essential to test IsEmpty() again because another thread, or threads, 00116 // might have got the mutex before this one and emptied the array. 00117 // For this reason if is good practise to use while( IsEmpty() ) instead of if( IsEmpty() ). 00118 } 00119 // Deletion operation. 00120 iArray.Remove(0); 00121 // Release the mutex. 00122 iMutex.Signal(); 00123 } 00124 00129 TBool CQueue::IsEmpty() 00130 { 00131 if(iArray.Count() > 0) 00132 { 00133 return ETrue; 00134 } 00135 return EFalse; 00136 } 00137 00142 void CQueue::GetTokens(RArray<TInt>& aArray) 00143 { 00144 for(TInt ix = 0; ix < iArray.Count(); ix++) 00145 { 00146 aArray.Append(iArray[ix]); 00147 } 00148 } 00149 00153 CQueue::~CQueue() 00154 { 00155 iMutex.Close(); 00156 iCondVar.Close(); 00157 }