• Main Page
  • Classes
  • Files
  • File List

CSimpleEventQueueBuffer.cpp

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2012 Bolton Software Ltd.
00004  * Copyright (C) 2004 Chris Schoeneman
00005  * 
00006  * This package is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * found in the file COPYING that should have accompanied this file.
00009  * 
00010  * This package is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include "CSimpleEventQueueBuffer.h"
00020 #include "CStopwatch.h"
00021 #include "CArch.h"
00022 
00023 //
00024 // CSimpleEventQueueBuffer
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 }

Generated on Thu Jun 20 2013 00:00:05 for Synergy by  doxygen 1.7.1