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,8 +236,11 @@ namespace OpenTK.Platform.Windows
break; break;
} }
keyboards[rawids[handle]] = keyboard; lock (UpdateLock)
return processed; {
keyboards[rawids[handle]] = keyboard;
return processed;
}
} }
#endregion #endregion
@ -264,20 +268,26 @@ namespace OpenTK.Platform.Windows
public KeyboardState GetState() public KeyboardState GetState()
{ {
KeyboardState master = new KeyboardState(); lock (UpdateLock)
foreach (KeyboardState ks in keyboards)
{ {
master.MergeBits(ks); KeyboardState master = new KeyboardState();
foreach (KeyboardState ks in keyboards)
{
master.MergeBits(ks);
}
return master;
} }
return master;
} }
public KeyboardState GetState(int index) public KeyboardState GetState(int index)
{ {
if (keyboards.Count > index) lock (UpdateLock)
return keyboards[index]; {
else if (keyboards.Count > index)
return new KeyboardState(); return keyboards[index];
else
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)
{ {
@ -65,20 +66,26 @@ namespace OpenTK.Platform.Windows
public MouseState GetState() public MouseState GetState()
{ {
MouseState master = new MouseState(); lock (UpdateLock)
foreach (MouseState ms in mice)
{ {
master.MergeBits(ms); MouseState master = new MouseState();
foreach (MouseState ms in mice)
{
master.MergeBits(ms);
}
return master;
} }
return master;
} }
public MouseState GetState(int index) public MouseState GetState(int index)
{ {
if (mice.Count > index) lock (UpdateLock)
return mice[index]; {
else if (mice.Count > index)
return new MouseState(); return mice[index];
else
return new MouseState();
}
} }
#endregion #endregion
@ -226,8 +233,11 @@ namespace OpenTK.Platform.Windows
mouse.Y += raw.LastY; mouse.Y += raw.LastY;
} }
mice[rawids[handle]] = mouse; lock (UpdateLock)
return true; {
mice[rawids[handle]] = mouse;
return true;
}
} }
} }
} }