added more KMP platforms, reused protectedop from mp_bintools where it is implemented almost everywhere

This commit is contained in:
Sergey Chernov 2024-03-16 10:47:32 +01:00
parent b86c7a853e
commit 431d91b080
8 changed files with 27 additions and 76 deletions

1
.idea/misc.xml generated
View File

@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="FrameworkDetectionExcludesConfiguration"> <component name="FrameworkDetectionExcludesConfiguration">

View File

@ -11,6 +11,7 @@ version = "0.1.1-SNAPSHOT"
repositories { repositories {
mavenCentral() mavenCentral()
mavenLocal()
maven("https://maven.universablockchain.com/") maven("https://maven.universablockchain.com/")
maven("https://gitea.sergeych.net/api/packages/SergeychWorks/maven") maven("https://gitea.sergeych.net/api/packages/SergeychWorks/maven")
} }
@ -26,23 +27,17 @@ kotlin {
} }
} }
js(KotlinJsCompilerType.IR) { js(KotlinJsCompilerType.IR) {
browser { browser()
}
}
val hostOs = System.getProperty("os.name")
val isArm64 = System.getProperty("os.arch") == "aarch64"
val isMingwX64 = hostOs.startsWith("Windows")
@Suppress("UNUSED_VARIABLE")
val nativeTarget = when {
hostOs == "Mac OS X" && isArm64 -> macosArm64("native")
hostOs == "Mac OS X" && !isArm64 -> macosX64("native")
hostOs == "Linux" && isArm64 -> linuxArm64("native")
hostOs == "Linux" && !isArm64 -> linuxX64("native")
isMingwX64 -> mingwX64("native")
else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
} }
val ktor_version = "2.3.6" macosX64()
iosX64()
iosArm64()
iosSimulatorArm64()
linuxX64()
mingwX64()
// wasmJs() no libsodimu bindings yet (strangely)
// val ktor_version = "2.3.6"
sourceSets { sourceSets {
all { all {
@ -53,14 +48,14 @@ kotlin {
val commonMain by getting { val commonMain by getting {
dependencies { dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1") implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
implementation("com.ionspin.kotlin:multiplatform-crypto-libsodium-bindings:0.9.0") implementation("com.ionspin.kotlin:multiplatform-crypto-libsodium-bindings:0.9.0")
api("com.ionspin.kotlin:bignum:0.3.8") api("com.ionspin.kotlin:bignum:0.3.9")
api("net.sergeych:mp_bintools:0.0.6") api("net.sergeych:mp_bintools:0.1.1")
api("net.sergeych:mp_stools:1.4.1") api("net.sergeych:mp_stools:1.4.7")
} }
} }
val commonTest by getting { val commonTest by getting {
@ -80,11 +75,11 @@ kotlin {
} }
} }
val jsTest by getting val jsTest by getting
val nativeMain by getting { // val nativeMain by getting {
dependencies { // dependencies {
} // }
} // }
val nativeTest by getting // val nativeTest by getting
} }
} }

View File

@ -2,6 +2,8 @@ package net.sergeych.crypto2
import net.sergeych.bintools.CRC import net.sergeych.bintools.CRC
fun isValidContrail(data: UByteArray): Boolean = CRC.crc8(data.copyOfRange(1, data.size)) == data[0] fun isValidContrail(data: UByteArray): Boolean = CRC.crc8(
data.copyOfRange(1, data.size).toByteArray()
) == data[0]
fun createContrail(data: UByteArray): UByteArray = ubyteArrayOf(CRC.crc8(data)) + data fun createContrail(data: UByteArray): UByteArray = ubyteArrayOf(CRC.crc8(data.toByteArray())) + data

View File

@ -1,5 +1,8 @@
package net.sergeych.tools package net.sergeych.tools
import net.sergeych.synctools.ProtectedOp
import net.sergeych.synctools.invoke
/** /**
* Multiplatform (JS and battery included) atomically mutable value. * Multiplatform (JS and battery included) atomically mutable value.
* Actual value can be either changed in a block of [mutuate] when * Actual value can be either changed in a block of [mutuate] when
@ -18,7 +21,7 @@ open class AtomicValue<T>(initialValue: T) {
* @return result of the mutation. Note that immediate call to property [value] * @return result of the mutation. Note that immediate call to property [value]
* could already return modified bu some other thread value! * could already return modified bu some other thread value!
*/ */
fun mutate(mutator: (T) -> T): T = op { fun mutate(mutator: (T) -> T): T = op.invoke {
actualValue = mutator(actualValue) actualValue = mutator(actualValue)
actualValue actualValue
} }

View File

@ -1,21 +0,0 @@
package net.sergeych.tools
/**
* Multiplatform interface to perform a regular (not suspend) operation
* protected by a platform mutex (where necessary). Get real implementation
* with [ProtectedOp]
*/
interface ProtectedOpImplementation {
/**
* Call [f] iin mutually exclusive mode, it means that only one invocation
* can be active at a time, all the rest are waiting until the current operation
* will finish.
*/
operator fun <T>invoke(f: ()->T): T
}
/**
* Get the platform-depended implementation of a mutex-protected operation.
*/
expect fun ProtectedOp(): ProtectedOpImplementation

View File

@ -1,6 +0,0 @@
package net.sergeych.tools
actual fun ProtectedOp(): ProtectedOpImplementation = object : ProtectedOpImplementation {
// JS targets are inherently single-threaded, so we do noting:
override fun <T> invoke(f: () -> T): T = f()
}

View File

@ -1,8 +0,0 @@
package net.sergeych.tools
actual fun ProtectedOp(): ProtectedOpImplementation = object : ProtectedOpImplementation {
private val lock = Object()
override fun <T> invoke(f: () -> T): T {
synchronized(lock) { return f() }
}
}

View File

@ -1,13 +0,0 @@
package net.sergeych.tools
import kotlinx.atomicfu.locks.SynchronizedObject
import kotlinx.atomicfu.locks.synchronized
actual fun ProtectedOp(): ProtectedOpImplementation = object : ProtectedOpImplementation {
private val lock = SynchronizedObject()
override fun <T> invoke(f: () -> T): T {
synchronized(lock) {
return f()
}
}
}