Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify ldftn reverse lookups #88719

Merged
merged 5 commits into from
Jul 14, 2023
Merged
Changes from 1 commit
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 @@ -566,26 +566,22 @@ public bool TryGetOffsetsRange(IntPtr functionPointer, out int firstParserOffset
// ldftn reverse lookup hash. Must be cleared and reset if the module list changes. (All sets to
// this variable must happen under a lock)
private volatile KeyValuePair<NativeFormatModuleInfo, FunctionPointersToOffsets>[] _ldftnReverseLookup_InvokeMap;
private Func<NativeFormatModuleInfo, FunctionPointersToOffsets> _computeLdFtnLookupInvokeMapInvokeMap = ComputeLdftnReverseLookup_InvokeMap;

/// <summary>
/// Initialize a lookup array of module to function pointer/parser offset pair arrays. Do so in a manner that will allow
MichalStrehovsky marked this conversation as resolved.
Show resolved Hide resolved
/// future work which will invalidate the cache (by setting it to null)
/// </summary>
/// <param name="ldftnReverseLookupStatic">pointer to static which holds cache value. This is treated as a volatile variable</param>
/// <param name="lookupComputer"></param>
/// <returns></returns>
private KeyValuePair<NativeFormatModuleInfo, FunctionPointersToOffsets>[] GetLdFtnReverseLookups_Helper(ref KeyValuePair<NativeFormatModuleInfo, FunctionPointersToOffsets>[] ldftnReverseLookupStatic, Func<NativeFormatModuleInfo, FunctionPointersToOffsets> lookupComputer)
private KeyValuePair<NativeFormatModuleInfo, FunctionPointersToOffsets>[] GetLdFtnReverseLookups_InvokeMap()
{
KeyValuePair<NativeFormatModuleInfo, FunctionPointersToOffsets>[] ldFtnReverseLookup = Volatile.Read(ref ldftnReverseLookupStatic);
KeyValuePair<NativeFormatModuleInfo, FunctionPointersToOffsets>[] ldFtnReverseLookup = _ldftnReverseLookup_InvokeMap;

if (ldFtnReverseLookup != null)
return ldFtnReverseLookup;
else
{
lock (this)
{
ldFtnReverseLookup = Volatile.Read(ref ldftnReverseLookupStatic);
ldFtnReverseLookup = _ldftnReverseLookup_InvokeMap;

// double checked lock, safe due to use of volatile on s_ldftnReverseHashes
MichalStrehovsky marked this conversation as resolved.
Show resolved Hide resolved
if (ldFtnReverseLookup != null)
Expand All @@ -612,7 +608,7 @@ private KeyValuePair<NativeFormatModuleInfo, FunctionPointersToOffsets>[] GetLdF
break;
}

ldFtnReverseLookup[index] = new KeyValuePair<NativeFormatModuleInfo, FunctionPointersToOffsets>(module, lookupComputer(module));
ldFtnReverseLookup[index] = new KeyValuePair<NativeFormatModuleInfo, FunctionPointersToOffsets>(module, ComputeLdftnReverseLookup_InvokeMap(module));
index++;
}

Expand All @@ -623,19 +619,12 @@ private KeyValuePair<NativeFormatModuleInfo, FunctionPointersToOffsets>[] GetLdF
break;
}

Volatile.Write(ref ldftnReverseLookupStatic, ldFtnReverseLookup);
_ldftnReverseLookup_InvokeMap = ldFtnReverseLookup;
return ldFtnReverseLookup;
}
}
}

private KeyValuePair<NativeFormatModuleInfo, FunctionPointersToOffsets>[] GetLdFtnReverseLookups_InvokeMap()
{
#pragma warning disable 0420 // GetLdFtnReverseLookups_Helper treats its first parameter as volatile by using explicit Volatile operations
return GetLdFtnReverseLookups_Helper(ref _ldftnReverseLookup_InvokeMap, _computeLdFtnLookupInvokeMapInvokeMap);
#pragma warning restore 0420
}

internal unsafe void GetFunctionPointerAndInstantiationArgumentForOriginalLdFtnResult(IntPtr originalLdFtnResult, out IntPtr canonOriginalLdFtnResult, out IntPtr instantiationArgument)
{
if (FunctionPointerOps.IsGenericMethodPointer(originalLdFtnResult))
Expand Down
Loading