From 9b63078b11b017e11c53cbbc590ea165912d8f37 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Tue, 3 Nov 2009 13:16:59 +0000 Subject: [PATCH] Fixed race condition in GraphicsMode.Default. --- Source/OpenTK/Graphics/GraphicsMode.cs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/Source/OpenTK/Graphics/GraphicsMode.cs b/Source/OpenTK/Graphics/GraphicsMode.cs index 2b7fb28b..b6e18b5e 100644 --- a/Source/OpenTK/Graphics/GraphicsMode.cs +++ b/Source/OpenTK/Graphics/GraphicsMode.cs @@ -23,7 +23,7 @@ namespace OpenTK.Graphics static GraphicsMode defaultMode; static IGraphicsMode implementation; - static object mode_selection_lock = new object(); + static readonly object SyncRoot = new object(); #region --- Constructors --- @@ -31,7 +31,10 @@ namespace OpenTK.Graphics static GraphicsMode() { - implementation = Platform.Factory.Default.CreateGraphicsMode(); + lock (SyncRoot) + { + implementation = Platform.Factory.Default.CreateGraphicsMode(); + } } #endregion @@ -180,10 +183,7 @@ namespace OpenTK.Graphics if (index == null) { 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; ColorFormat = mode.ColorFormat; @@ -303,13 +303,16 @@ namespace OpenTK.Graphics { get { - if (defaultMode == null) + lock (SyncRoot) { - Debug.Print("Creating default GraphicsMode ({0}, {1}, {2}, {3}, {4}, {5}, {6}).", DisplayDevice.Default.BitsPerPixel, - 16, 0, 0, 0, 2, false); - defaultMode = new GraphicsMode(DisplayDevice.Default.BitsPerPixel, 16, 0, 0, 0, 2, false); + if (defaultMode == null) + { + Debug.Print("Creating default GraphicsMode ({0}, {1}, {2}, {3}, {4}, {5}, {6}).", DisplayDevice.Default.BitsPerPixel, + 16, 0, 0, 0, 2, false); + defaultMode = new GraphicsMode(DisplayDevice.Default.BitsPerPixel, 16, 0, 0, 0, 2, false); + } + return defaultMode; } - return defaultMode; } }