#include <FifoQueue.h>
Public Member Functions | |
CFifoQueue () | |
virtual | ~CFifoQueue () |
HRESULT | Initialize (int Size, CSemaphore *SemaphoreElement, ofstream *Logstream) |
void | Put (QueueElement *Request) |
bool | Get (QueueElement *Request) |
int | nRequests () |
Private Attributes | |
int | m_Size |
int | m_n |
int | m_First |
int | m_End |
CSemaphore * | m_SemaphorePosition |
CSemaphore * | m_SemaphoreElement |
CMutex | m_Mutex |
QueueElement * | m_Request |
ofstream * | m_log |
Definition at line 43 of file FifoQueue.h.
CFifoQueue::CFifoQueue | ( | ) |
Definition at line 47 of file FifoQueue.cpp.
00048 { 00049 m_Size = 0; 00050 m_n =0 ; 00051 m_First = 0; 00052 m_End = 0; 00053 m_Request = 0; 00054 m_SemaphoreElement = 0; 00055 m_SemaphorePosition = 0; 00056 m_log = 0; 00057 }
CFifoQueue::~CFifoQueue | ( | ) | [virtual] |
Definition at line 59 of file FifoQueue.cpp.
00060 { 00061 if(m_Request != 0) 00062 { 00063 delete[] m_Request; 00064 m_Request = 0; 00065 } 00066 if(m_SemaphorePosition != 0) 00067 { 00068 delete m_SemaphorePosition; 00069 m_SemaphorePosition = 0; 00070 } 00071 }
bool CFifoQueue::Get | ( | QueueElement * | Request | ) |
Definition at line 146 of file FifoQueue.cpp.
00147 { 00148 00149 // Get exclusive access to queue variables 00150 m_Mutex.Wait(); 00151 00152 if(m_n == 0) 00153 { 00154 m_Mutex.Release(); 00155 return false; 00156 } 00157 00158 *Request = m_Request[m_First]; 00159 m_n--; 00160 m_First++; 00161 if(m_First >= m_Size) 00162 m_First = 0; 00163 00164 // Release use of queue 00165 m_Mutex.Release(); 00166 00167 // Signal one more position available on queue 00168 m_SemaphorePosition->V(); 00169 00170 return true; 00171 00172 }
HRESULT CFifoQueue::Initialize | ( | int | Size, | |
CSemaphore * | SemaphoreElement, | |||
ofstream * | Logstream | |||
) |
Definition at line 73 of file FifoQueue.cpp.
00076 { 00077 m_log = LogStream; 00078 00079 // check if mutex was successfully created 00080 if(!m_Mutex.IsOpen()){ 00081 *m_log << "CFifoQueue::Initialize(): Failed to create mutex" << endl; 00082 *m_log << " ERROR: " << strerror(errno) << endl; 00083 return ERROR_NETWORKMANAGER + ERROR_CREATE_MUTEX; 00084 } 00085 00086 // Allocate and create position semaphore 00087 m_SemaphorePosition = new CSemaphore (Size,Size); 00088 if(m_SemaphorePosition == 0) 00089 { 00090 *m_log << "CFifoQueue::Initialize(): Failed to allocate memory" << endl; 00091 return ERROR_NETWORKMANAGER + ERROR_MEMORY; 00092 } 00093 00094 m_SemaphoreElement = SemaphoreElement; 00095 00096 // Check if position semaphore was allocated successfully 00097 if(!m_SemaphorePosition->IsOpen()) 00098 { 00099 *m_log << "CFifoQueue::Initialize(): Failed to create semaphore" << endl; 00100 *m_log << " ERROR: " << strerror(errno) << endl; 00101 delete m_SemaphorePosition; 00102 m_SemaphorePosition = 0; 00103 return ERROR_NETWORKMANAGER + ERROR_CREATE_SEMAPHORE; 00104 } 00105 00106 // Allocate space for queued requests 00107 m_Request = new QueueElement[Size]; 00108 if(m_Request == 0) 00109 { 00110 *m_log << "CFifoQueue::Initialize(): Failed to allocate memory" << endl; 00111 delete m_SemaphorePosition; 00112 m_SemaphorePosition = 0; 00113 return ERROR_NETWORKMANAGER + ERROR_MEMORY; 00114 } 00115 00116 m_Size = Size; 00117 m_n = m_First = m_End = 0; 00118 00119 return S_OK; 00120 00121 }
int CFifoQueue::nRequests | ( | ) |
void CFifoQueue::Put | ( | QueueElement * | Request | ) |
Definition at line 123 of file FifoQueue.cpp.
00124 { 00125 // Wait for avaiable position on queue 00126 m_SemaphorePosition->P(); 00127 00128 // Get exclusive access to queue variables 00129 m_Mutex.Wait(); 00130 00131 // Put Request at end of queue 00132 m_Request[m_End] = *Request; 00133 m_End++; 00134 m_n++; 00135 if(m_End >= m_Size) 00136 m_End = 0; 00137 00138 // Release use of queue 00139 m_Mutex.Release(); 00140 00141 // Signal one more element available on queue 00142 m_SemaphoreElement->V(); 00143 00144 }
int CFifoQueue::m_End [private] |
Definition at line 58 of file FifoQueue.h.
int CFifoQueue::m_First [private] |
Definition at line 57 of file FifoQueue.h.
ofstream* CFifoQueue::m_log [private] |
Definition at line 63 of file FifoQueue.h.
CMutex CFifoQueue::m_Mutex [private] |
Definition at line 61 of file FifoQueue.h.
int CFifoQueue::m_n [private] |
Definition at line 56 of file FifoQueue.h.
QueueElement* CFifoQueue::m_Request [private] |
Definition at line 62 of file FifoQueue.h.
CSemaphore* CFifoQueue::m_SemaphoreElement [private] |
Definition at line 60 of file FifoQueue.h.
CSemaphore* CFifoQueue::m_SemaphorePosition [private] |
Definition at line 59 of file FifoQueue.h.
int CFifoQueue::m_Size [private] |
Definition at line 55 of file FifoQueue.h.