00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "CSimpleEventQueueBuffer.h"
00020 #include "CStopwatch.h"
00021 #include "CArch.h"
00022
00023
00024
00025
00026
00027 CSimpleEventQueueBuffer::CSimpleEventQueueBuffer()
00028 {
00029 m_queueMutex = ARCH->newMutex();
00030 m_queueReadyCond = ARCH->newCondVar();
00031 m_queueReady = false;
00032 }
00033
00034 CSimpleEventQueueBuffer::~CSimpleEventQueueBuffer()
00035 {
00036 ARCH->closeCondVar(m_queueReadyCond);
00037 ARCH->closeMutex(m_queueMutex);
00038 }
00039
00040 void
00041 CSimpleEventQueueBuffer::waitForEvent(double timeout)
00042 {
00043 CArchMutexLock lock(m_queueMutex);
00044 CStopwatch timer(true);
00045 while (!m_queueReady) {
00046 double timeLeft = timeout;
00047 if (timeLeft >= 0.0) {
00048 timeLeft -= timer.getTime();
00049 if (timeLeft < 0.0) {
00050 return;
00051 }
00052 }
00053 ARCH->waitCondVar(m_queueReadyCond, m_queueMutex, timeLeft);
00054 }
00055 }
00056
00057 IEventQueueBuffer::Type
00058 CSimpleEventQueueBuffer::getEvent(CEvent&, UInt32& dataID)
00059 {
00060 CArchMutexLock lock(m_queueMutex);
00061 if (!m_queueReady) {
00062 return kNone;
00063 }
00064 dataID = m_queue.back();
00065 m_queue.pop_back();
00066 m_queueReady = !m_queue.empty();
00067 return kUser;
00068 }
00069
00070 bool
00071 CSimpleEventQueueBuffer::addEvent(UInt32 dataID)
00072 {
00073 CArchMutexLock lock(m_queueMutex);
00074 m_queue.push_front(dataID);
00075 if (!m_queueReady) {
00076 m_queueReady = true;
00077 ARCH->broadcastCondVar(m_queueReadyCond);
00078 }
00079 return true;
00080 }
00081
00082 bool
00083 CSimpleEventQueueBuffer::isEmpty() const
00084 {
00085 CArchMutexLock lock(m_queueMutex);
00086 return !m_queueReady;
00087 }
00088
00089 CEventQueueTimer*
00090 CSimpleEventQueueBuffer::newTimer(double, bool) const
00091 {
00092 return new CEventQueueTimer;
00093 }
00094
00095 void
00096 CSimpleEventQueueBuffer::deleteTimer(CEventQueueTimer* timer) const
00097 {
00098 delete timer;
00099 }