1
0
mirror of https://github.com/mamoe/mirai.git synced 2025-03-30 09:50:12 +08:00

[console] Enable atomicfu compiler

This commit is contained in:
Him188 2022-10-29 13:14:35 +01:00
parent 7e84330262
commit 440122d4f9
No known key found for this signature in database
GPG Key ID: BA439CDDCF652375
2 changed files with 6 additions and 4 deletions
mirai-console/backend/mirai-console

View File

@ -21,6 +21,7 @@ plugins {
`maven-publish`
id("me.him188.kotlin-jvm-blocking-bridge")
id("me.him188.kotlin-dynamic-delegation")
id("kotlinx-atomicfu")
}
version = Versions.console
@ -45,7 +46,6 @@ dependencies {
compileAndTestRuntime(project(":mirai-core-utils"))
compileAndTestRuntime(`kotlin-stdlib-jdk8`)
compileAndTestRuntime(`kotlinx-atomicfu`)
compileAndTestRuntime(`kotlinx-coroutines-core`)
compileAndTestRuntime(`kotlinx-serialization-core`)
compileAndTestRuntime(`kotlinx-serialization-json`)

View File

@ -10,7 +10,6 @@
package net.mamoe.mirai.console.util
import kotlinx.atomicfu.atomic
import kotlinx.atomicfu.loop
import kotlinx.coroutines.*
import net.mamoe.mirai.console.internal.util.runIgnoreException
import net.mamoe.mirai.utils.currentTimeMillis
@ -38,7 +37,8 @@ internal class TimedTask(
val job: Job = scope.launch(coroutineContext) {
// `delay` always checks for cancellation
lastChangedTime.loop { last ->
while (true) {
val last = lastChangedTime.value
val current = currentTimeMillis()
if (last == UNCHANGED) {
runIgnoreException<CancellationException> {
@ -46,7 +46,7 @@ internal class TimedTask(
} ?: return@launch
} else {
if (current - last > intervalMillis) {
if (!lastChangedTime.compareAndSet(last, UNCHANGED)) return@loop
if (!casLastChangedToUnchanged(last)) continue
action()
}
runIgnoreException<CancellationException> {
@ -55,6 +55,8 @@ internal class TimedTask(
}
}
}
private fun casLastChangedToUnchanged(last: Long) = lastChangedTime.compareAndSet(last, UNCHANGED)
}
internal fun CoroutineScope.launchTimedTask(