Browse Source

Thread-safety for LoadedAssembly.GetTypeSystemOrNull()

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

40
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,30 +154,34 @@ 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 new SimpleCompilation(
return typeSystem = 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)
{ {
if (typeSystemWithOptions != null && options == currentTypeSystemOptions) lock (typeSystemWithOptionsLockObj)
return typeSystemWithOptions; {
var module = GetPEFileOrNull(); if (typeSystemWithOptions != null && options == currentTypeSystemOptions)
if (module == null) return typeSystemWithOptions;
return null; var module = GetPEFileOrNull();
currentTypeSystemOptions = options; if (module == null)
return typeSystemWithOptions = new SimpleCompilation( return null;
module.WithOptions(options | TypeSystemOptions.Uncached | TypeSystemOptions.KeepModifiers), currentTypeSystemOptions = options;
MinimalCorlib.Instance); return typeSystemWithOptions = new SimpleCompilation(
module.WithOptions(options | TypeSystemOptions.Uncached | TypeSystemOptions.KeepModifiers),
MinimalCorlib.Instance);
}
} }
public AssemblyList AssemblyList => assemblyList; public AssemblyList AssemblyList => assemblyList;

Loading…
Cancel
Save