From e9dd59bdd928430c25929249c4a48186168e5e36 Mon Sep 17 00:00:00 2001 From: Roger Hughston Date: Mon, 5 Nov 2007 22:57:51 +0000 Subject: [PATCH] Added Thread Safty --- panda/src/display/graphicsWindowInputDevice.I | 11 ++++++ .../src/display/graphicsWindowInputDevice.cxx | 35 ++++++++++--------- panda/src/display/graphicsWindowInputDevice.h | 10 ++++-- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/panda/src/display/graphicsWindowInputDevice.I b/panda/src/display/graphicsWindowInputDevice.I index d6edb09408..9a15978aba 100644 --- a/panda/src/display/graphicsWindowInputDevice.I +++ b/panda/src/display/graphicsWindowInputDevice.I @@ -23,6 +23,7 @@ //////////////////////////////////////////////////////////////////// INLINE GraphicsWindowInputDevice:: GraphicsWindowInputDevice() { + MutexHolder holder(_lock); _flags = 0; } @@ -33,6 +34,7 @@ GraphicsWindowInputDevice() { //////////////////////////////////////////////////////////////////// INLINE string GraphicsWindowInputDevice:: get_name() const { + MutexHolder holder(_lock); return _name; } @@ -43,6 +45,7 @@ get_name() const { //////////////////////////////////////////////////////////////////// INLINE bool GraphicsWindowInputDevice:: has_pointer() const { + MutexHolder holder(_lock); return ((_flags & IDF_has_pointer) != 0); } @@ -53,6 +56,7 @@ has_pointer() const { //////////////////////////////////////////////////////////////////// INLINE bool GraphicsWindowInputDevice:: has_keyboard() const { + MutexHolder holder(_lock); return ((_flags & IDF_has_keyboard) != 0); } @@ -64,6 +68,7 @@ has_keyboard() const { //////////////////////////////////////////////////////////////////// INLINE const MouseData &GraphicsWindowInputDevice:: get_pointer() const { + MutexHolder holder(_lock); return _mouse_data; } @@ -76,6 +81,7 @@ get_pointer() const { //////////////////////////////////////////////////////////////////// INLINE const MouseData &GraphicsWindowInputDevice:: get_raw_pointer() const { + MutexHolder holder(_lock); return _true_mouse_data; } @@ -88,6 +94,7 @@ get_raw_pointer() const { //////////////////////////////////////////////////////////////////// INLINE void GraphicsWindowInputDevice:: set_device_index(int index) { + MutexHolder holder(_lock); _device_index = index; } @@ -98,6 +105,7 @@ set_device_index(int index) { //////////////////////////////////////////////////////////////////// INLINE void GraphicsWindowInputDevice:: enable_pointer_events() { + MutexHolder holder(_lock); _enable_pointer_events = true; } @@ -108,6 +116,7 @@ enable_pointer_events() { //////////////////////////////////////////////////////////////////// INLINE void GraphicsWindowInputDevice:: disable_pointer_events() { + MutexHolder holder(_lock); _enable_pointer_events = false; _pointer_events.clear(); } @@ -121,6 +130,7 @@ disable_pointer_events() { //////////////////////////////////////////////////////////////////// INLINE void GraphicsWindowInputDevice:: set_pointer_in_window(int x, int y, double time) { + // mutex is handled in set pointer .. convience function set_pointer(true, x, y, time); } @@ -133,6 +143,7 @@ set_pointer_in_window(int x, int y, double time) { //////////////////////////////////////////////////////////////////// INLINE void GraphicsWindowInputDevice:: set_pointer_out_of_window(double time) { + // mutex is handled in set pointer .. convience function set_pointer(false, _mouse_data._xpos, _mouse_data._ypos, time); } diff --git a/panda/src/display/graphicsWindowInputDevice.cxx b/panda/src/display/graphicsWindowInputDevice.cxx index 93325e499f..0d24c89ab1 100644 --- a/panda/src/display/graphicsWindowInputDevice.cxx +++ b/panda/src/display/graphicsWindowInputDevice.cxx @@ -99,22 +99,9 @@ pointer_and_keyboard(GraphicsWindow *host, const string &name) { // Description: //////////////////////////////////////////////////////////////////// GraphicsWindowInputDevice:: -GraphicsWindowInputDevice(const GraphicsWindowInputDevice ©) : - _host(copy._host), - _name(copy._name), - _flags(copy._flags), - _device_index(copy._device_index), - _event_sequence(copy._event_sequence), - _pointer_mode_enable(copy._pointer_mode_enable), - _pointer_speed(copy._pointer_speed), - _pointer_true_x(copy._pointer_true_x), - _pointer_true_y(copy._pointer_true_y), - _enable_pointer_events(copy._enable_pointer_events), - _mouse_data(copy._mouse_data), - _true_mouse_data(copy._true_mouse_data), - _button_events(copy._button_events), - _pointer_events(copy._pointer_events) +GraphicsWindowInputDevice(const GraphicsWindowInputDevice ©) { + *this = copy; } //////////////////////////////////////////////////////////////////// @@ -123,7 +110,10 @@ GraphicsWindowInputDevice(const GraphicsWindowInputDevice ©) : // Description: //////////////////////////////////////////////////////////////////// void GraphicsWindowInputDevice:: -operator = (const GraphicsWindowInputDevice ©) { +operator = (const GraphicsWindowInputDevice ©) +{ + MutexHolder holder(_lock); + MutexHolder holder1(copy._lock); _host = copy._host; _name = copy._name; _flags = copy._flags; @@ -160,6 +150,7 @@ GraphicsWindowInputDevice:: //////////////////////////////////////////////////////////////////// bool GraphicsWindowInputDevice:: has_button_event() const { + MutexHolder holder(_lock); return !_button_events.empty(); } @@ -171,6 +162,7 @@ has_button_event() const { //////////////////////////////////////////////////////////////////// ButtonEvent GraphicsWindowInputDevice:: get_button_event() { + MutexHolder holder(_lock); ButtonEvent be = _button_events.front(); _button_events.pop_front(); return be; @@ -186,6 +178,7 @@ get_button_event() { //////////////////////////////////////////////////////////////////// bool GraphicsWindowInputDevice:: has_pointer_event() const { + MutexHolder holder(_lock); return (_pointer_events != 0); } @@ -197,6 +190,7 @@ has_pointer_event() const { //////////////////////////////////////////////////////////////////// PT(PointerEventList) GraphicsWindowInputDevice:: get_pointer_events() { + MutexHolder holder(_lock); PT(PointerEventList) result = _pointer_events; _pointer_events = 0; return result; @@ -222,6 +216,7 @@ get_pointer_events() { //////////////////////////////////////////////////////////////////// void GraphicsWindowInputDevice:: enable_pointer_mode(double speed) { + MutexHolder holder(_lock); nassertv(_device_index != 0); _pointer_mode_enable = true; _pointer_speed = speed; @@ -239,6 +234,7 @@ enable_pointer_mode(double speed) { //////////////////////////////////////////////////////////////////// void GraphicsWindowInputDevice:: disable_pointer_mode() { + MutexHolder holder(_lock); nassertv(_device_index != 0); _pointer_mode_enable = false; _pointer_speed = 1.0; @@ -254,6 +250,8 @@ disable_pointer_mode() { //////////////////////////////////////////////////////////////////// void GraphicsWindowInputDevice:: set_pointer(bool inwin, int x, int y, double time) { + MutexHolder holder(_lock); + int delta_x = x - _true_mouse_data._xpos; int delta_y = y - _true_mouse_data._ypos; _true_mouse_data._in_window = inwin; @@ -295,6 +293,7 @@ set_pointer(bool inwin, int x, int y, double time) { //////////////////////////////////////////////////////////////////// void GraphicsWindowInputDevice:: button_down(ButtonHandle button, double time) { + MutexHolder holder(_lock); _button_events.push_back(ButtonEvent(button, ButtonEvent::T_down, time)); } @@ -308,6 +307,7 @@ button_down(ButtonHandle button, double time) { //////////////////////////////////////////////////////////////////// void GraphicsWindowInputDevice:: button_resume_down(ButtonHandle button, double time) { + MutexHolder holder(_lock); _button_events.push_back(ButtonEvent(button, ButtonEvent::T_resume_down, time)); } @@ -318,6 +318,7 @@ button_resume_down(ButtonHandle button, double time) { //////////////////////////////////////////////////////////////////// void GraphicsWindowInputDevice:: button_up(ButtonHandle button, double time) { + MutexHolder holder(_lock); _button_events.push_back(ButtonEvent(button, ButtonEvent::T_up, time)); } @@ -329,6 +330,7 @@ button_up(ButtonHandle button, double time) { //////////////////////////////////////////////////////////////////// void GraphicsWindowInputDevice:: keystroke(int keycode, double time) { + MutexHolder holder(_lock); _button_events.push_back(ButtonEvent(keycode, time)); } @@ -341,6 +343,7 @@ keystroke(int keycode, double time) { void GraphicsWindowInputDevice:: candidate(const wstring &candidate_string, size_t highlight_start, size_t highlight_end, size_t cursor_pos) { + MutexHolder holder(_lock); _button_events.push_back(ButtonEvent(candidate_string, highlight_start, highlight_end, cursor_pos)); diff --git a/panda/src/display/graphicsWindowInputDevice.h b/panda/src/display/graphicsWindowInputDevice.h index 66e6bb9d3c..f01a1b5138 100644 --- a/panda/src/display/graphicsWindowInputDevice.h +++ b/panda/src/display/graphicsWindowInputDevice.h @@ -29,6 +29,9 @@ #include "pdeque.h" #include "pvector.h" +#include "pmutex.h" +#include "mutexHolder.h" + //////////////////////////////////////////////////////////////////// // Class : GraphicsWindowInputDevice @@ -58,8 +61,8 @@ public: INLINE void set_device_index(int index); - INLINE const MouseData &get_pointer() const; - INLINE const MouseData &get_raw_pointer() const; + INLINE MouseData get_pointer() const; + INLINE MouseData get_raw_pointer() const; INLINE void enable_pointer_events(); INLINE void disable_pointer_events(); @@ -100,6 +103,8 @@ private: IDF_has_keyboard = 0x02 }; typedef pdeque ButtonEvents; + + Mutex _lock; GraphicsWindow *_host; @@ -118,6 +123,7 @@ private: MouseData _true_mouse_data; ButtonEvents _button_events; PT(PointerEventList) _pointer_events; + }; #include "graphicsWindowInputDevice.I"