-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Small Preview CrossFade not work
Co-Authored-By: renovate[bot] <99822064+revonateb0t@users.noreply.github.com>
- Loading branch information
1 parent
d184165
commit bf38759
Showing
6 changed files
with
143 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2024 Moedog | ||
* | ||
* This file is part of EhViewer | ||
* | ||
* EhViewer is free software: you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* EhViewer is distributed in the hope that it will be useful, but | ||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with EhViewer. | ||
* If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.hippo.ehviewer.coil | ||
|
||
import kotlin.contracts.InvocationKind | ||
import kotlin.contracts.contract | ||
|
||
interface LockPool<Lock, K> { | ||
fun acquire(key: K): Lock | ||
fun release(key: K, lock: Lock) | ||
suspend fun Lock.lock() | ||
fun Lock.tryLock(): Boolean | ||
fun Lock.unlock() | ||
} | ||
|
||
suspend inline fun <Lock, K, R> LockPool<Lock, K>.withLock(key: K, action: () -> R): R { | ||
contract { | ||
callsInPlace(action, InvocationKind.EXACTLY_ONCE) | ||
} | ||
val lock = acquire(key) | ||
return try { | ||
lock.lock() | ||
return try { | ||
action() | ||
} finally { | ||
lock.unlock() | ||
} | ||
} finally { | ||
release(key, lock) | ||
} | ||
} | ||
|
||
suspend inline fun <Lock, K, R> LockPool<Lock, K>.withLockNeedSuspend(key: K, action: () -> R): Pair<R, Boolean> { | ||
contract { | ||
callsInPlace(action, InvocationKind.EXACTLY_ONCE) | ||
} | ||
val lock = acquire(key) | ||
return try { | ||
val mustSuspend = !lock.tryLock() | ||
if (mustSuspend) lock.lock() | ||
return try { | ||
action() to mustSuspend | ||
} finally { | ||
lock.unlock() | ||
} | ||
} finally { | ||
release(key, lock) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2024 Moedog | ||
* | ||
* This file is part of EhViewer | ||
* | ||
* EhViewer is free software: you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* EhViewer is distributed in the hope that it will be useful, but | ||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with EhViewer. | ||
* If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.hippo.ehviewer.coil | ||
|
||
import androidx.collection.MutableScatterMap | ||
import androidx.collection.mutableScatterMapOf | ||
import io.ktor.utils.io.pool.DefaultPool | ||
import kotlinx.coroutines.sync.Mutex | ||
|
||
class MutexTracker(mutex: Mutex = Mutex(), private var count: Int = 0) : Mutex by mutex { | ||
operator fun inc() = apply { count++ } | ||
operator fun dec() = apply { count-- } | ||
val isFree | ||
get() = count == 0 | ||
} | ||
|
||
object MutexPool : DefaultPool<MutexTracker>(capacity = 32) { | ||
override fun produceInstance() = MutexTracker() | ||
override fun validateInstance(mutex: MutexTracker) { | ||
check(!mutex.isLocked) | ||
check(mutex.isFree) | ||
} | ||
} | ||
|
||
class NamedMutex<K>(val active: MutableScatterMap<K, MutexTracker> = mutableScatterMapOf()) : LockPool<MutexTracker, K> { | ||
override fun acquire(key: K) = synchronized(active) { active.getOrPut(key) { MutexPool.borrow() }.inc() } | ||
override fun release(key: K, lock: MutexTracker) = synchronized(active) { | ||
lock.dec() | ||
if (lock.isFree) { | ||
active.remove(key) | ||
MutexPool.recycle(lock) | ||
} | ||
} | ||
override suspend fun MutexTracker.lock() = lock() | ||
override fun MutexTracker.tryLock() = tryLock() | ||
override fun MutexTracker.unlock() = unlock() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters