All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.badoo.reaktive.utils.lock.ConditionLockExt.kt Maven / Gradle / Ivy

There is a newer version: 2.3.0
Show newest version
package com.badoo.reaktive.utils.lock

import com.badoo.reaktive.utils.InternalReaktiveApi
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds

@OptIn(ExperimentalContracts::class)
@InternalReaktiveApi
inline fun  ConditionLock.synchronized(block: () -> T): T {
    contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }

    lock()
    try {
        return block()
    } finally {
        unlock()
    }
}

@InternalReaktiveApi
inline fun ConditionLock.waitFor(timeout: Duration, predicate: () -> Boolean): Boolean {
    require(!timeout.isNegative()) { "Timeout must not be negative" }

    var remainingTimeout = timeout

    while (true) {
        if (predicate()) {
            return true
        }
        if (!remainingTimeout.isPositive()) {
            return false
        }

        remainingTimeout = await(timeout = timeout)
    }
}

@InternalReaktiveApi
fun ConditionLock.waitForOrFail(timeout: Duration = 5.seconds, predicate: () -> Boolean) {
    if (!waitFor(timeout, predicate)) {
        error("Timeout waiting for condition")
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy