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

jvmMain.org.jetbrains.skiko.Task.kt Maven / Gradle / Ivy

There is a newer version: 0.6.7
Show newest version
package org.jetbrains.skiko

import kotlinx.coroutines.channels.Channel
import java.util.concurrent.atomic.AtomicBoolean

internal class Task {
    private val onFinish = Channel(1)

    private var done = AtomicBoolean(true)

    /**
     * Run task and await its finishing (i.e. calling of [finish])
     */
    suspend fun runAndAwait(run: suspend () -> Unit) {
        done.set(false)
        run()
        onFinish.receive()
    }

    /**
     * Finish running task. If there is no running task, do nothing.
     */
    fun finish() {
        if (!done.getAndSet(true)) {
            onFinish.trySend(Unit)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy