提交 03976157 编写于 作者: A Andy Gocke 提交者: GitHub

Use Interlocked exchange on some MetadataReferences to guarantee reference equality (#18782)

It seems like the reference set for CoreCLR tests is sometimes different, resulting
in CI failures. This is likely due to the lazy initialization of static MetadataReferences
in TestBase without thread synchronization. This change adds Interlocked calls where
necessary and adds a comment warning about when more Interlocked calls may be
needed.

This is a test-only change.
上级 9c782b85
......@@ -486,6 +486,9 @@ public static SyntaxTree ParseWithRoundTripCheck(string text, CSharpParseOptions
? ImmutableArray.Create<MetadataReference>(NetStandard20.NetStandard, NetStandard20.MscorlibRef, NetStandard20.SystemRuntimeRef, NetStandard20.SystemDynamicRuntimeRef)
: ImmutableArray.Create(MscorlibRef);
// Careful! Make sure everything in s_desktopRefsToRemove is constructed with
// the same object identity, since MetadataReference uses reference equality.
// this may mean adding Interlocked calls in the construction of the reference.
private static readonly ImmutableArray<MetadataReference> s_desktopRefsToRemove = ImmutableArray.Create(SystemRef, SystemCoreRef);
public static CSharpCompilation CreateStandardCompilation(
......
......@@ -4,6 +4,7 @@
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Xml.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
......@@ -147,7 +148,12 @@ public static MetadataReference SystemCoreRef
{
if (s_systemCoreRef == null)
{
s_systemCoreRef = AssemblyMetadata.CreateFromImage(TestResources.NetFX.v4_0_30319.System_Core).GetReference(display: "System.Core.v4_0_30319.dll");
// We rely on reference equality in CreateSharedCompilation, so
// we must use a CompareExchange here.
Interlocked.CompareExchange(
ref s_systemCoreRef,
AssemblyMetadata.CreateFromImage(TestResources.NetFX.v4_0_30319.System_Core).GetReference(display: "System.Core.v4_0_30319.dll"),
null);
}
return s_systemCoreRef;
......@@ -414,7 +420,12 @@ public static MetadataReference SystemRef
{
if (s_systemRef == null)
{
s_systemRef = AssemblyMetadata.CreateFromImage(TestResources.NetFX.v4_0_30319.System).GetReference(display: "System.v4_0_30319.dll");
// We rely on reference equality in CreateSharedCompilation, so
// we must use a CompareExchange here.
Interlocked.CompareExchange(
ref s_systemRef,
AssemblyMetadata.CreateFromImage(TestResources.NetFX.v4_0_30319.System).GetReference(display: "System.v4_0_30319.dll"),
null);
}
return s_systemRef;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册