[PATCH 14/14] Use simpler classes for mutex handling

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Frediano Ziglio <fziglio@xxxxxxxxxx>
---
 common/vdcommon.h          | 39 +++++++++++++++++++++++++++++++++++----
 vdagent/desktop_layout.cpp |  1 -
 vdagent/desktop_layout.h   |  4 ++--
 vdagent/vdagent.cpp        | 17 +++++------------
 vdservice/vdservice.cpp    | 11 +++--------
 5 files changed, 45 insertions(+), 27 deletions(-)

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;
 }
 
-- 
2.7.4

_______________________________________________
Spice-devel mailing list
Spice-devel@xxxxxxxxxxxxxxxxxxxxx
https://lists.freedesktop.org/mailman/listinfo/spice-devel




[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]     [Monitors]