Added SyncRoot object to BindingsBase that can be used to protect shared state in the various bindings.

This commit is contained in:
the_fiddler 2009-11-03 23:26:57 +00:00
parent 043e0fec2f
commit ddb56a1952
8 changed files with 112 additions and 7 deletions

View file

@ -111,6 +111,14 @@ namespace OpenTK
/// </remarks> /// </remarks>
protected abstract IntPtr GetAddress(string funcname); protected abstract IntPtr GetAddress(string funcname);
/// <summary>
/// Gets an object that can be used to synchronize access to the bindings implementation.
/// </summary>
/// <remarks>This object should be unique across bindings but consistent between bindings
/// of the same type. For example, ES10.GL, OpenGL.GL and CL10.CL should all return
/// unique objects, but all instances of ES10.GL should return the same object.</remarks>
protected abstract object SyncRoot { get; }
#endregion #endregion
#region Internal Members #region Internal Members
@ -141,8 +149,11 @@ namespace OpenTK
if (d != null) if (d != null)
++supported; ++supported;
lock (SyncRoot)
{
f.SetValue(null, d); f.SetValue(null, d);
} }
}
rebuildExtensionList = true; rebuildExtensionList = true;
@ -166,10 +177,13 @@ namespace OpenTK
Delegate old = f.GetValue(null) as Delegate; Delegate old = f.GetValue(null) as Delegate;
Delegate @new = LoadDelegate(f.Name, f.FieldType); Delegate @new = LoadDelegate(f.Name, f.FieldType);
lock (SyncRoot)
{
if (old.Target != @new.Target) if (old.Target != @new.Target)
{ {
f.SetValue(null, @new); f.SetValue(null, @new);
} }
}
return @new != null; return @new != null;
} }

View file

@ -35,11 +35,12 @@ namespace OpenTK.Compute.CL10
/// <summary> /// <summary>
/// Provides access to the OpenCL 1.0 flat API. /// Provides access to the OpenCL 1.0 flat API.
/// </summary> /// </summary>
public partial class CL public partial class CL : BindingsBase
{ {
#region Private Members #region Fields
const string Library = "opencl.dll"; const string Library = "opencl.dll";
static readonly object sync_root = new object();
#endregion #endregion
@ -61,5 +62,35 @@ namespace OpenTK.Compute.CL10
} }
#endregion #endregion
#region Protected Members
/// <summary>
/// Returns a synchronization token unique for the GL class.
/// </summary>
protected override object SyncRoot
{
get { return sync_root; }
}
/// <summary>
/// Not supported yet.
/// </summary>
/// <param name="funcname">The name of the extension function.</param>
/// <returns>A pointer to the extension function, if available; IntPtr.Zero otherwise.</returns>
/// <remarks>
/// <para>Use <see cref="Marshal.GetDelegateForFunctionPointer"/> to turn this function pointer
/// into a callable delegate.</para>
/// <para>A non-zero return value does not mean that this extension function is supported. You also
/// need to query available extensions through <see cref="GetDeviceInfo"/>.</para>
/// <para>This method will always return IntPtr.Zero for core (i.e. non-extension) functions.</para>
/// </remarks>
protected override IntPtr GetAddress(string funcname)
{
throw new NotSupportedException();
//CL.GetExtensionFunctionAddress
}
#endregion
} }
} }

View file

@ -10,5 +10,19 @@ namespace OpenTK.Graphics.ES10
public sealed partial class GL : GraphicsBindingsBase public sealed partial class GL : GraphicsBindingsBase
{ {
const string Library = "libGLES.dll"; const string Library = "libGLES.dll";
static readonly object sync_root = new object();
#region --- Protected Members ---
/// <summary>
/// Returns a synchronization token unique for the GL class.
/// </summary>
protected override object SyncRoot
{
get { return sync_root; }
}
#endregion
} }
} }

View file

@ -12,5 +12,19 @@ namespace OpenTK.Graphics.ES11
public sealed partial class GL : GraphicsBindingsBase public sealed partial class GL : GraphicsBindingsBase
{ {
const string Library = "libGLES.dll"; const string Library = "libGLES.dll";
static readonly object sync_root = new object();
#region --- Protected Members ---
/// <summary>
/// Returns a synchronization token unique for the GL class.
/// </summary>
protected override object SyncRoot
{
get { return sync_root; }
}
#endregion
} }
} }

View file

@ -37,6 +37,19 @@ namespace OpenTK.Graphics.ES20
public sealed partial class GL : GraphicsBindingsBase public sealed partial class GL : GraphicsBindingsBase
{ {
const string Library = "libGLESv2.dll"; const string Library = "libGLESv2.dll";
static readonly object sync_root = new object();
#region --- Protected Members ---
/// <summary>
/// Returns a synchronization token unique for the GL class.
/// </summary>
protected override object SyncRoot
{
get { return sync_root; }
}
#endregion
#region Helper Overloads #region Helper Overloads

View file

@ -32,7 +32,7 @@ namespace OpenTK.Graphics
/// <summary> /// <summary>
/// Implements BindingsBase for the OpenTK.Graphics namespace (OpenGL and OpenGL|ES). /// Implements BindingsBase for the OpenTK.Graphics namespace (OpenGL and OpenGL|ES).
/// </summary> /// </summary>
public class GraphicsBindingsBase : BindingsBase public abstract class GraphicsBindingsBase : BindingsBase
{ {
/// <summary> /// <summary>
/// Retrieves an unmanaged function pointer to the specified function. /// Retrieves an unmanaged function pointer to the specified function.

View file

@ -52,7 +52,8 @@ namespace OpenTK.Graphics.OpenGL
internal const string Library = "opengl32.dll"; internal const string Library = "opengl32.dll";
private static SortedList<string, bool> AvailableExtensions = new SortedList<string, bool>(); static SortedList<string, bool> AvailableExtensions = new SortedList<string, bool>();
static readonly object sync_root = new object();
#endregion #endregion
@ -335,6 +336,18 @@ namespace OpenTK.Graphics.OpenGL
#endif #endif
#endregion #endregion
#region --- Protected Members ---
/// <summary>
/// Returns a synchronization token unique for the GL class.
/// </summary>
protected override object SyncRoot
{
get { return sync_root; }
}
#endregion
#region --- GL Overloads --- #region --- GL Overloads ---
#pragma warning disable 3019 #pragma warning disable 3019

View file

@ -18,10 +18,16 @@ namespace OpenTK.Platform.X11
partial class Glx : BindingsBase partial class Glx : BindingsBase
{ {
const string Library = "libGL.so.1"; const string Library = "libGL.so.1";
static readonly object sync_root = new object();
// Disable BeforeFieldInit optimization. // Disable BeforeFieldInit optimization.
static Glx() { } static Glx() { }
protected override object SyncRoot
{
get { return sync_root; }
}
protected override IntPtr GetAddress (string funcname) protected override IntPtr GetAddress (string funcname)
{ {
return Glx.GetProcAddress(funcname); return Glx.GetProcAddress(funcname);