-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Implement GC bridge VM support in NativeAOT #117738
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4b84e8b
Implement GC bridge in NativeAOT
jkoritzinsky af2605d
Move fetching value from gchandle to by synchronized with GC bridge
jkoritzinsky fd77d07
Don't treat pointer types as COM in the linker
jkoritzinsky 5876847
Merge branch 'main' of https://github.com/dotnet/runtime into gcbridg…
jkoritzinsky 8379bc7
PR feedback
jkoritzinsky a4fc32d
Merge branch 'main' of https://github.com/dotnet/runtime into gcbridg…
jkoritzinsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
src/coreclr/nativeaot/Runtime/interoplibinterface_java.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#ifdef FEATURE_JAVAMARSHAL | ||
|
||
// Runtime headers | ||
#include "common.h" | ||
#include "gcenv.h" | ||
#include "gcenv.ee.h" | ||
#include "gcheaputilities.h" | ||
#include "gchandleutilities.h" | ||
#include "thread.h" | ||
#include "threadstore.h" | ||
#include "threadstore.inl" | ||
#include "event.h" | ||
|
||
#include "interoplibinterface.h" | ||
|
||
using CrossreferenceHandleCallback = void(__stdcall *)(MarkCrossReferencesArgs*); | ||
|
||
namespace | ||
{ | ||
volatile CrossreferenceHandleCallback g_MarkCrossReferences = NULL; | ||
|
||
Volatile<bool> g_GCBridgeActive = false; | ||
CLREventStatic g_bridgeFinished; | ||
|
||
void ReleaseGCBridgeArgumentsWorker( | ||
MarkCrossReferencesArgs* args) | ||
{ | ||
_ASSERTE(args != NULL); | ||
|
||
// Memory was allocated for the collections by the GC. | ||
jkoritzinsky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// See callers of GCToEEInterface::TriggerGCBridge(). | ||
|
||
// Free memory in each of the SCCs | ||
for (size_t i = 0; i < args->ComponentCount; i++) | ||
{ | ||
delete[] args->Components[i].Contexts; | ||
} | ||
delete[] args->Components; | ||
delete[] args->CrossReferences; | ||
delete args; | ||
} | ||
} | ||
|
||
void JavaMarshalNative::TriggerClientBridgeProcessing( | ||
_In_ MarkCrossReferencesArgs* args) | ||
{ | ||
_ASSERTE(GCHeapUtilities::IsGCInProgress()); | ||
|
||
if (g_GCBridgeActive) | ||
{ | ||
// Release the memory allocated since the GCBridge | ||
// is already running and we're not passing them to it. | ||
ReleaseGCBridgeArgumentsWorker(args); | ||
return; | ||
} | ||
|
||
// Not initialized | ||
if (g_MarkCrossReferences == NULL) | ||
{ | ||
// Release the memory allocated since we | ||
// don't have a GC bridge callback. | ||
ReleaseGCBridgeArgumentsWorker(args); | ||
return; | ||
} | ||
|
||
g_MarkCrossReferences(args); | ||
|
||
// This runs during GC while the world is stopped, no synchronisation required | ||
g_bridgeFinished.Reset(); | ||
|
||
// Mark the GCBridge as active. | ||
g_GCBridgeActive = true; | ||
} | ||
|
||
extern "C" BOOL QCALLTYPE JavaMarshal_Initialize( | ||
void* markCrossReferences) | ||
{ | ||
_ASSERTE(markCrossReferences != NULL); | ||
|
||
BOOL success = FALSE; | ||
|
||
jkoritzinsky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Switch to Cooperative mode since we are setting callbacks that | ||
// will be used during a GC and we want to ensure a GC isn't occurring | ||
// while they are being set. | ||
Thread* pThisThread = ThreadStore::GetCurrentThreadIfAvailable(); | ||
pThisThread->DeferTransitionFrame(); | ||
pThisThread->DisablePreemptiveMode(); | ||
|
||
if (PalInterlockedCompareExchangePointer((void* volatile*)&g_MarkCrossReferences, (void*)markCrossReferences, NULL) == NULL) | ||
{ | ||
success = g_bridgeFinished.CreateManualEventNoThrow(false); | ||
} | ||
|
||
pThisThread->EnablePreemptiveMode(); | ||
|
||
return success; | ||
} | ||
|
||
extern "C" void QCALLTYPE JavaMarshal_FinishCrossReferenceProcessing( | ||
MarkCrossReferencesArgs *crossReferences, | ||
size_t length, | ||
void* unreachableObjectHandles) | ||
{ | ||
_ASSERTE(crossReferences->ComponentCount >= 0); | ||
|
||
_ASSERTE(g_GCBridgeActive); | ||
|
||
// Mark the GCBridge as inactive. | ||
// This must be synchronized with the GC so switch to cooperative mode. | ||
{ | ||
Thread* pThisThread = ThreadStore::GetCurrentThreadIfAvailable(); | ||
pThisThread->DeferTransitionFrame(); | ||
pThisThread->DisablePreemptiveMode(); | ||
|
||
GCHeapUtilities::GetGCHeap()->NullBridgeObjectsWeakRefs(length, unreachableObjectHandles); | ||
|
||
IGCHandleManager* pHandleManager = GCHandleUtilities::GetGCHandleManager(); | ||
OBJECTHANDLE* handles = (OBJECTHANDLE*)unreachableObjectHandles; | ||
for (size_t i = 0; i < length; i++) | ||
pHandleManager->DestroyHandleOfUnknownType(handles[i]); | ||
|
||
g_GCBridgeActive = false; | ||
g_bridgeFinished.Set(); | ||
|
||
pThisThread->EnablePreemptiveMode(); | ||
} | ||
|
||
ReleaseGCBridgeArgumentsWorker(crossReferences); | ||
} | ||
|
||
FCIMPL2(FC_BOOL_RET, GCHandle_InternalTryGetBridgeWait, OBJECTHANDLE handle, OBJECTREF* pObjResult) | ||
{ | ||
if (g_GCBridgeActive) | ||
{ | ||
FC_RETURN_BOOL(false); | ||
} | ||
|
||
*pObjResult = ObjectFromHandle(handle); | ||
FC_RETURN_BOOL(true); | ||
} | ||
FCIMPLEND | ||
|
||
extern "C" void QCALLTYPE GCHandle_InternalGetBridgeWait(OBJECTHANDLE handle, OBJECTREF* pObj) | ||
{ | ||
_ASSERTE(pObj != NULL); | ||
|
||
// Transition to cooperative mode to ensure that the GC is not in progress | ||
jkoritzinsky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Thread* pThisThread = ThreadStore::GetCurrentThreadIfAvailable(); | ||
pThisThread->DeferTransitionFrame(); | ||
pThisThread->DisablePreemptiveMode(); | ||
|
||
while (g_GCBridgeActive) | ||
{ | ||
// This wait will transition to pre-emptive mode to wait for the bridge to finish. | ||
jkotas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
g_bridgeFinished.Wait(INFINITE, false, false); | ||
} | ||
|
||
// If we reach here, then the bridge has finished processing and we can be sure that | ||
// it isn't currently active. | ||
|
||
// No GC can happen between the wait and obtaining of the reference, so the | ||
// bridge processing status can't change, guaranteeing the nulling of weak refs | ||
// took place in the bridge processing finish stage. | ||
*pObj = ObjectFromHandle(handle); | ||
|
||
// Re-enable preemptive mode before we exit the QCall to ensure we're in the right GC state. | ||
pThisThread->EnablePreemptiveMode(); | ||
} | ||
|
||
#endif // FEATURE_JAVAMARSHAL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.