Skip to content

Commit

Permalink
Merge pull request #11 from curit/pull-request
Browse files Browse the repository at this point in the history
add lock before accessing dictionary
  • Loading branch information
jbtule committed Apr 21, 2016
2 parents fafba61 + 852b45b commit 366b3bd
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions Dynamitey/Internal/Optimization/InvokeHelper-Regular.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ internal static IEnumerable<CSharpArgumentInfo> GetBindingArgumentList(object[]
internal static readonly IDictionary<Type, CallSite<DynamicCreateCallSite>>
_dynamicInvokeCreateCallSite = new Dictionary<Type, CallSite<DynamicCreateCallSite>>();

private static readonly object _callSiteCacheLock = new object();

internal static CallSite CreateCallSite(
Type delegateType,
Type specificBinderType,
Expand All @@ -239,7 +241,15 @@ internal static CallSite CreateCallSite(
)
{
CallSite<DynamicCreateCallSite> tSite;
if (!_dynamicInvokeCreateCallSite.TryGetValue(delegateType, out tSite))

bool foundInCache;

lock (_callSiteCacheLock)
{
foundInCache = _dynamicInvokeCreateCallSite.TryGetValue(delegateType, out tSite);
}

if (!foundInCache)
{
tSite = CallSite<DynamicCreateCallSite>.Create(
Binder.InvokeMember(
Expand All @@ -262,7 +272,15 @@ internal static CallSite CreateCallSite(
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null), //isevent
}
));
_dynamicInvokeCreateCallSite[delegateType] = tSite;

lock (_callSiteCacheLock)
{
// another thread might have been faster; add to dictionary only if we are the first
if (!_dynamicInvokeCreateCallSite.ContainsKey(delegateType))
{
_dynamicInvokeCreateCallSite[delegateType] = tSite;
}
}
}
return (CallSite)tSite.Target(tSite, typeof(InvokeHelper), specificBinderType, knownType, binder, name, context, argNames, staticContext, isEvent);
}
Expand Down

0 comments on commit 366b3bd

Please sign in to comment.