From 100a6569b71de46c93170edb651b2f5d20e26647 Mon Sep 17 00:00:00 2001 From: Dude so hot Date: Tue, 15 Oct 2024 13:18:15 +0800 Subject: [PATCH] Use rwlock --- .../com/hippo/ehviewer/coil/QrCodeInterceptor.kt | 7 +++++-- .../com/hippo/ehviewer/gallery/PageLoader.kt | 14 +++++++++----- app/src/main/kotlin/moe/tarsin/kt/Sync.kt | 3 --- 3 files changed, 14 insertions(+), 10 deletions(-) delete mode 100644 app/src/main/kotlin/moe/tarsin/kt/Sync.kt diff --git a/app/src/main/kotlin/com/hippo/ehviewer/coil/QrCodeInterceptor.kt b/app/src/main/kotlin/com/hippo/ehviewer/coil/QrCodeInterceptor.kt index ffa226ebe..c704c032d 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/coil/QrCodeInterceptor.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/coil/QrCodeInterceptor.kt @@ -12,8 +12,10 @@ import coil3.request.SuccessResult import com.hippo.ehviewer.Settings import com.hippo.ehviewer.image.hasQrCode import eu.kanade.tachiyomi.util.system.logcat +import java.util.concurrent.locks.ReentrantReadWriteLock +import kotlin.concurrent.read +import kotlin.concurrent.write import moe.tarsin.coroutines.runSuspendCatching -import moe.tarsin.kt.sync private val detectQrCodeKey = Extras.Key(default = false) @@ -22,6 +24,7 @@ fun ImageRequest.Builder.detectQrCode(enable: Boolean) = apply { } private val cache = SieveCache(50) +private val lock = ReentrantReadWriteLock() val ImageRequest.detectQrCode: Boolean get() = getExtra(detectQrCodeKey) @@ -34,7 +37,7 @@ object QrCodeInterceptor : Interceptor { if (image is BitmapImageWithExtraInfo) { fun compute() = runSuspendCatching { hasQrCode(image.image.bitmap) }.onFailure { logcat(it) }.getOrThrow() val hasQrCode = when (val key = result.memoryCacheKey) { - is MemoryCache.Key -> cache.sync { cache[key] } ?: compute().also { cache.sync { cache[key] = it } } + is MemoryCache.Key -> lock.read { cache[key] } ?: compute().also { lock.write { cache[key] = it } } null -> compute() } val new = image.copy(hasQrCode = hasQrCode) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/gallery/PageLoader.kt b/app/src/main/kotlin/com/hippo/ehviewer/gallery/PageLoader.kt index 66a1dea8a..5501d2fad 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/gallery/PageLoader.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/gallery/PageLoader.kt @@ -6,9 +6,11 @@ import com.hippo.ehviewer.Settings import com.hippo.ehviewer.image.Image import com.hippo.ehviewer.util.OSUtils import com.hippo.ehviewer.util.isAtLeastO +import java.util.concurrent.locks.ReentrantReadWriteLock +import kotlin.concurrent.read +import kotlin.concurrent.write import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.update -import moe.tarsin.kt.sync private const val MAX_CACHE_SIZE = 512 * 1024 * 1024 private const val MIN_CACHE_SIZE = 128 * 1024 * 1024 @@ -31,6 +33,8 @@ abstract class PageLoader { }, ) + private val lock = ReentrantReadWriteLock() + val pages by lazy { check(size > 0) (0 until size).map { Page(it) } @@ -48,11 +52,11 @@ abstract class PageLoader { @CallSuper open fun stop() { - cache.sync { evictAll() } + lock.write { cache.evictAll() } } fun restart() { - cache.sync { evictAll() } + lock.write { cache.evictAll() } pages.forEach(Page::reset) } @@ -61,7 +65,7 @@ abstract class PageLoader { private var lastRequestIndex = -1 fun request(index: Int) { - val image = cache.sync { cache[index] } + val image = lock.read { cache[index] } if (image != null) { notifyPageSucceed(index, image, false) } else { @@ -120,7 +124,7 @@ abstract class PageLoader { fun notifyPageSucceed(index: Int, image: Image, replaceCache: Boolean = true) { if (replaceCache) { - cache.sync { cache[index] = image } + lock.write { cache[index] = image } } pages[index].statusFlow.update { if (image.hasQrCode) PageStatus.Blocked(image) else PageStatus.Ready(image) } } diff --git a/app/src/main/kotlin/moe/tarsin/kt/Sync.kt b/app/src/main/kotlin/moe/tarsin/kt/Sync.kt deleted file mode 100644 index 69380c63a..000000000 --- a/app/src/main/kotlin/moe/tarsin/kt/Sync.kt +++ /dev/null @@ -1,3 +0,0 @@ -package moe.tarsin.kt - -inline fun T.sync(block: T.() -> R) = synchronized(this) { block(this) }