diff --git a/kotlin-native/runtime/src/legacymm/cpp/Memory.cpp b/kotlin-native/runtime/src/legacymm/cpp/Memory.cpp index 088005da2b0..7ce05d4dbe3 100644 --- a/kotlin-native/runtime/src/legacymm/cpp/Memory.cpp +++ b/kotlin-native/runtime/src/legacymm/cpp/Memory.cpp @@ -3746,7 +3746,7 @@ kotlin::ThreadState kotlin::GetThreadState(MemoryState* thread) noexcept { return ThreadState::kRunnable; } -ALWAYS_INLINE kotlin::CalledFromNativeGuard::CalledFromNativeGuard() noexcept { +ALWAYS_INLINE kotlin::CalledFromNativeGuard::CalledFromNativeGuard(bool reentrant) noexcept { // no-op, used by the new MM only. } diff --git a/kotlin-native/runtime/src/main/cpp/Memory.h b/kotlin-native/runtime/src/main/cpp/Memory.h index 308316b3da1..3e4964f8a2c 100644 --- a/kotlin-native/runtime/src/main/cpp/Memory.h +++ b/kotlin-native/runtime/src/main/cpp/Memory.h @@ -447,13 +447,15 @@ private: // No-op for old GC. class CalledFromNativeGuard final : private Pinned { public: - ALWAYS_INLINE CalledFromNativeGuard() noexcept; + ALWAYS_INLINE CalledFromNativeGuard(bool reentrant = false) noexcept; ~CalledFromNativeGuard() noexcept { - SwitchThreadState(thread_, ThreadState::kNative); + SwitchThreadState(thread_, oldState_, reentrant_); } private: MemoryState* thread_; + ThreadState oldState_; + bool reentrant_; }; template diff --git a/kotlin-native/runtime/src/mm/cpp/Memory.cpp b/kotlin-native/runtime/src/mm/cpp/Memory.cpp index 7c864c97558..c33d51c9172 100644 --- a/kotlin-native/runtime/src/mm/cpp/Memory.cpp +++ b/kotlin-native/runtime/src/mm/cpp/Memory.cpp @@ -532,10 +532,10 @@ MemoryState* kotlin::mm::GetMemoryState() { return ToMemoryState(ThreadRegistry::Instance().CurrentThreadDataNode()); } -ALWAYS_INLINE kotlin::CalledFromNativeGuard::CalledFromNativeGuard() noexcept { +ALWAYS_INLINE kotlin::CalledFromNativeGuard::CalledFromNativeGuard(bool reentrant) noexcept : reentrant_(reentrant) { Kotlin_initRuntimeIfNeeded(); thread_ = mm::GetMemoryState(); - SwitchThreadState(thread_, ThreadState::kRunnable); + oldState_ = SwitchThreadState(thread_, ThreadState::kRunnable, reentrant_); } const bool kotlin::kSupportsMultipleMutators = kotlin::gc::kSupportsMultipleMutators;