1
//---------------------------------------------------------------------
2 // <copyright file="LockedAssemblyCache.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
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
27 Monitor
.Enter(_lockObject
);
28 #pragma warning restore 0618
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
);
39 _globalAssemblyCache
= null;
42 [Conditional("DEBUG")]
43 private void AssertLockedByThisThread()
46 Monitor
.TryEnter(_lockObject
, ref 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
);
69 AssertLockedByThisThread();
70 _globalAssemblyCache
.Clear();