From f231ace413f02376feffffb3cd855345440398bd Mon Sep 17 00:00:00 2001 From: Arkadii Ivanov Date: Tue, 31 Oct 2023 23:02:20 +0000 Subject: [PATCH 1/4] Replaced kotlin.native.concurrent atomics with kotlin.concurrent --- .../kotlin/com/badoo/reaktive/scheduler/MainScheduler.kt | 2 +- .../kotlin/com/badoo/reaktive/disposable/Various.kt | 5 +---- .../badoo/reaktive/looperthread/FixedLooperThreadStrategy.kt | 2 +- .../kotlin/com/badoo/reaktive/scheduler/SchedulerImpl.kt | 2 +- .../kotlin/com/badoo/reaktive/utils/DelayQueue.kt | 4 ++-- .../kotlin/com/badoo/reaktive/utils/atomic/AtomicBoolean.kt | 2 +- .../kotlin/com/badoo/reaktive/utils/atomic/AtomicInt.kt | 2 +- .../kotlin/com/badoo/reaktive/utils/atomic/AtomicLong.kt | 2 +- .../com/badoo/reaktive/utils/atomic/AtomicReference.kt | 2 +- 9 files changed, 10 insertions(+), 13 deletions(-) diff --git a/reaktive/src/darwinCommonMain/kotlin/com/badoo/reaktive/scheduler/MainScheduler.kt b/reaktive/src/darwinCommonMain/kotlin/com/badoo/reaktive/scheduler/MainScheduler.kt index d8fe3e1d6..c36554d9a 100644 --- a/reaktive/src/darwinCommonMain/kotlin/com/badoo/reaktive/scheduler/MainScheduler.kt +++ b/reaktive/src/darwinCommonMain/kotlin/com/badoo/reaktive/scheduler/MainScheduler.kt @@ -10,7 +10,7 @@ import platform.darwin.DISPATCH_TIME_NOW import platform.darwin.dispatch_after import platform.darwin.dispatch_get_main_queue import platform.darwin.dispatch_time -import kotlin.native.concurrent.AtomicReference +import kotlin.concurrent.AtomicReference import kotlin.time.Duration internal class MainScheduler : Scheduler { diff --git a/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/disposable/Various.kt b/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/disposable/Various.kt index c5f137425..0904aec71 100644 --- a/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/disposable/Various.kt +++ b/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/disposable/Various.kt @@ -1,11 +1,9 @@ package com.badoo.reaktive.disposable -import kotlin.native.concurrent.AtomicInt +import kotlin.concurrent.AtomicInt -@Suppress("FunctionName") actual inline fun Disposable(crossinline onDispose: () -> Unit): Disposable = object : Disposable { - @Suppress("ObjectPropertyName") // Backing property private var _isDisposed = AtomicInt(0) override val isDisposed: Boolean get() = _isDisposed.value != 0 @@ -16,7 +14,6 @@ actual inline fun Disposable(crossinline onDispose: () -> Unit): Disposable = } } -@Suppress("FunctionName") actual fun Disposable(): Disposable = SimpleDisposable() private class SimpleDisposable : Disposable { diff --git a/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/looperthread/FixedLooperThreadStrategy.kt b/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/looperthread/FixedLooperThreadStrategy.kt index 39b00753f..f75dd4669 100644 --- a/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/looperthread/FixedLooperThreadStrategy.kt +++ b/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/looperthread/FixedLooperThreadStrategy.kt @@ -1,6 +1,6 @@ package com.badoo.reaktive.looperthread -import kotlin.native.concurrent.AtomicInt +import kotlin.concurrent.AtomicInt internal class FixedLooperThreadStrategy(threadCount: Int) : LooperThreadStrategy { diff --git a/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/scheduler/SchedulerImpl.kt b/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/scheduler/SchedulerImpl.kt index 8381441c2..9d2fa27f5 100644 --- a/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/scheduler/SchedulerImpl.kt +++ b/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/scheduler/SchedulerImpl.kt @@ -7,7 +7,7 @@ import com.badoo.reaktive.looperthread.LooperThreadStrategy import com.badoo.reaktive.utils.clock.Clock import com.badoo.reaktive.utils.clock.DefaultClock import com.badoo.reaktive.utils.coerceAtLeastZero -import kotlin.native.concurrent.AtomicInt +import kotlin.concurrent.AtomicInt import kotlin.time.Duration internal class SchedulerImpl( diff --git a/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/DelayQueue.kt b/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/DelayQueue.kt index cf66daf68..62482ba41 100644 --- a/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/DelayQueue.kt +++ b/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/DelayQueue.kt @@ -5,7 +5,7 @@ import com.badoo.reaktive.utils.clock.DefaultClock import com.badoo.reaktive.utils.lock.ConditionLock import com.badoo.reaktive.utils.lock.synchronized import com.badoo.reaktive.utils.queue.PriorityQueue -import kotlin.native.concurrent.AtomicLong +import kotlin.concurrent.AtomicLong import kotlin.time.Duration internal class DelayQueue( @@ -95,7 +95,7 @@ internal class DelayQueue( val sequenceNumber = sequencer.addAndGet(1L) private companion object { - private val sequencer = AtomicLong() + private val sequencer = AtomicLong(0L) } } diff --git a/utils-internal/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/atomic/AtomicBoolean.kt b/utils-internal/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/atomic/AtomicBoolean.kt index f49865236..253053570 100644 --- a/utils-internal/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/atomic/AtomicBoolean.kt +++ b/utils-internal/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/atomic/AtomicBoolean.kt @@ -5,7 +5,7 @@ import com.badoo.reaktive.utils.InternalReaktiveApi @InternalReaktiveApi actual class AtomicBoolean actual constructor(initialValue: Boolean) { - private val delegate = kotlin.native.concurrent.AtomicInt(initialValue.intValue) + private val delegate = kotlin.concurrent.AtomicInt(initialValue.intValue) actual var value: Boolean get() = delegate.value != 0 diff --git a/utils-internal/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/atomic/AtomicInt.kt b/utils-internal/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/atomic/AtomicInt.kt index 9aa49a9b1..578a370cf 100644 --- a/utils-internal/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/atomic/AtomicInt.kt +++ b/utils-internal/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/atomic/AtomicInt.kt @@ -5,7 +5,7 @@ import com.badoo.reaktive.utils.InternalReaktiveApi @InternalReaktiveApi actual class AtomicInt actual constructor(initialValue: Int) { - private val delegate = kotlin.native.concurrent.AtomicInt(initialValue) + private val delegate = kotlin.concurrent.AtomicInt(initialValue) actual var value: Int get() = delegate.value diff --git a/utils-internal/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/atomic/AtomicLong.kt b/utils-internal/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/atomic/AtomicLong.kt index 1d9b7b46e..6769a7b11 100644 --- a/utils-internal/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/atomic/AtomicLong.kt +++ b/utils-internal/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/atomic/AtomicLong.kt @@ -5,7 +5,7 @@ import com.badoo.reaktive.utils.InternalReaktiveApi @InternalReaktiveApi actual class AtomicLong actual constructor(initialValue: Long) { - private val delegate = kotlin.native.concurrent.AtomicLong(initialValue) + private val delegate = kotlin.concurrent.AtomicLong(initialValue) actual var value: Long get() = delegate.value diff --git a/utils-internal/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/atomic/AtomicReference.kt b/utils-internal/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/atomic/AtomicReference.kt index f71272d5d..20fdfd341 100644 --- a/utils-internal/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/atomic/AtomicReference.kt +++ b/utils-internal/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/atomic/AtomicReference.kt @@ -5,7 +5,7 @@ import com.badoo.reaktive.utils.InternalReaktiveApi @InternalReaktiveApi actual class AtomicReference actual constructor(initialValue: T) { - private val delegate = kotlin.native.concurrent.AtomicReference(initialValue) + private val delegate = kotlin.concurrent.AtomicReference(initialValue) actual var value: T get() = delegate.value From ebd309cb3110e6449f1f71ff44edcf65311df153 Mon Sep 17 00:00:00 2001 From: Arkadii Ivanov Date: Tue, 31 Oct 2023 23:03:58 +0000 Subject: [PATCH 2/4] Use ValueTimeMark for monotonic time measurements instead of Duration --- .../CoroutineContextScheduler.kt | 5 ++-- .../CoroutineContextSchedulerTest.kt | 4 ++- .../SchedulerCoroutineDispatcherTest.kt | 5 ++-- .../reaktive/scheduler/TrampolineScheduler.kt | 7 +++--- .../reaktive/looperthread/LooperThread.kt | 4 +-- .../badoo/reaktive/scheduler/SchedulerImpl.kt | 5 ++-- .../com/badoo/reaktive/utils/DelayQueue.kt | 5 ++-- .../reaktive/looperthread/LooperThreadTest.kt | 11 ++++---- .../com/badoo/reaktive/utils/clock/Clock.kt | 4 +-- .../reaktive/utils/clock/DefaultClock.kt | 7 +++++- .../reaktive/utils/clock/DefaultClockTest.kt | 3 --- .../reaktive/utils/clock/DefaultClock.kt | 25 ------------------- .../reaktive/utils/clock/DefaultClock.kt | 12 --------- .../reaktive/utils/clock/DefaultClock.kt | 13 ---------- 14 files changed, 34 insertions(+), 76 deletions(-) delete mode 100644 utils-internal/src/jsMain/kotlin/com/badoo/reaktive/utils/clock/DefaultClock.kt delete mode 100644 utils-internal/src/jvmCommonMain/kotlin/com/badoo/reaktive/utils/clock/DefaultClock.kt delete mode 100644 utils-internal/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/clock/DefaultClock.kt diff --git a/coroutines-interop/src/commonMain/kotlin/com/badoo/reaktive/coroutinesinterop/CoroutineContextScheduler.kt b/coroutines-interop/src/commonMain/kotlin/com/badoo/reaktive/coroutinesinterop/CoroutineContextScheduler.kt index 0d95256bb..8ea46d520 100644 --- a/coroutines-interop/src/commonMain/kotlin/com/badoo/reaktive/coroutinesinterop/CoroutineContextScheduler.kt +++ b/coroutines-interop/src/commonMain/kotlin/com/badoo/reaktive/coroutinesinterop/CoroutineContextScheduler.kt @@ -20,6 +20,7 @@ import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock import kotlin.coroutines.CoroutineContext import kotlin.time.Duration +import kotlin.time.TimeSource.Monotonic.ValueTimeMark internal class CoroutineContextScheduler( private val context: CoroutineContext, @@ -102,7 +103,7 @@ internal class CoroutineContextScheduler( } } - private suspend fun delayUntilStart(startTime: Duration) { + private suspend fun delayUntilStart(startTime: ValueTimeMark) { val uptime = clock.uptime if (uptime < startTime) { delay(startTime - uptime) @@ -123,7 +124,7 @@ internal class CoroutineContextScheduler( } private data class Task( - val startTime: Duration, + val startTime: ValueTimeMark, val period: Duration, val task: () -> Unit ) diff --git a/coroutines-interop/src/commonTest/kotlin/com/badoo/reaktive/coroutinesinterop/CoroutineContextSchedulerTest.kt b/coroutines-interop/src/commonTest/kotlin/com/badoo/reaktive/coroutinesinterop/CoroutineContextSchedulerTest.kt index 5a0b47e61..cad46d656 100644 --- a/coroutines-interop/src/commonTest/kotlin/com/badoo/reaktive/coroutinesinterop/CoroutineContextSchedulerTest.kt +++ b/coroutines-interop/src/commonTest/kotlin/com/badoo/reaktive/coroutinesinterop/CoroutineContextSchedulerTest.kt @@ -13,6 +13,8 @@ import kotlin.test.assertTrue import kotlin.time.Duration import kotlin.time.Duration.Companion.milliseconds import kotlin.time.Duration.Companion.seconds +import kotlin.time.TimeSource +import kotlin.time.TimeSource.Monotonic.ValueTimeMark @OptIn(ExperimentalCoroutinesApi::class) // UnconfinedTestDispatcher is experimental class CoroutineContextSchedulerTest { @@ -291,7 +293,7 @@ class CoroutineContextSchedulerTest { } private class TestClock : Clock { - override var uptime: Duration = Duration.ZERO + override var uptime: ValueTimeMark = TimeSource.Monotonic.markNow() fun advanceBy(duration: Duration) { uptime += duration diff --git a/coroutines-interop/src/commonTest/kotlin/com/badoo/reaktive/coroutinesinterop/SchedulerCoroutineDispatcherTest.kt b/coroutines-interop/src/commonTest/kotlin/com/badoo/reaktive/coroutinesinterop/SchedulerCoroutineDispatcherTest.kt index fcdb02876..9b0ec938b 100644 --- a/coroutines-interop/src/commonTest/kotlin/com/badoo/reaktive/coroutinesinterop/SchedulerCoroutineDispatcherTest.kt +++ b/coroutines-interop/src/commonTest/kotlin/com/badoo/reaktive/coroutinesinterop/SchedulerCoroutineDispatcherTest.kt @@ -14,7 +14,6 @@ import kotlin.coroutines.EmptyCoroutineContext import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertTrue -import kotlin.time.Duration import kotlin.time.Duration.Companion.milliseconds class SchedulerCoroutineDispatcherTest { @@ -44,7 +43,7 @@ class SchedulerCoroutineDispatcherTest { val scheduler = TestScheduler(isManualProcessing = false) val dispatcher = SchedulerCoroutineDispatcher(scheduler = scheduler) val startTime = DefaultClock.uptime - val endTime = AtomicReference(Duration.ZERO) + val endTime = AtomicReference(startTime) launch(dispatcher) { delay(500.milliseconds) @@ -52,7 +51,7 @@ class SchedulerCoroutineDispatcherTest { } withContext(Dispatchers.Default) { - while (endTime.value == Duration.ZERO) { + while (endTime.value == startTime) { yield() } } diff --git a/reaktive/src/commonMain/kotlin/com/badoo/reaktive/scheduler/TrampolineScheduler.kt b/reaktive/src/commonMain/kotlin/com/badoo/reaktive/scheduler/TrampolineScheduler.kt index 8ed4ab51c..839f666e7 100644 --- a/reaktive/src/commonMain/kotlin/com/badoo/reaktive/scheduler/TrampolineScheduler.kt +++ b/reaktive/src/commonMain/kotlin/com/badoo/reaktive/scheduler/TrampolineScheduler.kt @@ -10,6 +10,7 @@ import com.badoo.reaktive.utils.clock.DefaultClock import com.badoo.reaktive.utils.coerceAtLeastZero import com.badoo.reaktive.utils.serializer.serializer import kotlin.time.Duration +import kotlin.time.TimeSource.Monotonic.ValueTimeMark internal class TrampolineScheduler( private val clock: Clock = DefaultClock, @@ -79,11 +80,11 @@ internal class TrampolineScheduler( return false } - val nextStart = if (task.period.isInfinite()) Duration.INFINITE else clock.uptime + task.period + val nextStart = if (task.period.isInfinite()) null else clock.uptime + task.period task.task() - if (!nextStart.isInfinite()) { + if (nextStart != null) { submit(task.copy(startTime = nextStart)) } @@ -91,7 +92,7 @@ internal class TrampolineScheduler( } private data class Task( - val startTime: Duration, + val startTime: ValueTimeMark, val period: Duration, val task: () -> Unit ) : Comparable { diff --git a/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/looperthread/LooperThread.kt b/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/looperthread/LooperThread.kt index 4dc139db4..545d3f1cc 100644 --- a/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/looperthread/LooperThread.kt +++ b/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/looperthread/LooperThread.kt @@ -3,7 +3,7 @@ package com.badoo.reaktive.looperthread import com.badoo.reaktive.utils.DelayQueue import kotlin.native.concurrent.TransferMode import kotlin.native.concurrent.Worker -import kotlin.time.Duration +import kotlin.time.TimeSource.Monotonic.ValueTimeMark internal class LooperThread { @@ -14,7 +14,7 @@ internal class LooperThread { worker.execute(TransferMode.SAFE, { this }) { it.loop() } } - fun schedule(token: Any, startTime: Duration, task: () -> Unit) { + fun schedule(token: Any, startTime: ValueTimeMark, task: () -> Unit) { queue.offerAt(Message(token, task), startTime) } diff --git a/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/scheduler/SchedulerImpl.kt b/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/scheduler/SchedulerImpl.kt index 9d2fa27f5..935f7c866 100644 --- a/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/scheduler/SchedulerImpl.kt +++ b/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/scheduler/SchedulerImpl.kt @@ -9,6 +9,7 @@ import com.badoo.reaktive.utils.clock.DefaultClock import com.badoo.reaktive.utils.coerceAtLeastZero import kotlin.concurrent.AtomicInt import kotlin.time.Duration +import kotlin.time.TimeSource.Monotonic.ValueTimeMark internal class SchedulerImpl( private val looperThreadStrategy: LooperThreadStrategy, @@ -63,7 +64,7 @@ internal class SchedulerImpl( submit(startTime = getStartTime(delay), task = t) } - private fun submit(startTime: Duration, task: () -> Unit) { + private fun submit(startTime: ValueTimeMark, task: () -> Unit) { if (!isDisposed) { looperThread.schedule(token = this, startTime = startTime, task = task) } @@ -73,7 +74,7 @@ internal class SchedulerImpl( looperThread.cancel(this) } - private fun getStartTime(delay: Duration): Duration = + private fun getStartTime(delay: Duration): ValueTimeMark = clock.uptime + delay.coerceAtLeastZero() } } diff --git a/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/DelayQueue.kt b/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/DelayQueue.kt index 62482ba41..cfad747a7 100644 --- a/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/DelayQueue.kt +++ b/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/DelayQueue.kt @@ -7,6 +7,7 @@ import com.badoo.reaktive.utils.lock.synchronized import com.badoo.reaktive.utils.queue.PriorityQueue import kotlin.concurrent.AtomicLong import kotlin.time.Duration +import kotlin.time.TimeSource.Monotonic.ValueTimeMark internal class DelayQueue( private val clock: Clock = DefaultClock, @@ -66,7 +67,7 @@ internal class DelayQueue( offerAt(value, clock.uptime + timeout) } - fun offerAt(value: T, time: Duration) { + fun offerAt(value: T, time: ValueTimeMark) { lock.synchronized { val queue = queue ?: return queue.offer(Holder(value, time)) @@ -90,7 +91,7 @@ internal class DelayQueue( private data class Holder( val value: T, - val endTime: Duration, + val endTime: ValueTimeMark, ) { val sequenceNumber = sequencer.addAndGet(1L) diff --git a/reaktive/src/nativeCommonTest/kotlin/com/badoo/reaktive/looperthread/LooperThreadTest.kt b/reaktive/src/nativeCommonTest/kotlin/com/badoo/reaktive/looperthread/LooperThreadTest.kt index b39f7091e..31c6f3acf 100644 --- a/reaktive/src/nativeCommonTest/kotlin/com/badoo/reaktive/looperthread/LooperThreadTest.kt +++ b/reaktive/src/nativeCommonTest/kotlin/com/badoo/reaktive/looperthread/LooperThreadTest.kt @@ -10,6 +10,7 @@ import kotlin.test.Test import kotlin.test.assertFalse import kotlin.test.assertTrue import kotlin.time.Duration.Companion.milliseconds +import kotlin.time.TimeSource class LooperThreadTest { @@ -18,9 +19,9 @@ class LooperThreadTest { val isExecuted = AtomicBoolean() val lock = ConditionLock() val thread = LooperThread() - val startTime = getTimeMillis() + 200L + val startTime = TimeSource.Monotonic.markNow() + 200.milliseconds - thread.schedule(Unit, startTime.milliseconds) { + thread.schedule(Unit, startTime) { lock.synchronized { isExecuted.value = true lock.signal() @@ -31,7 +32,7 @@ class LooperThreadTest { lock.waitForOrFail(predicate = isExecuted::value) } - assertTrue(getTimeMillis() >= startTime) + assertTrue(startTime.hasPassedNow()) } @Test @@ -40,7 +41,7 @@ class LooperThreadTest { val lock = ConditionLock() val thread = LooperThread() - thread.schedule(Unit, getTimeMillis().milliseconds) { + thread.schedule(Unit, TimeSource.Monotonic.markNow()) { lock.synchronized { isExecuted.value = true lock.signal() @@ -54,7 +55,7 @@ class LooperThreadTest { thread.destroy() isExecuted.value = false - thread.schedule(Unit, getTimeMillis().milliseconds) { + thread.schedule(Unit, TimeSource.Monotonic.markNow()) { isExecuted.value = true } diff --git a/utils-internal/src/commonMain/kotlin/com/badoo/reaktive/utils/clock/Clock.kt b/utils-internal/src/commonMain/kotlin/com/badoo/reaktive/utils/clock/Clock.kt index d4f646a50..090e0e77b 100644 --- a/utils-internal/src/commonMain/kotlin/com/badoo/reaktive/utils/clock/Clock.kt +++ b/utils-internal/src/commonMain/kotlin/com/badoo/reaktive/utils/clock/Clock.kt @@ -1,10 +1,10 @@ package com.badoo.reaktive.utils.clock import com.badoo.reaktive.utils.InternalReaktiveApi -import kotlin.time.Duration +import kotlin.time.TimeSource.Monotonic.ValueTimeMark @InternalReaktiveApi interface Clock { - val uptime: Duration + val uptime: ValueTimeMark } diff --git a/utils-internal/src/commonMain/kotlin/com/badoo/reaktive/utils/clock/DefaultClock.kt b/utils-internal/src/commonMain/kotlin/com/badoo/reaktive/utils/clock/DefaultClock.kt index b1a957dc7..968a234f7 100644 --- a/utils-internal/src/commonMain/kotlin/com/badoo/reaktive/utils/clock/DefaultClock.kt +++ b/utils-internal/src/commonMain/kotlin/com/badoo/reaktive/utils/clock/DefaultClock.kt @@ -1,6 +1,11 @@ package com.badoo.reaktive.utils.clock import com.badoo.reaktive.utils.InternalReaktiveApi +import kotlin.time.TimeSource +import kotlin.time.TimeSource.Monotonic.ValueTimeMark @InternalReaktiveApi -expect object DefaultClock : Clock +object DefaultClock : Clock { + + override val uptime: ValueTimeMark get() = TimeSource.Monotonic.markNow() +} diff --git a/utils-internal/src/commonTest/kotlin/com/badoo/reaktive/utils/clock/DefaultClockTest.kt b/utils-internal/src/commonTest/kotlin/com/badoo/reaktive/utils/clock/DefaultClockTest.kt index da55f20a1..3a2363c22 100644 --- a/utils-internal/src/commonTest/kotlin/com/badoo/reaktive/utils/clock/DefaultClockTest.kt +++ b/utils-internal/src/commonTest/kotlin/com/badoo/reaktive/utils/clock/DefaultClockTest.kt @@ -4,7 +4,6 @@ import kotlin.test.Test import kotlin.test.assertTrue import kotlin.time.Duration import kotlin.time.Duration.Companion.milliseconds -import kotlin.time.ExperimentalTime import kotlin.time.TimeSource class DefaultClockTest { @@ -15,11 +14,9 @@ class DefaultClockTest { busySleep(1.milliseconds) val time2 = DefaultClock.uptime - assertTrue(time1.isPositive()) assertTrue(time2 - time1 >= 1.milliseconds) } - @OptIn(ExperimentalTime::class) private fun busySleep(duration: Duration) { val end = TimeSource.Monotonic.markNow() while (end.elapsedNow() < duration) { diff --git a/utils-internal/src/jsMain/kotlin/com/badoo/reaktive/utils/clock/DefaultClock.kt b/utils-internal/src/jsMain/kotlin/com/badoo/reaktive/utils/clock/DefaultClock.kt deleted file mode 100644 index 1635099d7..000000000 --- a/utils-internal/src/jsMain/kotlin/com/badoo/reaktive/utils/clock/DefaultClock.kt +++ /dev/null @@ -1,25 +0,0 @@ -package com.badoo.reaktive.utils.clock - -import com.badoo.reaktive.utils.InternalReaktiveApi -import kotlinx.browser.window -import kotlin.time.Duration -import kotlin.time.Duration.Companion.milliseconds -import kotlin.time.Duration.Companion.nanoseconds -import kotlin.time.Duration.Companion.seconds - -private external val process: dynamic - -@InternalReaktiveApi -actual object DefaultClock : Clock { - - private val isWindowDefined = jsTypeOf(window) != "undefined" - - override val uptime: Duration - get() = - if (isWindowDefined) { - window.performance.now().milliseconds - } else { - val t = process.hrtime().unsafeCast() - t[0].seconds + t[1].nanoseconds - } -} diff --git a/utils-internal/src/jvmCommonMain/kotlin/com/badoo/reaktive/utils/clock/DefaultClock.kt b/utils-internal/src/jvmCommonMain/kotlin/com/badoo/reaktive/utils/clock/DefaultClock.kt deleted file mode 100644 index f336bbf3c..000000000 --- a/utils-internal/src/jvmCommonMain/kotlin/com/badoo/reaktive/utils/clock/DefaultClock.kt +++ /dev/null @@ -1,12 +0,0 @@ -package com.badoo.reaktive.utils.clock - -import com.badoo.reaktive.utils.InternalReaktiveApi -import kotlin.time.Duration -import kotlin.time.Duration.Companion.nanoseconds - -@InternalReaktiveApi -actual object DefaultClock : Clock { - - override val uptime: Duration - get() = System.nanoTime().nanoseconds -} diff --git a/utils-internal/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/clock/DefaultClock.kt b/utils-internal/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/clock/DefaultClock.kt deleted file mode 100644 index e31565ad0..000000000 --- a/utils-internal/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/clock/DefaultClock.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.badoo.reaktive.utils.clock - -import com.badoo.reaktive.utils.InternalReaktiveApi -import kotlin.system.getTimeNanos -import kotlin.time.Duration -import kotlin.time.Duration.Companion.nanoseconds - -@InternalReaktiveApi -actual object DefaultClock : Clock { - - override val uptime: Duration - get() = getTimeNanos().nanoseconds -} From 89f0c0647f46c460e1a49ee6f324049e85fb6c5b Mon Sep 17 00:00:00 2001 From: Arkadii Ivanov Date: Tue, 31 Oct 2023 23:06:03 +0000 Subject: [PATCH 3/4] Suppressed ObsoleteWorkersApi warnings --- .../kotlin/com/badoo/reaktive/looperthread/LooperThread.kt | 2 ++ .../kotlin/com/badoo/reaktive/utils/ExpirationPool.kt | 2 ++ 2 files changed, 4 insertions(+) diff --git a/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/looperthread/LooperThread.kt b/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/looperthread/LooperThread.kt index 545d3f1cc..b77191aeb 100644 --- a/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/looperthread/LooperThread.kt +++ b/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/looperthread/LooperThread.kt @@ -1,10 +1,12 @@ package com.badoo.reaktive.looperthread import com.badoo.reaktive.utils.DelayQueue +import kotlin.native.concurrent.ObsoleteWorkersApi import kotlin.native.concurrent.TransferMode import kotlin.native.concurrent.Worker import kotlin.time.TimeSource.Monotonic.ValueTimeMark +@OptIn(ObsoleteWorkersApi::class) // There is no replacement yet internal class LooperThread { private val queue = DelayQueue() diff --git a/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/ExpirationPool.kt b/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/ExpirationPool.kt index 53654eb09..07dc7258d 100644 --- a/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/ExpirationPool.kt +++ b/reaktive/src/nativeCommonMain/kotlin/com/badoo/reaktive/utils/ExpirationPool.kt @@ -1,5 +1,6 @@ package com.badoo.reaktive.utils +import kotlin.native.concurrent.ObsoleteWorkersApi import kotlin.native.concurrent.TransferMode import kotlin.native.concurrent.Worker import kotlin.time.Duration @@ -9,6 +10,7 @@ import kotlin.time.Duration * DelayQueue should be destroyed, but all readers and writers must be cancelled first. * See LooperThread for sample implementation. */ +@OptIn(ObsoleteWorkersApi::class) // There is no replacement yet internal class ExpirationPool( private val onItemExpired: (T) -> Unit ) { From 85119b7897955300b87a9732bbecaf5feb0d399d Mon Sep 17 00:00:00 2001 From: Arkadii Ivanov Date: Tue, 31 Oct 2023 23:06:23 +0000 Subject: [PATCH 4/4] Specified Unit type explicitly for blockingGet --- .../kotlin/com/badoo/reaktive/completable/BlockingAwait.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reaktive/src/commonMain/kotlin/com/badoo/reaktive/completable/BlockingAwait.kt b/reaktive/src/commonMain/kotlin/com/badoo/reaktive/completable/BlockingAwait.kt index 3f3854f63..73751b1b4 100644 --- a/reaktive/src/commonMain/kotlin/com/badoo/reaktive/completable/BlockingAwait.kt +++ b/reaktive/src/commonMain/kotlin/com/badoo/reaktive/completable/BlockingAwait.kt @@ -13,5 +13,5 @@ import com.badoo.reaktive.maybe.blockingGet * Please refer to the corresponding RxJava [document](http://reactivex.io/RxJava/javadoc/io/reactivex/Completable.html#blockingAwait--). */ fun Completable.blockingAwait() { - asMaybe().blockingGet() + asMaybe().blockingGet() }