[Wasm/WASI] Use K/JVM implementation of MonotonicTimeSource
Fixed #KT-60963
This commit is contained in:
committed by
Space Team
parent
567fc6b140
commit
1e9bc1abc7
@@ -5,12 +5,10 @@
|
|||||||
|
|
||||||
package kotlin.time
|
package kotlin.time
|
||||||
|
|
||||||
import kotlin.time.Duration.Companion.nanoseconds
|
|
||||||
import kotlin.time.TimeSource.Monotonic.ValueTimeMark
|
import kotlin.time.TimeSource.Monotonic.ValueTimeMark
|
||||||
import kotlin.wasm.WasiError
|
import kotlin.wasm.WasiError
|
||||||
import kotlin.wasm.WasiErrorCode
|
import kotlin.wasm.WasiErrorCode
|
||||||
import kotlin.wasm.WasmImport
|
import kotlin.wasm.WasmImport
|
||||||
import kotlin.wasm.unsafe.Pointer
|
|
||||||
import kotlin.wasm.unsafe.withScopedMemoryAllocator
|
import kotlin.wasm.unsafe.withScopedMemoryAllocator
|
||||||
|
|
||||||
private const val MONOTONIC = 1
|
private const val MONOTONIC = 1
|
||||||
@@ -29,31 +27,27 @@ private fun clockTimeGet(): Long = withScopedMemoryAllocator { allocator ->
|
|||||||
resultPtr = rp0.address.toInt()
|
resultPtr = rp0.address.toInt()
|
||||||
)
|
)
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
(Pointer(rp0.address.toInt().toUInt())).loadLong()
|
rp0.loadLong()
|
||||||
} else {
|
} else {
|
||||||
throw WasiError(WasiErrorCode.values()[ret])
|
throw WasiError(WasiErrorCode.values()[ret])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SinceKotlin("1.3")
|
@SinceKotlin("1.3")
|
||||||
//TODO: Try use implementation of K/JVM since it uses a Long nanosecond counter similar to System.nanoTime (KT-60963)
|
|
||||||
internal actual object MonotonicTimeSource : TimeSource.WithComparableMarks {
|
internal actual object MonotonicTimeSource : TimeSource.WithComparableMarks {
|
||||||
actual override fun markNow(): ValueTimeMark =
|
private val zero: Long = clockTimeGet()
|
||||||
ValueTimeMark(clockTimeGet())
|
private fun read(): Long = clockTimeGet() - zero
|
||||||
|
override fun toString(): String = "TimeSource(clock_time_get())"
|
||||||
|
|
||||||
|
actual override fun markNow(): ValueTimeMark = ValueTimeMark(read())
|
||||||
actual fun elapsedFrom(timeMark: ValueTimeMark): Duration =
|
actual fun elapsedFrom(timeMark: ValueTimeMark): Duration =
|
||||||
(clockTimeGet() - timeMark.reading).nanoseconds
|
saturatingDiff(read(), timeMark.reading, DurationUnit.NANOSECONDS)
|
||||||
|
|
||||||
|
actual fun differenceBetween(one: ValueTimeMark, another: ValueTimeMark): Duration =
|
||||||
|
saturatingOriginsDiff(one.reading, another.reading, DurationUnit.NANOSECONDS)
|
||||||
|
|
||||||
actual fun adjustReading(timeMark: ValueTimeMark, duration: Duration): ValueTimeMark =
|
actual fun adjustReading(timeMark: ValueTimeMark, duration: Duration): ValueTimeMark =
|
||||||
ValueTimeMark(timeMark.reading + duration.toLong(DurationUnit.NANOSECONDS))
|
ValueTimeMark(saturatingAdd(timeMark.reading, DurationUnit.NANOSECONDS, duration))
|
||||||
|
|
||||||
actual fun differenceBetween(one: ValueTimeMark, another: ValueTimeMark): Duration {
|
|
||||||
val ms1 = one.reading
|
|
||||||
val ms2 = another.reading
|
|
||||||
return if (ms1 == ms2) Duration.ZERO else (ms1 - ms2).nanoseconds
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun toString(): String = "WASI monotonic time source"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("ACTUAL_WITHOUT_EXPECT") // visibility
|
@Suppress("ACTUAL_WITHOUT_EXPECT") // visibility
|
||||||
|
|||||||
Reference in New Issue
Block a user