Browse Source

Thread-safety for LoadedAssembly.GetTypeSystemOrNull()

pull/2191/head
Daniel Grunwald 5 years ago
parent
commit
b2cbb12f15
  1. 12
      ILSpy/LoadedAssembly.cs

12
ILSpy/LoadedAssembly.cs

@ -146,7 +146,7 @@ namespace ICSharpCode.ILSpy
ICompilation typeSystem; ICompilation typeSystem;
/// <summary> /// <summary>
/// Gets a type system containing all types from this assembly + primitve types from mscorlib. /// Gets a type system containing all types from this assembly + primitive types from mscorlib.
/// Returns null in case of load errors. /// Returns null in case of load errors.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
@ -154,20 +154,23 @@ namespace ICSharpCode.ILSpy
/// </remarks> /// </remarks>
public ICompilation GetTypeSystemOrNull() public ICompilation GetTypeSystemOrNull()
{ {
if (typeSystem != null) return LazyInitializer.EnsureInitialized(ref this.typeSystem, () => {
return typeSystem;
var module = GetPEFileOrNull(); var module = GetPEFileOrNull();
if (module == null) if (module == null)
return null; return null;
return typeSystem = new SimpleCompilation( return new SimpleCompilation(
module.WithOptions(TypeSystemOptions.Default | TypeSystemOptions.Uncached | TypeSystemOptions.KeepModifiers), module.WithOptions(TypeSystemOptions.Default | TypeSystemOptions.Uncached | TypeSystemOptions.KeepModifiers),
MinimalCorlib.Instance); MinimalCorlib.Instance);
});
} }
readonly object typeSystemWithOptionsLockObj = new object();
ICompilation typeSystemWithOptions; ICompilation typeSystemWithOptions;
TypeSystemOptions currentTypeSystemOptions; TypeSystemOptions currentTypeSystemOptions;
public ICompilation GetTypeSystemOrNull(TypeSystemOptions options) public ICompilation GetTypeSystemOrNull(TypeSystemOptions options)
{
lock (typeSystemWithOptionsLockObj)
{ {
if (typeSystemWithOptions != null && options == currentTypeSystemOptions) if (typeSystemWithOptions != null && options == currentTypeSystemOptions)
return typeSystemWithOptions; return typeSystemWithOptions;
@ -179,6 +182,7 @@ namespace ICSharpCode.ILSpy
module.WithOptions(options | TypeSystemOptions.Uncached | TypeSystemOptions.KeepModifiers), module.WithOptions(options | TypeSystemOptions.Uncached | TypeSystemOptions.KeepModifiers),
MinimalCorlib.Instance); MinimalCorlib.Instance);
} }
}
public AssemblyList AssemblyList => assemblyList; public AssemblyList AssemblyList => assemblyList;

Loading…
Cancel
Save