source: vendor/synergy/current/lib/base/CSimpleEventQueueBuffer.cpp

Last change on this file was 2749, checked in by bird, 19 years ago

synergy v1.3.1 sources (zip).

File size: 2.1 KB
Line 
1/*
2 * synergy -- mouse and keyboard sharing utility
3 * Copyright (C) 2004 Chris Schoeneman
4 *
5 * This package is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * found in the file COPYING that should have accompanied this file.
8 *
9 * This package is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#include "CSimpleEventQueueBuffer.h"
16#include "CStopwatch.h"
17#include "CArch.h"
18
19class CEventQueueTimer { };
20
21//
22// CSimpleEventQueueBuffer
23//
24
25CSimpleEventQueueBuffer::CSimpleEventQueueBuffer()
26{
27 m_queueMutex = ARCH->newMutex();
28 m_queueReadyCond = ARCH->newCondVar();
29 m_queueReady = false;
30}
31
32CSimpleEventQueueBuffer::~CSimpleEventQueueBuffer()
33{
34 ARCH->closeCondVar(m_queueReadyCond);
35 ARCH->closeMutex(m_queueMutex);
36}
37
38void
39CSimpleEventQueueBuffer::waitForEvent(double timeout)
40{
41 CArchMutexLock lock(m_queueMutex);
42 CStopwatch timer(true);
43 while (!m_queueReady) {
44 double timeLeft = timeout;
45 if (timeLeft >= 0.0) {
46 timeLeft -= timer.getTime();
47 if (timeLeft < 0.0) {
48 return;
49 }
50 }
51 ARCH->waitCondVar(m_queueReadyCond, m_queueMutex, timeLeft);
52 }
53}
54
55IEventQueueBuffer::Type
56CSimpleEventQueueBuffer::getEvent(CEvent&, UInt32& dataID)
57{
58 CArchMutexLock lock(m_queueMutex);
59 if (!m_queueReady) {
60 return kNone;
61 }
62 dataID = m_queue.back();
63 m_queue.pop_back();
64 m_queueReady = !m_queue.empty();
65 return kUser;
66}
67
68bool
69CSimpleEventQueueBuffer::addEvent(UInt32 dataID)
70{
71 CArchMutexLock lock(m_queueMutex);
72 m_queue.push_front(dataID);
73 if (!m_queueReady) {
74 m_queueReady = true;
75 ARCH->broadcastCondVar(m_queueReadyCond);
76 }
77 return true;
78}
79
80bool
81CSimpleEventQueueBuffer::isEmpty() const
82{
83 CArchMutexLock lock(m_queueMutex);
84 return !m_queueReady;
85}
86
87CEventQueueTimer*
88CSimpleEventQueueBuffer::newTimer(double, bool) const
89{
90 return new CEventQueueTimer;
91}
92
93void
94CSimpleEventQueueBuffer::deleteTimer(CEventQueueTimer* timer) const
95{
96 delete timer;
97}
Note: See TracBrowser for help on using the repository browser.