deskflow/src/lib/base/SimpleEventQueueBuffer.cpp

100 lines
2.2 KiB
C++
Raw Normal View History

2012-06-10 16:50:54 +00:00
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2012 Synergy Si Ltd.
* Copyright (C) 2004 Chris Schoeneman
2012-06-10 16:50:54 +00:00
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file LICENSE that should have accompanied this file.
2012-06-10 16:50:54 +00:00
*
* This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "base/SimpleEventQueueBuffer.h"
#include "base/Stopwatch.h"
#include "arch/Arch.h"
2012-06-10 16:50:54 +00:00
//
2014-11-11 13:51:47 +00:00
// SimpleEventQueueBuffer
2012-06-10 16:50:54 +00:00
//
2014-11-11 13:51:47 +00:00
SimpleEventQueueBuffer::SimpleEventQueueBuffer()
2012-06-10 16:50:54 +00:00
{
m_queueMutex = ARCH->newMutex();
m_queueReadyCond = ARCH->newCondVar();
m_queueReady = false;
}
2014-11-11 13:51:47 +00:00
SimpleEventQueueBuffer::~SimpleEventQueueBuffer()
2012-06-10 16:50:54 +00:00
{
ARCH->closeCondVar(m_queueReadyCond);
ARCH->closeMutex(m_queueMutex);
}
void
2014-11-11 13:51:47 +00:00
SimpleEventQueueBuffer::waitForEvent(double timeout)
2012-06-10 16:50:54 +00:00
{
2014-11-11 13:51:47 +00:00
ArchMutexLock lock(m_queueMutex);
Stopwatch timer(true);
2012-06-10 16:50:54 +00:00
while (!m_queueReady) {
double timeLeft = timeout;
if (timeLeft >= 0.0) {
timeLeft -= timer.getTime();
if (timeLeft < 0.0) {
return;
}
}
ARCH->waitCondVar(m_queueReadyCond, m_queueMutex, timeLeft);
}
}
IEventQueueBuffer::Type
2014-11-11 13:51:47 +00:00
SimpleEventQueueBuffer::getEvent(Event&, UInt32& dataID)
2012-06-10 16:50:54 +00:00
{
2014-11-11 13:51:47 +00:00
ArchMutexLock lock(m_queueMutex);
2012-06-10 16:50:54 +00:00
if (!m_queueReady) {
return kNone;
}
dataID = m_queue.back();
m_queue.pop_back();
m_queueReady = !m_queue.empty();
return kUser;
}
bool
2014-11-11 13:51:47 +00:00
SimpleEventQueueBuffer::addEvent(UInt32 dataID)
2012-06-10 16:50:54 +00:00
{
2014-11-11 13:51:47 +00:00
ArchMutexLock lock(m_queueMutex);
2012-06-10 16:50:54 +00:00
m_queue.push_front(dataID);
if (!m_queueReady) {
m_queueReady = true;
ARCH->broadcastCondVar(m_queueReadyCond);
}
return true;
}
bool
2014-11-11 13:51:47 +00:00
SimpleEventQueueBuffer::isEmpty() const
2012-06-10 16:50:54 +00:00
{
2014-11-11 13:51:47 +00:00
ArchMutexLock lock(m_queueMutex);
2012-06-10 16:50:54 +00:00
return !m_queueReady;
}
2014-11-11 13:51:47 +00:00
EventQueueTimer*
SimpleEventQueueBuffer::newTimer(double, bool) const
2012-06-10 16:50:54 +00:00
{
2014-11-11 13:51:47 +00:00
return new EventQueueTimer;
2012-06-10 16:50:54 +00:00
}
void
2014-11-11 13:51:47 +00:00
SimpleEventQueueBuffer::deleteTimer(EventQueueTimer* timer) const
2012-06-10 16:50:54 +00:00
{
delete timer;
}