From 494a5f4719e1c857c7a0f91abb56ad96522f7f4f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 16 Sep 2019 13:39:45 +0200 Subject: [PATCH] Fixed a threading issue --- .../Models/ReadableMember.GetValue.cs | 29 +++++++++++++++---- .../Translation/ShaderLoader.cs | 12 ++++++-- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.GetValue.cs b/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.GetValue.cs index f40bf55b..411f6cb4 100644 --- a/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.GetValue.cs +++ b/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.GetValue.cs @@ -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; /// - /// 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 /// - /// The target instance to use to read the value from - [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); + /// + /// Returns the value of the wrapped member for the current instance + /// + /// The target instance to use to read the value from + /// This method doesn't ensure that the accessor is available + /// The dynamic accessor is not available + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public object DangerousGetValue(object? instance) => _Getter!(instance); + + /// + /// Returns the value of the wrapped member for the current instance + /// + /// The target instance to use to read the value from + [Pure] + public object GetValue(object? instance) + { + PreloadAccessor(); + return _Getter!(instance); } /// diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index ed75fcee..ff2a4ae6 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -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(); } ///