Skip to content

Commit

Permalink
Add debug entries for scheduler creation and usage
Browse files Browse the repository at this point in the history
  • Loading branch information
clementetb committed Oct 22, 2023
1 parent 3c8ea7a commit 1ec6d03
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<RealmSchedulerT>(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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public open class ConfigurationImpl(

override suspend fun openRealm(realm: RealmImpl): Triple<FrozenRealmReference, Boolean, RealmSchedulerPointer> {
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
}
Expand All @@ -56,7 +57,7 @@ public fun interface CoroutineDispatcherFactory {
*/
public fun unmanaged(dispatcher: CoroutineDispatcher): CoroutineDispatcherFactory {
return CoroutineDispatcherFactory {
UnmanagedDispatcherHolder(dispatcher)
UnmanagedDispatcherHolder(dispatcher, "unmanaged")
}
}
}
Expand Down Expand Up @@ -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.
Expand All @@ -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
}
Expand All @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 1ec6d03

Please sign in to comment.