forked from sergeych/crypto2
missing initial files + publishing
This commit is contained in:
1 parent
aaa8c436b0
commit
f429cfe418
25 files changed
+870
No files matched your search
@@ -0,0 +1,10 @@
|
||||
package net.sergeych.tools
|
||||
|
||||
@Suppress("unused")
|
||||
class AtomicCounter(initialValue: Long = 0) {
|
||||
private val op = ProtectedOp()
|
||||
var value: Long = initialValue
|
||||
private set
|
||||
|
||||
fun incrementAndGet(): Long = op { ++value }
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
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
|
||||
@@ -0,0 +1,22 @@
|
||||
@file:Suppress("unused")
|
||||
|
||||
package net.sergeych.tools
|
||||
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
/**
|
||||
* suspend until the flow produces the value to which the
|
||||
* predicate returns true
|
||||
*/
|
||||
suspend fun <T>Flow<T>.waitFor(predicate: (T)->Boolean) {
|
||||
coroutineScope {
|
||||
launch {
|
||||
collect {
|
||||
if( predicate(it) ) cancel()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user