Do not modify device state during the GetState() call. Fixes intermittent crashes.

This commit is contained in:
the_fiddler 2010-10-28 13:00:21 +00:00
parent 03c97e306b
commit c1043d1693
2 changed files with 40 additions and 20 deletions

View file

@ -46,6 +46,7 @@ namespace OpenTK.Platform.Windows
readonly Dictionary<ContextHandle, int> rawids = new Dictionary<ContextHandle, int>(); readonly Dictionary<ContextHandle, int> rawids = new Dictionary<ContextHandle, int>();
private List<KeyboardDevice> keyboards_old = new List<KeyboardDevice>(); private List<KeyboardDevice> keyboards_old = new List<KeyboardDevice>();
private IntPtr window; private IntPtr window;
readonly object UpdateLock = new object();
#region --- Constructors --- #region --- Constructors ---
@ -235,9 +236,12 @@ namespace OpenTK.Platform.Windows
break; break;
} }
lock (UpdateLock)
{
keyboards[rawids[handle]] = keyboard; keyboards[rawids[handle]] = keyboard;
return processed; return processed;
} }
}
#endregion #endregion
@ -263,6 +267,8 @@ namespace OpenTK.Platform.Windows
} }
public KeyboardState GetState() public KeyboardState GetState()
{
lock (UpdateLock)
{ {
KeyboardState master = new KeyboardState(); KeyboardState master = new KeyboardState();
foreach (KeyboardState ks in keyboards) foreach (KeyboardState ks in keyboards)
@ -271,14 +277,18 @@ namespace OpenTK.Platform.Windows
} }
return master; return master;
} }
}
public KeyboardState GetState(int index) public KeyboardState GetState(int index)
{
lock (UpdateLock)
{ {
if (keyboards.Count > index) if (keyboards.Count > index)
return keyboards[index]; return keyboards[index];
else else
return new KeyboardState(); return new KeyboardState();
} }
}
#endregion #endregion

View file

@ -44,6 +44,7 @@ namespace OpenTK.Platform.Windows
List<MouseState> mice; List<MouseState> mice;
Dictionary<ContextHandle, int> rawids; // ContextHandle instead of IntPtr for fast dictionary access Dictionary<ContextHandle, int> rawids; // ContextHandle instead of IntPtr for fast dictionary access
readonly IntPtr Window; readonly IntPtr Window;
readonly object UpdateLock = new object();
public WinRawMouse(IntPtr window) public WinRawMouse(IntPtr window)
{ {
@ -64,6 +65,8 @@ namespace OpenTK.Platform.Windows
public IList<MouseDevice> Mouse { get { throw new NotImplementedException(); } } public IList<MouseDevice> Mouse { get { throw new NotImplementedException(); } }
public MouseState GetState() public MouseState GetState()
{
lock (UpdateLock)
{ {
MouseState master = new MouseState(); MouseState master = new MouseState();
foreach (MouseState ms in mice) foreach (MouseState ms in mice)
@ -72,14 +75,18 @@ namespace OpenTK.Platform.Windows
} }
return master; return master;
} }
}
public MouseState GetState(int index) public MouseState GetState(int index)
{
lock (UpdateLock)
{ {
if (mice.Count > index) if (mice.Count > index)
return mice[index]; return mice[index];
else else
return new MouseState(); return new MouseState();
} }
}
#endregion #endregion
@ -226,8 +233,11 @@ namespace OpenTK.Platform.Windows
mouse.Y += raw.LastY; mouse.Y += raw.LastY;
} }
lock (UpdateLock)
{
mice[rawids[handle]] = mouse; mice[rawids[handle]] = mouse;
return true; return true;
} }
} }
}
} }