Fixed a threading issue

このコミットが含まれているのは:
Sergio Pedri committed 2019-09-16 13:39:45 +02:00
1 親 8cb9bd679b
コミット 494a5f4719
2ファイル変更
+33 -8

検索条件に一致するファイルはありません

+23 -6
ファイルの表示
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
namespace ComputeSharp.Shaders.Translation.Models
{
@@ -19,11 +20,9 @@ namespace ComputeSharp.Shaders.Translation.Models
private Getter? _Getter;
/// <summary>
/// Returns the value of the wrapped member for the current instance
/// Ensures the dynamic accessor for the current instance is loaded and ready to use
/// </summary>
/// <param name="instance">The target instance to use to read the value from</param>
[Pure]
public object GetValue(object? instance)
public void PreloadAccessor()
{
if (_Getter == null)
{
@@ -38,10 +37,28 @@ namespace ComputeSharp.Shaders.Translation.Models
_Getter = getter = BuildDynamicGetter();
GettersMapping.Add(Id, getter);
}
}
}
return _Getter(instance);
/// <summary>
/// Returns the value of the wrapped member for the current instance
/// </summary>
/// <param name="instance">The target instance to use to read the value from</param>
/// <remarks>This method doesn't ensure that the accessor is available</remarks>
/// <exception cref="NullReferenceException">The dynamic accessor is not available</exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public object DangerousGetValue(object? instance) => _Getter!(instance);
/// <summary>
/// Returns the value of the wrapped member for the current instance
/// </summary>
/// <param name="instance">The target instance to use to read the value from</param>
[Pure]
public object GetValue(object? instance)
{
PreloadAccessor();
return _Getter!(instance);
}
/// <summary>
+10 -2
ファイルの表示
@@ -88,7 +88,7 @@ namespace ComputeSharp.Shaders.Translation
int index = 1;
foreach (var item in _BufferMembers)
{
yield return (index++, (GraphicsResource)item.GetValue(action.Target));
yield return (index++, (GraphicsResource)item.DangerousGetValue(action.Target));
}
}
@@ -105,7 +105,7 @@ namespace ComputeSharp.Shaders.Translation
{
foreach (var item in _VariableMembers)
{
yield return item.GetValue(action.Target);
yield return item.DangerousGetValue(action.Target);
}
}
@@ -260,6 +260,14 @@ namespace ComputeSharp.Shaders.Translation
// Captured static delegates with a return type
LoadStaticMethodSource(fieldName, func.Method);
}
/* Build the dynamic IL accessor for the current member, if one is
* not available already. This is done during while the shader is
* being loaded and not just when needed because the internal cache
* system for the accessors is not thread safe. Preloading the accessor here
* leverages the external lock around the shader loading, ensuring
* that no race conditions occur when accessing the cached mapping */
memberInfo.PreloadAccessor();
}
/// <summary>