diff --git a/packages/cinterop/src/commonMain/kotlin/io/realm/kotlin/internal/interop/RealmInterop.kt b/packages/cinterop/src/commonMain/kotlin/io/realm/kotlin/internal/interop/RealmInterop.kt index 1cd6c153c9..7594a40d70 100644 --- a/packages/cinterop/src/commonMain/kotlin/io/realm/kotlin/internal/interop/RealmInterop.kt +++ b/packages/cinterop/src/commonMain/kotlin/io/realm/kotlin/internal/interop/RealmInterop.kt @@ -185,8 +185,12 @@ expect object RealmInterop { fun realm_config_set_in_memory(config: RealmConfigurationPointer, inMemory: Boolean) fun realm_schema_validate(schema: RealmSchemaPointer, mode: SchemaValidationMode): Boolean - fun realm_create_scheduler(): RealmSchedulerPointer - fun realm_create_scheduler(dispatcher: CoroutineDispatcher): RealmSchedulerPointer + fun realm_create_scheduler(name: String = "no-name"): RealmSchedulerPointer + fun realm_create_scheduler( + dispatcher: CoroutineDispatcher, + name: String = "no-name" + ): RealmSchedulerPointer + /** * Open a realm on the current thread. * diff --git a/packages/cinterop/src/jvm/kotlin/io/realm/kotlin/internal/interop/RealmInterop.kt b/packages/cinterop/src/jvm/kotlin/io/realm/kotlin/internal/interop/RealmInterop.kt index 14674de642..bdb81d4cd1 100644 --- a/packages/cinterop/src/jvm/kotlin/io/realm/kotlin/internal/interop/RealmInterop.kt +++ b/packages/cinterop/src/jvm/kotlin/io/realm/kotlin/internal/interop/RealmInterop.kt @@ -210,11 +210,22 @@ actual object RealmInterop { realmc.realm_config_set_in_memory(config.cptr(), inMemory) } - actual fun realm_create_scheduler(): RealmSchedulerPointer = - LongPointerWrapper(realmc.realm_create_generic_scheduler()) + actual fun realm_create_scheduler(name: String): RealmSchedulerPointer = + LongPointerWrapper( + realmc.realm_create_generic_scheduler().also { + println("Scheduler $name pointer : $it") + } + ) - actual fun realm_create_scheduler(dispatcher: CoroutineDispatcher): RealmSchedulerPointer = - LongPointerWrapper(realmc.realm_create_scheduler(JVMScheduler(dispatcher))) + actual fun realm_create_scheduler( + dispatcher: CoroutineDispatcher, + name: String, + ): RealmSchedulerPointer = + LongPointerWrapper( + realmc.realm_create_scheduler(JVMScheduler(dispatcher)).also { + println("Scheduler $name pointer: $it") + } + ) actual fun realm_open( config: RealmConfigurationPointer, @@ -2144,6 +2155,7 @@ private class JVMScheduler(dispatcher: CoroutineDispatcher) { val scope: CoroutineScope = CoroutineScope(dispatcher) fun notifyCore(schedulerPointer: Long) { + println("Scheduler invoke: $schedulerPointer") scope.launch { realmc.invoke_core_notify_callback(schedulerPointer) } diff --git a/packages/cinterop/src/nativeDarwin/kotlin/io/realm/kotlin/internal/interop/RealmInterop.kt b/packages/cinterop/src/nativeDarwin/kotlin/io/realm/kotlin/internal/interop/RealmInterop.kt index b33d0cbe22..1aa493d956 100644 --- a/packages/cinterop/src/nativeDarwin/kotlin/io/realm/kotlin/internal/interop/RealmInterop.kt +++ b/packages/cinterop/src/nativeDarwin/kotlin/io/realm/kotlin/internal/interop/RealmInterop.kt @@ -541,14 +541,14 @@ actual object RealmInterop { return Pair(realmPtr, fileCreated.value) } - actual fun realm_create_scheduler(): RealmSchedulerPointer { + actual fun realm_create_scheduler(name: String): RealmSchedulerPointer { // If there is no notification dispatcher use the default scheduler. // Re-verify if this is actually needed when notification scheduler is fully in place. val scheduler = checkedPointerResult(realm_wrapper.realm_scheduler_make_default()) return CPointerWrapper(scheduler) } - actual fun realm_create_scheduler(dispatcher: CoroutineDispatcher): RealmSchedulerPointer { + actual fun realm_create_scheduler(dispatcher: CoroutineDispatcher, name: String): RealmSchedulerPointer { printlntid("createSingleThreadDispatcherScheduler") val scheduler = SingleThreadDispatcherScheduler(tid(), dispatcher) diff --git a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/Realm.kt b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/Realm.kt index ca26a0f362..cd75d1e5fa 100644 --- a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/Realm.kt +++ b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/Realm.kt @@ -120,7 +120,7 @@ public interface Realm : TypedRealm { if (!fileExists(configuration.path)) return false val config = (configuration as InternalConfiguration) - return RealmInterop.realm_create_scheduler() + return RealmInterop.realm_create_scheduler("compact-realm") .use { scheduler -> val (dbPointer, _) = RealmInterop.realm_open( config = config.createNativeConfiguration(), diff --git a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/ConfigurationImpl.kt b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/ConfigurationImpl.kt index a031171541..0ed164c9ba 100644 --- a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/ConfigurationImpl.kt +++ b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/ConfigurationImpl.kt @@ -109,7 +109,7 @@ public open class ConfigurationImpl( override suspend fun openRealm(realm: RealmImpl): Triple { val configPtr = realm.configuration.createNativeConfiguration() - val scheduler = RealmInterop.realm_create_scheduler() + val scheduler = RealmInterop.realm_create_scheduler("configurationImpl") val (dbPointer, fileCreated) = RealmInterop.realm_open(configPtr, scheduler) val liveRealmReference = LiveRealmReference(realm, dbPointer) val frozenReference = liveRealmReference.snapshot(realm) diff --git a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/util/CoroutineDispatcherFactory.kt b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/util/CoroutineDispatcherFactory.kt index 370af4d31f..93c542b9f4 100644 --- a/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/util/CoroutineDispatcherFactory.kt +++ b/packages/library-base/src/commonMain/kotlin/io/realm/kotlin/internal/util/CoroutineDispatcherFactory.kt @@ -42,10 +42,11 @@ public fun interface CoroutineDispatcherFactory { public fun managed(name: String, threads: Int = 1): CoroutineDispatcherFactory { return CoroutineDispatcherFactory { ManagedDispatcherHolder( - when (threads) { + dispatcher = when (threads) { 1 -> singleThreadDispatcher(name) else -> multiThreadDispatcher(threads) - } + }, + name = name ) } } @@ -56,7 +57,7 @@ public fun interface CoroutineDispatcherFactory { */ public fun unmanaged(dispatcher: CoroutineDispatcher): CoroutineDispatcherFactory { return CoroutineDispatcherFactory { - UnmanagedDispatcherHolder(dispatcher) + UnmanagedDispatcherHolder(dispatcher, "unmanaged") } } } @@ -85,6 +86,8 @@ public sealed interface DispatcherHolder { */ public val dispatcher: CoroutineDispatcher + public val name: String + /** * Mark the dispatcher as no longer being used. For internal dispatchers, this will also * close them. @@ -95,12 +98,14 @@ public sealed interface DispatcherHolder { @OptIn(ExperimentalCoroutinesApi::class) private class ManagedDispatcherHolder( override val dispatcher: CloseableCoroutineDispatcher, + override val name: String, ) : DispatcherHolder { override fun close(): Unit = dispatcher.close() } private class UnmanagedDispatcherHolder( - override val dispatcher: CoroutineDispatcher + override val dispatcher: CoroutineDispatcher, + override val name: String, ) : DispatcherHolder { override fun close(): Unit = Unit } @@ -115,7 +120,7 @@ public class LiveRealmContext( public val scheduler: RealmSchedulerPointer = runBlocking { withContext(dispatcherHolder.dispatcher) { - RealmInterop.realm_create_scheduler(dispatcher) + RealmInterop.realm_create_scheduler(dispatcher, dispatcherHolder.name) } } diff --git a/packages/test-base/src/commonMain/kotlin/io/realm/kotlin/test/StandaloneDynamicMutableRealm.kt b/packages/test-base/src/commonMain/kotlin/io/realm/kotlin/test/StandaloneDynamicMutableRealm.kt index 5784b90573..28f04e627a 100644 --- a/packages/test-base/src/commonMain/kotlin/io/realm/kotlin/test/StandaloneDynamicMutableRealm.kt +++ b/packages/test-base/src/commonMain/kotlin/io/realm/kotlin/test/StandaloneDynamicMutableRealm.kt @@ -45,7 +45,7 @@ internal class StandaloneDynamicMutableRealm private constructor( ) { constructor(configuration: InternalConfiguration) : this( configuration, - RealmInterop.realm_create_scheduler() + RealmInterop.realm_create_scheduler("dynamic-standalone") ) override fun close() {