Fixed race condition in GraphicsMode.Default.

This commit is contained in:
the_fiddler 2009-11-03 13:16:59 +00:00
parent baf44cff90
commit 9b63078b11

View file

@ -23,16 +23,19 @@ namespace OpenTK.Graphics
static GraphicsMode defaultMode; static GraphicsMode defaultMode;
static IGraphicsMode implementation; static IGraphicsMode implementation;
static object mode_selection_lock = new object(); static readonly object SyncRoot = new object();
#region --- Constructors --- #region --- Constructors ---
#region static GraphicsMode() #region static GraphicsMode()
static GraphicsMode() static GraphicsMode()
{
lock (SyncRoot)
{ {
implementation = Platform.Factory.Default.CreateGraphicsMode(); implementation = Platform.Factory.Default.CreateGraphicsMode();
} }
}
#endregion #endregion
@ -180,10 +183,7 @@ namespace OpenTK.Graphics
if (index == null) if (index == null)
{ {
GraphicsMode mode; GraphicsMode mode;
lock (mode_selection_lock)
{
mode = implementation.SelectGraphicsMode(ColorFormat, Depth, Stencil, Samples, AccumulatorFormat, Buffers, Stereo); mode = implementation.SelectGraphicsMode(ColorFormat, Depth, Stencil, Samples, AccumulatorFormat, Buffers, Stereo);
}
Index = mode.Index; Index = mode.Index;
ColorFormat = mode.ColorFormat; ColorFormat = mode.ColorFormat;
@ -302,6 +302,8 @@ namespace OpenTK.Graphics
public static GraphicsMode Default public static GraphicsMode Default
{ {
get get
{
lock (SyncRoot)
{ {
if (defaultMode == null) if (defaultMode == null)
{ {
@ -312,6 +314,7 @@ namespace OpenTK.Graphics
return defaultMode; return defaultMode;
} }
} }
}
#endregion #endregion