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