diff --git a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/__Finalizer.cs b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/__Finalizer.cs index a55b7fc040d25..4e695601f1945 100644 --- a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/__Finalizer.cs +++ b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/__Finalizer.cs @@ -39,7 +39,7 @@ public static void ProcessFinalizers() { // RhpWaitForFinalizerRequest() returned false and indicated that memory is low. We help // out by initiating a garbage collection and then go back to waiting for another request. - InternalCalls.RhCollect(-1, InternalGCCollectionMode.Blocking); + InternalCalls.RhCollect(0, InternalGCCollectionMode.Blocking, lowMemoryP: true); } } } diff --git a/src/coreclr/nativeaot/Runtime/FinalizerHelpers.cpp b/src/coreclr/nativeaot/Runtime/FinalizerHelpers.cpp index 7a4d8a853a8b7..dd9f1e096842f 100644 --- a/src/coreclr/nativeaot/Runtime/FinalizerHelpers.cpp +++ b/src/coreclr/nativeaot/Runtime/FinalizerHelpers.cpp @@ -27,6 +27,8 @@ GPTR_DECL(Thread, g_pFinalizerThread); CLREventStatic g_FinalizerEvent; CLREventStatic g_FinalizerDoneEvent; +static HANDLE g_lowMemoryNotification = NULL; + EXTERN_C void QCALLTYPE ProcessFinalizers(); // Unmanaged front-end to the finalizer thread. We require this because at the point the GC creates the @@ -76,6 +78,7 @@ bool RhInitializeFinalization() return false; if (!g_FinalizerDoneEvent.CreateManualEventNoThrow(false)) return false; + g_lowMemoryNotification = PalCreateLowMemoryResourceNotification(); // Create the finalizer thread itself. if (!PalStartFinalizerThread(FinalizerStart, (void*)g_FinalizerEvent.GetOSEvent())) @@ -132,17 +135,12 @@ EXTERN_C UInt32_BOOL QCALLTYPE RhpWaitForFinalizerRequest() // two second timeout expires. do { - HANDLE lowMemEvent = NULL; -#if 0 // TODO: hook up low memory notification - lowMemEvent = pHeap->GetLowMemoryNotificationEvent(); + HANDLE lowMemEvent = g_lowMemoryNotification; HANDLE rgWaitHandles[] = { g_FinalizerEvent.GetOSEvent(), lowMemEvent }; uint32_t cWaitHandles = (fLastEventWasLowMemory || (lowMemEvent == NULL)) ? 1 : 2; uint32_t uTimeout = fLastEventWasLowMemory ? 2000 : INFINITE; - uint32_t uResult = PalWaitForMultipleObjectsEx(cWaitHandles, rgWaitHandles, FALSE, uTimeout, FALSE); -#else - uint32_t uResult = PalWaitForSingleObjectEx(g_FinalizerEvent.GetOSEvent(), INFINITE, FALSE); -#endif + uint32_t uResult = PalCompatibleWaitAny(/*alertable=*/ FALSE, uTimeout, cWaitHandles, rgWaitHandles, /*allowReentrantWait=*/ FALSE); switch (uResult) { diff --git a/src/coreclr/nativeaot/Runtime/PalRedhawk.h b/src/coreclr/nativeaot/Runtime/PalRedhawk.h index 9257324bd1589..b047e54d6a1c6 100644 --- a/src/coreclr/nativeaot/Runtime/PalRedhawk.h +++ b/src/coreclr/nativeaot/Runtime/PalRedhawk.h @@ -706,6 +706,8 @@ REDHAWK_PALIMPORT UInt32_BOOL REDHAWK_PALAPI PalMarkThunksAsValidCallTargets( REDHAWK_PALIMPORT uint32_t REDHAWK_PALAPI PalCompatibleWaitAny(UInt32_BOOL alertable, uint32_t timeout, uint32_t count, HANDLE* pHandles, UInt32_BOOL allowReentrantWait); +REDHAWK_PALIMPORT HANDLE PalCreateLowMemoryResourceNotification(); + REDHAWK_PALIMPORT void REDHAWK_PALAPI PalAttachThread(void* thread); REDHAWK_PALIMPORT bool REDHAWK_PALAPI PalDetachThread(void* thread); diff --git a/src/coreclr/nativeaot/Runtime/unix/PalRedhawkUnix.cpp b/src/coreclr/nativeaot/Runtime/unix/PalRedhawkUnix.cpp index ac3dd24a26704..d9ec970a59649 100644 --- a/src/coreclr/nativeaot/Runtime/unix/PalRedhawkUnix.cpp +++ b/src/coreclr/nativeaot/Runtime/unix/PalRedhawkUnix.cpp @@ -1126,6 +1126,11 @@ REDHAWK_PALEXPORT uint32_t REDHAWK_PALAPI PalCompatibleWaitAny(UInt32_BOOL alert return WaitForSingleObjectEx(pHandles[0], timeout, alertable); } +REDHAWK_PALEXPORT HANDLE PalCreateLowMemoryResourceNotification() +{ + return NULL; +} + #if !__has_builtin(_mm_pause) extern "C" void _mm_pause() // Defined for implementing PalYieldProcessor in PalRedhawk.h diff --git a/src/coreclr/nativeaot/Runtime/windows/PalRedhawkMinWin.cpp b/src/coreclr/nativeaot/Runtime/windows/PalRedhawkMinWin.cpp index 94424b17562be..8baee9a84be06 100644 --- a/src/coreclr/nativeaot/Runtime/windows/PalRedhawkMinWin.cpp +++ b/src/coreclr/nativeaot/Runtime/windows/PalRedhawkMinWin.cpp @@ -312,6 +312,11 @@ REDHAWK_PALEXPORT uint32_t REDHAWK_PALAPI PalCompatibleWaitAny(UInt32_BOOL alert } } +REDHAWK_PALEXPORT HANDLE PalCreateLowMemoryResourceNotification() +{ + return CreateMemoryResourceNotification(LowMemoryResourceNotification); +} + REDHAWK_PALEXPORT void REDHAWK_PALAPI PalSleep(uint32_t milliseconds) { return Sleep(milliseconds);