musikr.cache: fix deadlocking
This commit is contained in:
@@ -20,7 +20,9 @@ package org.oxycblt.musikr.cache.db
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
|
import android.util.Log
|
||||||
import kotlinx.coroutines.sync.Mutex
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
import org.oxycblt.musikr.cache.Cache
|
import org.oxycblt.musikr.cache.Cache
|
||||||
import org.oxycblt.musikr.cache.CacheResult
|
import org.oxycblt.musikr.cache.CacheResult
|
||||||
import org.oxycblt.musikr.cache.CachedSong
|
import org.oxycblt.musikr.cache.CachedSong
|
||||||
@@ -39,10 +41,10 @@ class DBCache private constructor(private val readDao: CacheReadDao) : Cache {
|
|||||||
private val mappingLock = Mutex()
|
private val mappingLock = Mutex()
|
||||||
|
|
||||||
override suspend fun read(file: File): CacheResult {
|
override suspend fun read(file: File): CacheResult {
|
||||||
mappingLock.lock()
|
|
||||||
val currentMapping =
|
val currentMapping =
|
||||||
mapping ?: readDao.selectAllSongs().associateBy { it.uri }.also { mapping = it }
|
mappingLock.withLock {
|
||||||
mappingLock.unlock()
|
mapping ?: readDao.selectAllSongs().associateBy { it.uri }.also { mapping = it }
|
||||||
|
}
|
||||||
val dbSong = currentMapping[file.uri] ?: return CacheResult.Miss(file)
|
val dbSong = currentMapping[file.uri] ?: return CacheResult.Miss(file)
|
||||||
if (dbSong.modifiedMs != file.modifiedMs) {
|
if (dbSong.modifiedMs != file.modifiedMs) {
|
||||||
return CacheResult.Stale(file, dbSong.addedMs)
|
return CacheResult.Stale(file, dbSong.addedMs)
|
||||||
|
|||||||
Reference in New Issue
Block a user