00001 /* 00002 * Copyright (C) 2006, Jose Renato Santos. 00003 * 00004 * This file may be distributed under the terms of the Q Public License 00005 * as defined by Trolltech AS of Norway and appearing in the file 00006 * LICENSE.QPL included in the packaging of this file. 00007 * 00008 * THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING 00009 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00010 * PURPOSE. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, 00011 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING 00012 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 00013 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 00014 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00015 * 00016 */ 00017 00018 /////////////////////////////////////////////////////////////////////////////// 00019 // msgqueue.cpp -- singely linked fifo queue for router messages 00020 /////////////////////////////////////////////////////////////////////////////// 00021 00022 00023 #include <msgqueue.h> 00024 #include <sserror.h> 00025 00026 /////////////////////////////// CMsgQueue /////////////////////////////////// 00027 00028 /////////////////////////////////////////////////////////////////////////////// 00029 // Put -- Put Msg at end of queue 00030 void CMsgQueue::Put(StrMsg* Msg) 00031 { 00032 m_Queue.Put( (RioQueueEle*) Msg); 00033 } 00034 00035 /////////////////////////////////////////////////////////////////////////////// 00036 // Get -- Get first element of queue. Block calling thread if none 00037 // available (Requires single thread using Get() ) 00038 StrMsg *CMsgQueue::Get() 00039 { 00040 return (StrMsg*) m_Queue.Get(); 00041 } 00042 00043 /////////////////////////////////////////////////////////////////////////////// 00044 // Signal -- Force a thread blocked on Get() to unblock if there is 00045 // such a thread. Get() will return 0 in this case 00046 void CMsgQueue::Signal() 00047 { 00048 m_Queue.Signal(); 00049 } 00050 00051 //////////////////////////// CMsgManager ///////////////////////////////////// 00052 00053 /////////////////////////////////////////////////////////////////////////////// 00054 CMsgManager::CMsgManager() 00055 { 00056 m_Msg = 0; 00057 } 00058 00059 CMsgManager::~CMsgManager() 00060 { 00061 if(m_Msg != 0) 00062 { 00063 delete[] m_Msg; 00064 } 00065 } 00066 00067 /////////////////////////////////////////////////////////////////////////////// 00068 int CMsgManager::Initialize(const unsigned int nMsg) 00069 { 00070 // In case was initialized before 00071 if(m_Msg != 0) 00072 { 00073 delete[] m_Msg; 00074 } 00075 00076 m_Msg = new StrMsg[nMsg]; 00077 00078 if(m_Msg == 0) 00079 return ERROR_SS_MEMORY; 00080 00081 for(int i= ((int)(nMsg))-1; i>=0; i--) 00082 { 00083 m_FreeList.Free((RioQueueEle*)&m_Msg[i]); 00084 } 00085 00086 return (0); 00087 00088 } 00089 00090 /////////////////////////////////////////////////////////////////////////////// 00091 // Free --Return Msg to free list 00092 void CMsgManager::Free(StrMsg* Msg) 00093 { 00094 m_FreeList.Free( (RioQueueEle*)Msg); 00095 } 00096 00097 /////////////////////////////////////////////////////////////////////////////// 00098 // New -- Get new Msg structure from free list if available, otherwise 00099 // return null 00100 StrMsg* CMsgManager::New() 00101 { 00102 return (StrMsg*) m_FreeList.Get(); 00103 } 00104