|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +#ifdef FEATURE_JAVAMARSHAL |
| 5 | + |
| 6 | +// Runtime headers |
| 7 | +#include "common.h" |
| 8 | +#include "gcenv.h" |
| 9 | +#include "gcenv.ee.h" |
| 10 | +#include "gcheaputilities.h" |
| 11 | +#include "gchandleutilities.h" |
| 12 | +#include "thread.h" |
| 13 | +#include "threadstore.h" |
| 14 | +#include "threadstore.inl" |
| 15 | +#include "event.h" |
| 16 | + |
| 17 | +#include "interoplibinterface.h" |
| 18 | + |
| 19 | +using CrossreferenceHandleCallback = void(__stdcall *)(MarkCrossReferencesArgs*); |
| 20 | + |
| 21 | +namespace |
| 22 | +{ |
| 23 | + volatile CrossreferenceHandleCallback g_MarkCrossReferences = NULL; |
| 24 | + |
| 25 | + Volatile<bool> g_GCBridgeActive = false; |
| 26 | + CLREventStatic g_bridgeFinished; |
| 27 | + |
| 28 | + void ReleaseGCBridgeArgumentsWorker( |
| 29 | + MarkCrossReferencesArgs* args) |
| 30 | + { |
| 31 | + _ASSERTE(args != NULL); |
| 32 | + |
| 33 | + // Memory was allocated for the collections by the GC. |
| 34 | + // See callers of GCToEEInterface::TriggerGCBridge(). |
| 35 | + |
| 36 | + // Free memory in each of the SCCs |
| 37 | + for (size_t i = 0; i < args->ComponentCount; i++) |
| 38 | + { |
| 39 | + delete[] args->Components[i].Contexts; |
| 40 | + } |
| 41 | + delete[] args->Components; |
| 42 | + delete[] args->CrossReferences; |
| 43 | + delete args; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +void JavaMarshalNative::TriggerClientBridgeProcessing( |
| 48 | + _In_ MarkCrossReferencesArgs* args) |
| 49 | +{ |
| 50 | + _ASSERTE(GCHeapUtilities::IsGCInProgress()); |
| 51 | + |
| 52 | + if (g_GCBridgeActive) |
| 53 | + { |
| 54 | + // Release the memory allocated since the GCBridge |
| 55 | + // is already running and we're not passing them to it. |
| 56 | + ReleaseGCBridgeArgumentsWorker(args); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + // Not initialized |
| 61 | + if (g_MarkCrossReferences == NULL) |
| 62 | + { |
| 63 | + // Release the memory allocated since we |
| 64 | + // don't have a GC bridge callback. |
| 65 | + ReleaseGCBridgeArgumentsWorker(args); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + g_MarkCrossReferences(args); |
| 70 | + |
| 71 | + // This runs during GC while the world is stopped, no synchronisation required |
| 72 | + g_bridgeFinished.Reset(); |
| 73 | + |
| 74 | + // Mark the GCBridge as active. |
| 75 | + g_GCBridgeActive = true; |
| 76 | +} |
| 77 | + |
| 78 | +extern "C" BOOL QCALLTYPE JavaMarshal_Initialize( |
| 79 | + void* markCrossReferences) |
| 80 | +{ |
| 81 | + _ASSERTE(markCrossReferences != NULL); |
| 82 | + |
| 83 | + BOOL success = FALSE; |
| 84 | + |
| 85 | + // Switch to Cooperative mode since we are setting callbacks that |
| 86 | + // will be used during a GC and we want to ensure a GC isn't occurring |
| 87 | + // while they are being set. |
| 88 | + Thread* pThisThread = ThreadStore::GetCurrentThreadIfAvailable(); |
| 89 | + pThisThread->DeferTransitionFrame(); |
| 90 | + pThisThread->DisablePreemptiveMode(); |
| 91 | + |
| 92 | + if (PalInterlockedCompareExchangePointer((void* volatile*)&g_MarkCrossReferences, (void*)markCrossReferences, NULL) == NULL) |
| 93 | + { |
| 94 | + success = g_bridgeFinished.CreateManualEventNoThrow(false); |
| 95 | + } |
| 96 | + |
| 97 | + pThisThread->EnablePreemptiveMode(); |
| 98 | + |
| 99 | + return success; |
| 100 | +} |
| 101 | + |
| 102 | +extern "C" void QCALLTYPE JavaMarshal_FinishCrossReferenceProcessing( |
| 103 | + MarkCrossReferencesArgs *crossReferences, |
| 104 | + size_t length, |
| 105 | + void* unreachableObjectHandles) |
| 106 | +{ |
| 107 | + _ASSERTE(crossReferences->ComponentCount >= 0); |
| 108 | + |
| 109 | + _ASSERTE(g_GCBridgeActive); |
| 110 | + |
| 111 | + // Mark the GCBridge as inactive. |
| 112 | + // This must be synchronized with the GC so switch to cooperative mode. |
| 113 | + { |
| 114 | + Thread* pThisThread = ThreadStore::GetCurrentThreadIfAvailable(); |
| 115 | + pThisThread->DeferTransitionFrame(); |
| 116 | + pThisThread->DisablePreemptiveMode(); |
| 117 | + |
| 118 | + GCHeapUtilities::GetGCHeap()->NullBridgeObjectsWeakRefs(length, unreachableObjectHandles); |
| 119 | + |
| 120 | + IGCHandleManager* pHandleManager = GCHandleUtilities::GetGCHandleManager(); |
| 121 | + OBJECTHANDLE* handles = (OBJECTHANDLE*)unreachableObjectHandles; |
| 122 | + for (size_t i = 0; i < length; i++) |
| 123 | + pHandleManager->DestroyHandleOfUnknownType(handles[i]); |
| 124 | + |
| 125 | + g_GCBridgeActive = false; |
| 126 | + g_bridgeFinished.Set(); |
| 127 | + |
| 128 | + pThisThread->EnablePreemptiveMode(); |
| 129 | + } |
| 130 | + |
| 131 | + ReleaseGCBridgeArgumentsWorker(crossReferences); |
| 132 | +} |
| 133 | + |
| 134 | +FCIMPL2(FC_BOOL_RET, GCHandle_InternalTryGetBridgeWait, OBJECTHANDLE handle, OBJECTREF* pObjResult) |
| 135 | +{ |
| 136 | + if (g_GCBridgeActive) |
| 137 | + { |
| 138 | + FC_RETURN_BOOL(false); |
| 139 | + } |
| 140 | + |
| 141 | + *pObjResult = ObjectFromHandle(handle); |
| 142 | + FC_RETURN_BOOL(true); |
| 143 | +} |
| 144 | +FCIMPLEND |
| 145 | + |
| 146 | +extern "C" void QCALLTYPE GCHandle_InternalGetBridgeWait(OBJECTHANDLE handle, OBJECTREF* pObj) |
| 147 | +{ |
| 148 | + _ASSERTE(pObj != NULL); |
| 149 | + |
| 150 | + // Transition to cooperative mode to ensure that the GC is not in progress |
| 151 | + Thread* pThisThread = ThreadStore::GetCurrentThreadIfAvailable(); |
| 152 | + pThisThread->DeferTransitionFrame(); |
| 153 | + pThisThread->DisablePreemptiveMode(); |
| 154 | + |
| 155 | + while (g_GCBridgeActive) |
| 156 | + { |
| 157 | + // This wait will transition to pre-emptive mode to wait for the bridge to finish. |
| 158 | + g_bridgeFinished.Wait(INFINITE, false, false); |
| 159 | + } |
| 160 | + |
| 161 | + // If we reach here, then the bridge has finished processing and we can be sure that |
| 162 | + // it isn't currently active. |
| 163 | + |
| 164 | + // No GC can happen between the wait and obtaining of the reference, so the |
| 165 | + // bridge processing status can't change, guaranteeing the nulling of weak refs |
| 166 | + // took place in the bridge processing finish stage. |
| 167 | + *pObj = ObjectFromHandle(handle); |
| 168 | + |
| 169 | + // Re-enable preemptive mode before we exit the QCall to ensure we're in the right GC state. |
| 170 | + pThisThread->EnablePreemptiveMode(); |
| 171 | +} |
| 172 | + |
| 173 | +#endif // FEATURE_JAVAMARSHAL |
0 commit comments