Use rwlock

This commit is contained in:
Dude so hot authored and renovate[bot] committed 2024-10-15 13:40:02 +08:00
1 parent e8c4b9ab78
commit 100a6569b7
3 files changed
+14 -10

No files matched your search

@@ -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<MemoryCache.Key, Boolean>(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)
@@ -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) }
}
@@ -1,3 +0,0 @@
package moe.tarsin.kt
inline fun <T : Any, R> T.sync(block: T.() -> R) = synchronized(this) { block(this) }