Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ namespace Castle.Components.DictionaryAdapter
{
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;
using System.Diagnostics;

using Castle.Components.DictionaryAdapter.Xml;
using Castle.Core.Internal;
Expand All @@ -34,8 +35,8 @@ namespace Castle.Components.DictionaryAdapter
/// </summary>
public class DictionaryAdapterFactory : IDictionaryAdapterFactory
{
private readonly SynchronizedDictionary<Type, DictionaryAdapterMeta> interfaceToMeta =
new SynchronizedDictionary<Type, DictionaryAdapterMeta>();
private readonly ConcurrentDictionary<Type, DictionaryAdapterMeta> interfaceToMeta =
new ConcurrentDictionary<Type, DictionaryAdapterMeta>();

#region IDictionaryAdapterFactory

Expand Down Expand Up @@ -132,8 +133,8 @@ private DictionaryAdapterMeta InternalGetAdapterMeta(Type type,
descriptor = other.CreateDescriptor();
}

var typeBuilder = CreateTypeBuilder(type);
return CreateAdapterMeta(type, typeBuilder, descriptor);
var typeBuilder = CreateTypeBuilder(t);
return CreateAdapterMeta(t, typeBuilder, descriptor);
});
}

Expand Down
120 changes: 0 additions & 120 deletions src/Castle.Core/Core/Internal/SynchronizedDictionary.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private Type GetDelegateType(MetaMethod method, ClassEmitter @class)
GetCacheKeyTypes(method),
null);

return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ =>
return scope.TypeCache.GetOrAdd(key, _ =>
new DelegateTypeGenerator(method, targetType)
.Generate(@class, namingScope)
.BuildType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private Type GetDelegateType(MetaMethod method, ClassEmitter @class)
GetCacheKeyTypes(method),
null);

return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ =>
return scope.TypeCache.GetOrAdd(key, _ =>
new DelegateTypeGenerator(method, targetType)
.Generate(@class, namingScope)
.BuildType());
Expand All @@ -136,7 +136,7 @@ private Type GetInvocationType(MetaMethod method, ClassEmitter @class)

// no locking required as we're already within a lock

return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ => BuildInvocationType(method, @class));
return scope.TypeCache.GetOrAdd(key, _ => BuildInvocationType(method, @class));
}

private MethodGenerator IndirectlyCalledMethodGenerator(MetaMethod method, ClassEmitter proxy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private Type GetInvocationType(MetaMethod method, ClassEmitter @class)

// no locking required as we're already within a lock

return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ =>
return scope.TypeCache.GetOrAdd(key, _ =>
new CompositionInvocationTypeGenerator(method.Method.DeclaringType,
method,
method.Method,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private Type GetInvocationType(MetaMethod method, ClassEmitter emitter)

// no locking required as we're already within a lock

return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ =>
return scope.TypeCache.GetOrAdd(key, _ =>
new CompositionInvocationTypeGenerator(methodInfo.DeclaringType,
method,
methodInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private Type GetInvocationType(MetaMethod method, ClassEmitter emitter)

// no locking required as we're already within a lock

return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ =>
return scope.TypeCache.GetOrAdd(key, _ =>
new CompositionInvocationTypeGenerator(method.Method.DeclaringType,
method,
method.Method,
Expand Down
8 changes: 3 additions & 5 deletions src/Castle.Core/DynamicProxy/Internal/InvocationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@
namespace Castle.DynamicProxy.Internal
{
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Reflection;
using System.Threading;

using Castle.Core.Internal;
using Castle.DynamicProxy.Generators;

internal static class InvocationHelper
{
private static readonly SynchronizedDictionary<CacheKey, MethodInfo> cache =
new SynchronizedDictionary<CacheKey, MethodInfo>();
private static readonly ConcurrentDictionary<CacheKey, MethodInfo> cache =
new ConcurrentDictionary<CacheKey, MethodInfo>();

public static MethodInfo GetMethodOnObject(object target, MethodInfo proxiedMethod)
{
Expand Down
16 changes: 9 additions & 7 deletions src/Castle.Core/DynamicProxy/ModuleScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
namespace Castle.DynamicProxy
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Resources;
using System.Threading;

using Castle.Core.Internal;
using Castle.DynamicProxy.Generators;
Expand Down Expand Up @@ -54,7 +53,7 @@ public class ModuleScope
private readonly string weakModulePath;

// Keeps track of generated types
private readonly SynchronizedDictionary<CacheKey, Type> typeCache = new SynchronizedDictionary<CacheKey, Type>();
private readonly ConcurrentDictionary<CacheKey, Type> typeCache = new ConcurrentDictionary<CacheKey, Type>();

// Used to lock the module builder creation
private readonly object moduleLocker = new object();
Expand Down Expand Up @@ -148,7 +147,7 @@ internal INamingScope NamingScope
get { return namingScope; }
}

internal SynchronizedDictionary<CacheKey, Type> TypeCache => typeCache;
internal ConcurrentDictionary<CacheKey, Type> TypeCache => typeCache;

/// <summary>
/// Gets the key pair used to sign the strong-named assembly generated by this <see cref = "ModuleScope" />.
Expand Down Expand Up @@ -504,15 +503,18 @@ private void AddCacheMappings(AssemblyBuilder builder)
{
var mappings = new Dictionary<CacheKey, string>();

typeCache.ForEach((key, value) =>
foreach (var kvp in typeCache)
{
var key = kvp.Key;
var value = kvp.Value;

// NOTE: using == returns invalid results.
// we need to use Equals here for it to work properly
if (builder.Equals(value.Assembly))
{
mappings.Add(key, value.FullName);
}
});
};

CacheMappingsAttribute.ApplyTo(builder, mappings);
}
Expand Down Expand Up @@ -551,7 +553,7 @@ public void LoadAssemblyIntoCache(Assembly assembly)

if (loadedType != null)
{
typeCache.AddOrUpdateWithoutTakingLock(mapping.Key, loadedType);
typeCache[mapping.Key] = loadedType;
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/Castle.Core/DynamicProxy/ProxyUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,19 @@
namespace Castle.DynamicProxy
{
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;

using Castle.Core.Internal;
using Castle.DynamicProxy.Generators;
using Castle.DynamicProxy.Internal;

public static class ProxyUtil
{
private static readonly SynchronizedDictionary<Assembly, bool> internalsVisibleToDynamicProxy = new SynchronizedDictionary<Assembly, bool>();
private static readonly ConcurrentDictionary<Assembly, bool> internalsVisibleToDynamicProxy = new ConcurrentDictionary<Assembly, bool>();

/// <summary>
/// Creates a delegate of the specified type to a suitable `Invoke` method
Expand Down