Skip to content
Merged
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
11 changes: 4 additions & 7 deletions src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,9 @@ protected Type ObtainProxyType(CacheKey cacheKey, Func<string, INamingScope, Typ
}

// This is to avoid generating duplicate types under heavy multithreaded load.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, you could probably move the comment 'This is to avoid generating duplicate types under heavy multithreaded load' to just after 'See if an earlier lock holder populated the cache.'

using (var locker = Scope.Lock.ForReadingUpgradeable())
using (var locker = Scope.Lock.ForWriting())
{
// Only one thread at a time may enter an upgradable read lock.
// Only one thread at a time may enter a write lock.
// See if an earlier lock holder populated the cache.
cacheType = GetFromCache(cacheKey);
if (cacheType != null)
Expand All @@ -414,11 +414,8 @@ protected Type ObtainProxyType(CacheKey cacheKey, Func<string, INamingScope, Typ
var name = Scope.NamingScope.GetUniqueName("Castle.Proxies." + targetType.Name + "Proxy");
var proxyType = factory.Invoke(name, Scope.NamingScope.SafeSubScope());

// Upgrade the lock to a write lock.
using (locker.Upgrade())
{
AddToCache(cacheKey, proxyType);
}
AddToCache(cacheKey, proxyType);

return proxyType;
}
}
Expand Down