From 40940d143064ba86a333cb48e8647b66aa96a707 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Tue, 3 Nov 2009 23:36:37 +0000 Subject: [PATCH] Avoid static caching of extension strings and improve multithreading behavior - WGL extension checking is fast enough that caching is not very useful. --- Source/OpenTK/Platform/Windows/WglHelper.cs | 28 +++++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/Source/OpenTK/Platform/Windows/WglHelper.cs b/Source/OpenTK/Platform/Windows/WglHelper.cs index 30a46e17..dbdd4dfb 100644 --- a/Source/OpenTK/Platform/Windows/WglHelper.cs +++ b/Source/OpenTK/Platform/Windows/WglHelper.cs @@ -132,7 +132,6 @@ namespace OpenTK.Platform.Windows /// Contains ARB extensions for WGL. public static partial class Arb { - private static string[] extensions; /// /// Checks if a Wgl extension is supported by the given context. /// @@ -141,20 +140,27 @@ namespace OpenTK.Platform.Windows /// True if the extension is supported by the given context, false otherwise public static bool SupportsExtension(WinGLContext context, string ext) { - if (Wgl.Delegates.wglGetExtensionsStringARB != null) + // We cache this locally, as another thread might create a context which doesn't support this method. + // The design is far from ideal, but there's no good solution to this issue as long as we are using + // static WGL/GL classes. Fortunately, this issue is extremely unlikely to arise in practice, as you'd + // have to create one accelerated and one non-accelerated context in the same application, with the + // non-accelerated context coming second. + Wgl.Delegates.GetExtensionsStringARB get = Wgl.Delegates.wglGetExtensionsStringARB; + + if (get != null) { - if (rebuildExtensionList) + string[] extensions = null; + unsafe { - rebuildExtensionList = false; - - extensions = Wgl.Arb.GetExtensionsString(context.DeviceContext).Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); - if (extensions == null || extensions.Length == 0) - return false; - - Array.Sort(extensions); + extensions = new string((sbyte*)get(context.DeviceContext)) + .Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); } + if (extensions == null || extensions.Length == 0) + return false; - return Array.BinarySearch(extensions, ext) != -1; + foreach (string s in extensions) + if (s == ext) + return true; } return false; }