From c4992f19129cb8d3cc344b1075303c312f7a9fd9 Mon Sep 17 00:00:00 2001 From: Alexander Shabalin Date: Thu, 30 Nov 2023 13:03:48 +0100 Subject: [PATCH] [K/N] Move AutoreleasePool into objc_support ^KT-63423 --- .../src/gc/common/cpp/FinalizerProcessor.hpp | 8 ++--- .../runtime/src/main/cpp/ObjCInterop.mm | 8 ----- .../runtime/src/main/cpp/ObjCMMAPI.h | 12 -------- kotlin-native/runtime/src/main/cpp/Worker.cpp | 11 +++---- .../main/cpp/objc_support/AutoreleasePool.cpp | 29 +++++++++++++++++++ .../main/cpp/objc_support/AutoreleasePool.hpp | 23 +++++++++++++++ 6 files changed, 59 insertions(+), 32 deletions(-) create mode 100644 kotlin-native/runtime/src/main/cpp/objc_support/AutoreleasePool.cpp create mode 100644 kotlin-native/runtime/src/main/cpp/objc_support/AutoreleasePool.hpp diff --git a/kotlin-native/runtime/src/gc/common/cpp/FinalizerProcessor.hpp b/kotlin-native/runtime/src/gc/common/cpp/FinalizerProcessor.hpp index 252d8ddc474..473a05c1460 100644 --- a/kotlin-native/runtime/src/gc/common/cpp/FinalizerProcessor.hpp +++ b/kotlin-native/runtime/src/gc/common/cpp/FinalizerProcessor.hpp @@ -17,9 +17,9 @@ #include "ScopedThread.hpp" #include "Utils.hpp" #include "Logging.hpp" +#include "objc_support/AutoreleasePool.hpp" #if KONAN_OBJC_INTEROP -#include "ObjCMMAPI.h" #include #include #endif @@ -100,9 +100,7 @@ private: void processSingle(FinalizerQueue&& queue, int64_t currentEpoch) noexcept { if (!FinalizerQueueTraits::isEmpty(queue)) { -#if KONAN_OBJC_INTEROP - konan::AutoreleasePool autoreleasePool; -#endif + objc_support::AutoreleasePool autoreleasePool; ThreadStateGuard guard(ThreadState::kRunnable); FinalizerQueueTraits::process(std::move(queue)); } @@ -150,7 +148,7 @@ private: } void body() override { - konan::AutoreleasePool autoreleasePool; + objc_support::AutoreleasePool autoreleasePool; auto mode = kCFRunLoopDefaultMode; CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource_, mode); diff --git a/kotlin-native/runtime/src/main/cpp/ObjCInterop.mm b/kotlin-native/runtime/src/main/cpp/ObjCInterop.mm index 42476a023ea..687999cb379 100644 --- a/kotlin-native/runtime/src/main/cpp/ObjCInterop.mm +++ b/kotlin-native/runtime/src/main/cpp/ObjCInterop.mm @@ -335,14 +335,6 @@ id objc_allocWithZone(Class clazz); id objc_retain(id ptr); void objc_release(id ptr); -konan::AutoreleasePool::AutoreleasePool() - : handle(objc_autoreleasePoolPush()) {} - -konan::AutoreleasePool::~AutoreleasePool() { - kotlin::ThreadStateGuard guard(kotlin::ThreadState::kNative, true); - objc_autoreleasePoolPop(handle); -} - void* Kotlin_objc_autoreleasePoolPush() { return objc_autoreleasePoolPush(); } diff --git a/kotlin-native/runtime/src/main/cpp/ObjCMMAPI.h b/kotlin-native/runtime/src/main/cpp/ObjCMMAPI.h index 3478e0b0e68..2e7d00c95c9 100644 --- a/kotlin-native/runtime/src/main/cpp/ObjCMMAPI.h +++ b/kotlin-native/runtime/src/main/cpp/ObjCMMAPI.h @@ -7,23 +7,11 @@ #define RUNTIME_OBJCMMAPI_H #include "Common.h" -#include "Utils.hpp" #if KONAN_OBJC_INTEROP extern "C" ALWAYS_INLINE void Kotlin_ObjCExport_releaseAssociatedObject(void* associatedObject); -namespace konan { -class AutoreleasePool : private kotlin::Pinned { - public: - AutoreleasePool(); - ~AutoreleasePool(); - - private: - void* handle; -}; -} // namespace konan - #endif // KONAN_OBJC_INTEROP #endif // RUNTIME_OBJCMMAPI_H diff --git a/kotlin-native/runtime/src/main/cpp/Worker.cpp b/kotlin-native/runtime/src/main/cpp/Worker.cpp index aaca9156c3f..3c6086091a5 100644 --- a/kotlin-native/runtime/src/main/cpp/Worker.cpp +++ b/kotlin-native/runtime/src/main/cpp/Worker.cpp @@ -14,6 +14,7 @@ * limitations under the License. */ +#include #include #include #include @@ -29,10 +30,10 @@ #include "KAssert.h" #include "Memory.h" #include "Natives.h" -#include "ObjCMMAPI.h" #include "Runtime.h" #include "Types.h" #include "Worker.h" +#include "objc_support/AutoreleasePool.hpp" using namespace kotlin; @@ -1036,9 +1037,7 @@ JobKind Worker::processQueueElement(bool blocking) { ObjHolder operationHolder, dummyHolder; KRef obj = DerefStablePointer(job.executeAfter.operation, operationHolder.slot()); try { - #if KONAN_OBJC_INTEROP - konan::AutoreleasePool autoreleasePool; - #endif + objc_support::AutoreleasePool autoreleasePool; WorkerLaunchpad(obj, dummyHolder.slot()); } catch(ExceptionObjHolder& e) { switch (exceptionHandling()) { @@ -1062,9 +1061,7 @@ JobKind Worker::processQueueElement(bool blocking) { ObjHolder resultHolder; KRef argument = AdoptStablePointer(job.regularJob.argument, argumentHolder.slot()); try { - #if KONAN_OBJC_INTEROP - konan::AutoreleasePool autoreleasePool; - #endif + objc_support::AutoreleasePool autoreleasePool; { CurrentFrameGuard guard; job.regularJob.function(argument, resultHolder.slot()); diff --git a/kotlin-native/runtime/src/main/cpp/objc_support/AutoreleasePool.cpp b/kotlin-native/runtime/src/main/cpp/objc_support/AutoreleasePool.cpp new file mode 100644 index 00000000000..9d31d46d981 --- /dev/null +++ b/kotlin-native/runtime/src/main/cpp/objc_support/AutoreleasePool.cpp @@ -0,0 +1,29 @@ +/* + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + +#include "AutoreleasePool.hpp" +#include "Memory.h" + +using namespace kotlin; + +#if KONAN_OBJC_INTEROP + +// Obj-C runtime provided by clang. +extern "C" void* objc_autoreleasePoolPush(void); +extern "C" void objc_autoreleasePoolPop(void*); + +objc_support::AutoreleasePool::AutoreleasePool() noexcept : handle_(objc_autoreleasePoolPush()) {} + +objc_support::AutoreleasePool::~AutoreleasePool() { + NativeOrUnregisteredThreadGuard guard(/* reentrant = */ true); + objc_autoreleasePoolPop(handle_); +} + +#else + +objc_support::AutoreleasePool::AutoreleasePool() noexcept = default; +objc_support::AutoreleasePool::~AutoreleasePool() = default; + +#endif \ No newline at end of file diff --git a/kotlin-native/runtime/src/main/cpp/objc_support/AutoreleasePool.hpp b/kotlin-native/runtime/src/main/cpp/objc_support/AutoreleasePool.hpp new file mode 100644 index 00000000000..92f081949c9 --- /dev/null +++ b/kotlin-native/runtime/src/main/cpp/objc_support/AutoreleasePool.hpp @@ -0,0 +1,23 @@ +/* + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + +#pragma once + +#include "Utils.hpp" + +namespace kotlin::objc_support { + +class AutoreleasePool : private Pinned { +public: + AutoreleasePool() noexcept; + ~AutoreleasePool(); + +private: +#if KONAN_OBJC_INTEROP + void* handle_; +#endif +}; + +} // namespace kotlin::objc_support \ No newline at end of file