Skip to content

Commit

Permalink
Merge pull request #383 from stakx/thread-synchronization
Browse files Browse the repository at this point in the history
Synchronize read/write accesses to `NamingScope.GetUniqueName`
  • Loading branch information
Gavin van der Merwe authored Jun 18, 2018
2 parents 6959806 + bcba394 commit 886e764
Showing 1 changed file with 4 additions and 7 deletions.
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.
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

0 comments on commit 886e764

Please sign in to comment.