From 9114630bbaec9020de897d970d7da1e2e739ae8f Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Mon, 1 Aug 2022 16:23:41 +0100 Subject: [PATCH] replacing single context thread with semaphore - avoids the need for a dedicated long living thread instance --- .../java/im/vector/app/core/di/SessionInitializer.kt | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/vector/src/main/java/im/vector/app/core/di/SessionInitializer.kt b/vector/src/main/java/im/vector/app/core/di/SessionInitializer.kt index c800ae5118..e1d4844940 100644 --- a/vector/src/main/java/im/vector/app/core/di/SessionInitializer.kt +++ b/vector/src/main/java/im/vector/app/core/di/SessionInitializer.kt @@ -16,15 +16,13 @@ package im.vector.app.core.di -import kotlinx.coroutines.DelicateCoroutinesApi -import kotlinx.coroutines.newSingleThreadContext -import kotlinx.coroutines.withContext +import kotlinx.coroutines.sync.Semaphore +import kotlinx.coroutines.sync.withPermit import org.matrix.android.sdk.api.auth.AuthenticationService import org.matrix.android.sdk.api.session.Session import javax.inject.Inject -@OptIn(DelicateCoroutinesApi::class) -private val INITIALIZER_CONTEXT = newSingleThreadContext("session-initializer") +private val initializerSemaphore = Semaphore(permits = 1) class SessionInitializer @Inject constructor( private val authenticationService: AuthenticationService, @@ -38,7 +36,7 @@ class SessionInitializer @Inject constructor( * @return the initialized Session or null when no authenticated sessions are available. */ suspend fun tryInitialize(readCurrentSession: () -> Session?, initializer: (Session) -> Unit): Session? { - return withContext(INITIALIZER_CONTEXT) { + return initializerSemaphore.withPermit { val currentInMemorySession = readCurrentSession() when { currentInMemorySession != null -> currentInMemorySession