Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Remove internal Lock/Reader/Writer structs from System.Composition.Convention #6033

Merged
merged 1 commit into from
Feb 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

45 changes: 0 additions & 45 deletions src/System.Composition.Convention/src/Microsoft/Internal/Lock.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@
<Compile Include="Microsoft\Composition\Diagnostics\TraceSourceTraceWriter.cs" />
<Compile Include="Microsoft\Composition\Diagnostics\TraceWriter.cs" />
<Compile Include="Microsoft\Internal\AttributeServices.cs" />
<Compile Include="Microsoft\Internal\Lock.cs" />
<Compile Include="Microsoft\Internal\Lock.Reader.cs" />
<Compile Include="Microsoft\Internal\Lock.Writer.cs" />
<Compile Include="Microsoft\Internal\ReflectionServices.cs" />
<Compile Include="Strings.Designer.cs" />
<Compile Include="System\Composition\Convention\ConventionBuilder.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using Microsoft.Internal;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

using Microsoft.Internal;
using System.Threading;

namespace System.Composition.Convention
{
Expand All @@ -19,7 +17,7 @@ public class ConventionBuilder : AttributedModelProvider
{
private static readonly List<object> s_emptyList = new List<object>();

private readonly Lock _lock = new Lock();
private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
private readonly List<PartConventionBuilder> _conventions = new List<PartConventionBuilder>();

private readonly Dictionary<MemberInfo, List<Attribute>> _memberInfos = new Dictionary<MemberInfo, List<Attribute>>();
Expand Down Expand Up @@ -161,13 +159,19 @@ public override IEnumerable<Attribute> GetCustomAttributes(Type reflectedType, S
if (typeInfo != null)
{
var memberInfo = typeInfo as MemberInfo;
using (new ReadLock(_lock))
_lock.EnterReadLock();
try
{
_memberInfos.TryGetValue(memberInfo, out cachedAttributes);
}
finally
{
_lock.ExitReadLock();
}
if (cachedAttributes == null)
{
using (new WriteLock(_lock))
_lock.EnterWriteLock();
try
{
//Double check locking another thread may have inserted one while we were away.
if (!_memberInfos.TryGetValue(memberInfo, out cachedAttributes))
Expand Down Expand Up @@ -210,6 +214,10 @@ public override IEnumerable<Attribute> GetCustomAttributes(Type reflectedType, S
// We will have updated all of the MemberInfos by now so lets reload cachedAttributes wiuth the current store
_memberInfos.TryGetValue(memberInfo, out cachedAttributes);
}
finally
{
_lock.ExitWriteLock();
}
}
}
else if (member.IsMemberInfoForProperty() || member.IsMemberInfoForConstructor() || member.IsMemberInfoForMethod())
Expand All @@ -232,7 +240,8 @@ private List<Attribute> ReadMemberCustomAttributes(Type reflectedType, System.Re
bool getMemberAttributes = false;

// Now edit the attributes returned from the base type
using (new ReadLock(_lock))
_lock.EnterReadLock();
try
{
if (!_memberInfos.TryGetValue(member, out cachedAttributes))
{
Expand All @@ -247,16 +256,25 @@ private List<Attribute> ReadMemberCustomAttributes(Type reflectedType, System.Re
cachedAttributes = null;
}
}
finally
{
_lock.ExitReadLock();
}

if (getMemberAttributes)
{
GetCustomAttributes(null, reflectedType.GetTypeInfo() as MemberInfo);

// We should have run the rules for the enclosing parameter so we can again
using (new ReadLock(_lock))
_lock.EnterReadLock();
try
{
_memberInfos.TryGetValue(member, out cachedAttributes);
}
finally
{
_lock.ExitReadLock();
}
}

return cachedAttributes;
Expand All @@ -283,7 +301,8 @@ private List<Attribute> ReadParameterCustomAttributes(Type reflectedType, System
bool getMemberAttributes = false;

// Now edit the attributes returned from the base type
using (new ReadLock(_lock))
_lock.EnterReadLock();
try
{
if (!_parameters.TryGetValue(parameter, out cachedAttributes))
{
Expand All @@ -298,16 +317,25 @@ private List<Attribute> ReadParameterCustomAttributes(Type reflectedType, System
cachedAttributes = null;
}
}
finally
{
_lock.ExitReadLock();
}

if (getMemberAttributes)
{
GetCustomAttributes(null, reflectedType.GetTypeInfo() as MemberInfo);

// We should have run the rules for the enclosing parameter so we can again
using (new ReadLock(_lock))
_lock.EnterReadLock();
try
{
_parameters.TryGetValue(parameter, out cachedAttributes);
}
finally
{
_lock.ExitReadLock();
}
}

return cachedAttributes;
Expand Down