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:
parent
7e84330262
commit
440122d4f9
mirai-console/backend/mirai-console
@ -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`)
|
||||
|
@ -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(
|
||||
|
Loading…
Reference in New Issue
Block a user