Updates referencesource to .NET 4.7
[mono-project.git] / mcs / class / referencesource / System.Data.Entity / System / Data / Metadata / ObjectLayer / LockedAssemblyCache.cs
bloba81ccee2cee04bcb6d7d45e3319e976d88379e1f
1 //---------------------------------------------------------------------
2 // <copyright file="LockedAssemblyCache.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 // </copyright>
5 //
6 // @owner Microsoft
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
10 using System.Collections.Generic;
11 using System.Diagnostics;
12 using System.Reflection;
13 using System.Threading;
15 namespace System.Data.Metadata.Edm
17 internal class LockedAssemblyCache : IDisposable
19 private object _lockObject;
20 private Dictionary<Assembly, ImmutableAssemblyCacheEntry> _globalAssemblyCache;
21 internal LockedAssemblyCache(object lockObject, Dictionary<Assembly, ImmutableAssemblyCacheEntry> globalAssemblyCache)
23 _lockObject = lockObject;
24 _globalAssemblyCache = globalAssemblyCache;
25 #pragma warning disable 0618
26 //@
27 Monitor.Enter(_lockObject);
28 #pragma warning restore 0618
31 public void Dispose()
33 // Technically, calling GC.SuppressFinalize is not required because the class does not
34 // have a finalizer, but it does no harm, protects against the case where a finalizer is added
35 // in the future, and prevents an FxCop warning.
36 GC.SuppressFinalize(this);
37 Monitor.Exit(_lockObject);
38 _lockObject = null;
39 _globalAssemblyCache = null;
42 [Conditional("DEBUG")]
43 private void AssertLockedByThisThread()
45 bool entered = false;
46 Monitor.TryEnter(_lockObject, ref entered);
47 if (entered)
49 Monitor.Exit(_lockObject);
52 Debug.Assert(entered, "The cache is being accessed by a thread that isn't holding the lock");
55 internal bool TryGetValue(Assembly assembly, out ImmutableAssemblyCacheEntry cacheEntry)
57 AssertLockedByThisThread();
58 return _globalAssemblyCache.TryGetValue(assembly, out cacheEntry);
61 internal void Add(Assembly assembly, ImmutableAssemblyCacheEntry assemblyCacheEntry)
63 AssertLockedByThisThread();
64 _globalAssemblyCache.Add(assembly, assemblyCacheEntry);
67 internal void Clear()
69 AssertLockedByThisThread();
70 _globalAssemblyCache.Clear();