[Spice-commits] 2 commits - common/vdcommon.h vdagent/desktop_layout.cpp vdagent/desktop_layout.h vdagent/vdagent.cpp vdservice/vdservice.cpp
Frediano Ziglio
fziglio at kemper.freedesktop.org
Thu Sep 1 14:51:25 UTC 2016
common/vdcommon.h | 39 ++++++++++++++++++++++++++++++++----
vdagent/desktop_layout.cpp | 1
vdagent/desktop_layout.h | 4 +--
vdagent/vdagent.cpp | 48 +++++++++++++++------------------------------
vdservice/vdservice.cpp | 11 ++--------
5 files changed, 56 insertions(+), 47 deletions(-)
New commits:
commit 75a0033f42f64914cbd063c1fb8ac0c0f28a2095
Author: Frediano Ziglio <fziglio at redhat.com>
Date: Fri Aug 12 15:31:33 2016 +0100
Use simpler classes for mutex handling
Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
Acked-by: Christophe Fergeau <cfergeau at redhat.com>
diff --git a/common/vdcommon.h b/common/vdcommon.h
index f4859e2..c1920e9 100644
--- a/common/vdcommon.h
+++ b/common/vdcommon.h
@@ -27,11 +27,42 @@
#include "spice/vd_agent.h"
#include "vdlog.h"
-typedef CRITICAL_SECTION mutex_t;
+class Mutex {
+public:
+ Mutex() {
+ InitializeCriticalSection(&_crit);
+ }
+ ~Mutex() {
+ DeleteCriticalSection(&_crit);
+ }
+ void lock() {
+ EnterCriticalSection(&_crit);
+ }
+ void unlock() {
+ LeaveCriticalSection(&_crit);
+ }
+private:
+ CRITICAL_SECTION _crit;
+ // no copy
+ Mutex(const Mutex&);
+ void operator=(const Mutex&);
+};
-#define MUTEX_INIT(mutex) InitializeCriticalSection(&mutex)
-#define MUTEX_LOCK(mutex) EnterCriticalSection(&mutex)
-#define MUTEX_UNLOCK(mutex) LeaveCriticalSection(&mutex)
+class MutexLocker {
+public:
+ MutexLocker(Mutex &mtx):_mtx(mtx) {
+ _mtx.lock();
+ }
+ ~MutexLocker() {
+ _mtx.unlock();
+ }
+private:
+ Mutex &_mtx;
+ // no copy
+ MutexLocker(const MutexLocker&);
+ void operator=(const MutexLocker&);
+};
+typedef Mutex mutex_t;
#define VD_AGENT_REGISTRY_KEY "SOFTWARE\\Red Hat\\Spice\\vdagent\\"
#define VD_AGENT_STOP_EVENT TEXT("Global\\vdagent_stop_event")
diff --git a/vdagent/desktop_layout.cpp b/vdagent/desktop_layout.cpp
index 65f06c1..11ad009 100644
--- a/vdagent/desktop_layout.cpp
+++ b/vdagent/desktop_layout.cpp
@@ -38,7 +38,6 @@ DesktopLayout::DesktopLayout()
, _total_height (0)
, _display_config (NULL)
{
- MUTEX_INIT(_mutex);
_display_config = DisplayConfig::create_config();
get_displays();
}
diff --git a/vdagent/desktop_layout.h b/vdagent/desktop_layout.h
index 34bec0d..690b34f 100644
--- a/vdagent/desktop_layout.h
+++ b/vdagent/desktop_layout.h
@@ -68,8 +68,8 @@ public:
~DesktopLayout();
void get_displays();
void set_displays();
- void lock() { MUTEX_LOCK(_mutex);}
- void unlock() { MUTEX_UNLOCK(_mutex);}
+ void lock() { _mutex.lock(); }
+ void unlock() { _mutex.unlock(); }
DisplayMode* get_display(int i) { return _displays.at(i);}
size_t get_display_count() { return _displays.size();}
DWORD get_total_width() { return _total_width;}
diff --git a/vdagent/vdagent.cpp b/vdagent/vdagent.cpp
index 45f2c68..e3ba14b 100644
--- a/vdagent/vdagent.cpp
+++ b/vdagent/vdagent.cpp
@@ -235,8 +235,6 @@ VDAgent::VDAgent()
ZeroMemory(&_read_overlapped, sizeof(_read_overlapped));
ZeroMemory(&_write_overlapped, sizeof(_write_overlapped));
ZeroMemory(_read_buf, sizeof(_read_buf));
- MUTEX_INIT(_control_mutex);
- MUTEX_INIT(_message_mutex);
_singleton = this;
}
@@ -366,17 +364,16 @@ void VDAgent::cleanup()
void VDAgent::set_control_event(int control_command)
{
- MUTEX_LOCK(_control_mutex);
+ MutexLocker lock(_control_mutex);
_control_queue.push(control_command);
if (_control_event && !SetEvent(_control_event)) {
vd_printf("SetEvent() failed: %lu", GetLastError());
}
- MUTEX_UNLOCK(_control_mutex);
}
void VDAgent::handle_control_event()
{
- MUTEX_LOCK(_control_mutex);
+ MutexLocker lock(_control_mutex);
while (_control_queue.size()) {
int control_command = _control_queue.front();
_control_queue.pop();
@@ -406,7 +403,6 @@ void VDAgent::handle_control_event()
vd_printf("Unsupported control command %u", control_command);
}
}
- MUTEX_UNLOCK(_control_mutex);
}
void VDAgent::input_desktop_message_loop()
@@ -936,7 +932,7 @@ bool VDAgent::write_clipboard(VDAgentMessage* msg, uint32_t size)
ASSERT(msg && size);
//FIXME: do it smarter - no loop, no memcopy
- MUTEX_LOCK(_message_mutex);
+ MutexLocker lock(_message_mutex);
while (pos < size) {
DWORD n = MIN(sizeof(VDIChunk) + size - pos, VD_AGENT_MAX_DATA_SIZE);
VDIChunk* chunk = new_chunk(n);
@@ -950,7 +946,6 @@ bool VDAgent::write_clipboard(VDAgentMessage* msg, uint32_t size)
enqueue_chunk(chunk);
pos += (n - sizeof(VDIChunk));
}
- MUTEX_UNLOCK(_message_mutex);
return ret;
}
@@ -1438,7 +1433,7 @@ void VDAgent::write_completion(DWORD err, DWORD bytes, LPOVERLAPPED overlapped)
a->_running = false;
return;
}
- MUTEX_LOCK(a->_message_mutex);
+ MutexLocker lock(a->_message_mutex);
a->_write_pos += bytes;
chunk = a->_message_queue.front();
count = sizeof(VDIChunk) + chunk->hdr.size - a->_write_pos;
@@ -1458,7 +1453,6 @@ void VDAgent::write_completion(DWORD err, DWORD bytes, LPOVERLAPPED overlapped)
a->_running = false;
}
}
- MUTEX_UNLOCK(a->_message_mutex);
}
VDIChunk* VDAgent::new_chunk(DWORD bytes)
@@ -1468,12 +1462,11 @@ VDIChunk* VDAgent::new_chunk(DWORD bytes)
void VDAgent::enqueue_chunk(VDIChunk* chunk)
{
- MUTEX_LOCK(_message_mutex);
+ MutexLocker lock(_message_mutex);
_message_queue.push(chunk);
if (_message_queue.size() == 1) {
write_completion(0, 0, &_write_overlapped);
}
- MUTEX_UNLOCK(_message_mutex);
}
LRESULT CALLBACK VDAgent::wnd_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
diff --git a/vdservice/vdservice.cpp b/vdservice/vdservice.cpp
index 2d7b5b9..1892b72 100644
--- a/vdservice/vdservice.cpp
+++ b/vdservice/vdservice.cpp
@@ -124,8 +124,6 @@ VDService::VDService()
_control_event = CreateEvent(NULL, FALSE, FALSE, NULL);
_agent_stop_event = CreateEvent(NULL, FALSE, FALSE, VD_AGENT_STOP_EVENT);
_agent_path[0] = wchar_t('\0');
- MUTEX_INIT(_agent_mutex);
- MUTEX_INIT(_control_mutex);
}
VDService::~VDService()
@@ -240,17 +238,16 @@ static const char* const session_events[] = {
void VDService::set_control_event(int control_command)
{
- MUTEX_LOCK(_control_mutex);
+ MutexLocker lock(_control_mutex);
_control_queue.push(control_command);
if (_control_event && !SetEvent(_control_event)) {
vd_printf("SetEvent() failed: %lu", GetLastError());
}
- MUTEX_UNLOCK(_control_mutex);
}
void VDService::handle_control_event()
{
- MUTEX_LOCK(_control_mutex);
+ MutexLocker lock(_control_mutex);
while (_control_queue.size()) {
int control_command = _control_queue.front();
_control_queue.pop();
@@ -265,7 +262,6 @@ void VDService::handle_control_event()
vd_printf("Unsupported control command %u", control_command);
}
}
- MUTEX_UNLOCK(_control_mutex);
}
DWORD WINAPI VDService::control_handler(DWORD control, DWORD event_type, LPVOID event_data,
@@ -781,7 +777,7 @@ bool VDService::restart_agent(bool normal_restart)
DWORD time = GetTickCount();
bool ret = true;
- MUTEX_LOCK(_agent_mutex);
+ MutexLocker lock(_agent_mutex);
if (!normal_restart && ++_agent_restarts > VD_AGENT_MAX_RESTARTS) {
vd_printf("Agent restarted too many times");
ret = false;
@@ -794,7 +790,6 @@ bool VDService::restart_agent(bool normal_restart)
_last_agent_restart_time = time;
ret = true;
}
- MUTEX_UNLOCK(_agent_mutex);
return ret;
}
commit ec0a2352b18f3069cb4cb31f05c28527dea5d6a0
Author: Frediano Ziglio <fziglio at redhat.com>
Date: Fri Aug 12 15:19:45 2016 +0100
Use std::vector for agent capabilities
Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
Acked-by: Christophe Fergeau <cfergeau at redhat.com>
diff --git a/vdagent/vdagent.cpp b/vdagent/vdagent.cpp
index d4f0d45..45f2c68 100644
--- a/vdagent/vdagent.cpp
+++ b/vdagent/vdagent.cpp
@@ -27,6 +27,7 @@
#include <lmcons.h>
#include <queue>
#include <set>
+#include <vector>
#define VD_AGENT_LOG_PATH TEXT("%svdagent.log")
#define VD_AGENT_WINCLASS_NAME TEXT("VDAGENT")
@@ -125,6 +126,10 @@ private:
bool send_announce_capabilities(bool request);
void cleanup_in_msg();
void cleanup();
+ bool has_capability(unsigned int capability) const {
+ return VD_AGENT_HAS_CAPABILITY(_client_caps.begin(), _client_caps.size(),
+ capability);
+ }
private:
static VDAgent* _singleton;
@@ -169,8 +174,7 @@ private:
bool _logon_occured;
int32_t _max_clipboard;
- uint32_t *_client_caps;
- uint32_t _client_caps_size;
+ std::vector<uint32_t> _client_caps;
std::set<uint32_t> _grab_types;
@@ -217,8 +221,6 @@ VDAgent::VDAgent()
, _logon_desktop (false)
, _display_setting_initialized (false)
, _max_clipboard (-1)
- , _client_caps (NULL)
- , _client_caps_size (0)
, _log (NULL)
{
TCHAR log_path[MAX_PATH];
@@ -242,7 +244,6 @@ VDAgent::VDAgent()
VDAgent::~VDAgent()
{
delete _log;
- delete[] _client_caps;
}
DWORD WINAPI VDAgent::event_thread_proc(LPVOID param)
@@ -861,16 +862,9 @@ bool VDAgent::handle_announce_capabilities(VDAgentAnnounceCapabilities* announce
for (uint32_t i = 0 ; i < caps_size; ++i) {
vd_printf("%X", announce_capabilities->caps[i]);
}
- if (caps_size != _client_caps_size) {
- delete[] _client_caps;
- _client_caps = new uint32_t[caps_size];
- ASSERT(_client_caps != NULL);
- _client_caps_size = caps_size;
- }
- memcpy(_client_caps, announce_capabilities->caps, sizeof(_client_caps[0]) * caps_size);
+ _client_caps.assign(announce_capabilities->caps, announce_capabilities->caps + caps_size);
- if (VD_AGENT_HAS_CAPABILITY(_client_caps, _client_caps_size,
- VD_AGENT_CAP_MONITORS_CONFIG_POSITION))
+ if (has_capability(VD_AGENT_CAP_MONITORS_CONFIG_POSITION))
_desktop_layout->set_position_configurable(true);
if (announce_capabilities->request) {
return send_announce_capabilities(false);
@@ -988,8 +982,7 @@ void VDAgent::on_clipboard_grab()
uint32_t types[clipboard_formats_count * VD_CLIPBOARD_FORMAT_MAX_TYPES];
int count = 0;
- if (!VD_AGENT_HAS_CAPABILITY(_client_caps, _client_caps_size,
- VD_AGENT_CAP_CLIPBOARD_BY_DEMAND)) {
+ if (!has_capability(VD_AGENT_CAP_CLIPBOARD_BY_DEMAND)) {
return;
}
if (CountClipboardFormats() == 0) {
@@ -1033,8 +1026,7 @@ void VDAgent::on_clipboard_request(UINT format)
vd_printf("Unsupported clipboard format %u", format);
return;
}
- if (!VD_AGENT_HAS_CAPABILITY(_client_caps, _client_caps_size,
- VD_AGENT_CAP_CLIPBOARD_BY_DEMAND)) {
+ if (!has_capability(VD_AGENT_CAP_CLIPBOARD_BY_DEMAND)) {
return;
}
@@ -1060,8 +1052,7 @@ void VDAgent::on_clipboard_request(UINT format)
void VDAgent::on_clipboard_release()
{
- if (!VD_AGENT_HAS_CAPABILITY(_client_caps, _client_caps_size,
- VD_AGENT_CAP_CLIPBOARD_BY_DEMAND)) {
+ if (!has_capability(VD_AGENT_CAP_CLIPBOARD_BY_DEMAND)) {
return;
}
if (_clipboard_owner == owner_guest) {
More information about the Spice-commits
mailing list