Event loop and continuations interfaces

This commit is contained in:
Andrew Golovashevich 2025-09-07 00:42:19 +03:00
parent 0f74f7f4e3
commit 01ae67c212
6 changed files with 60 additions and 3 deletions

View File

@ -54,7 +54,11 @@ kotlin {
} }
sourceSets { sourceSets {
val commonMain by getting val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:${this@kotlin.coreLibrariesVersion}")
}
}
val commonTest by getting val commonTest by getting
val notJvmMain by creating { dependsOn(commonMain) } val notJvmMain by creating { dependsOn(commonMain) }
@ -88,8 +92,6 @@ kotlin {
} }
} }
publishing { publishing {

View File

@ -0,0 +1,5 @@
package ru.landgrafhomyak.multitasking_0
public fun interface CancellationHandler {
public fun onCancellation(): Throwable
}

View File

@ -0,0 +1,24 @@
package ru.landgrafhomyak.multitasking_0
/**
* Callback that resumes execution of a corresponding task [at suspension point][SingleThreadEventLoop.yield].
*/
public interface Continuation<T> {
/**
* Resumes task with result that will be returned from [yield()][SingleThreadEventLoop.yield].
*/
public fun resumeWithSuccess(result: T)
/**
* Resumes task with error that will be thrown from [yield()][SingleThreadEventLoop.yield].
*/
public fun resumeWithError(result: Throwable)
public interface Cancellable<T> : Continuation<T> {
/**
* Set's callback called when task is canceled, but not earlier than [linkContinuation()][ContinuationHandler.linkContinuation] returns.
*/
public fun setCancellationHandler(handler: CancellationHandler)
}
}

View File

@ -0,0 +1,5 @@
package ru.landgrafhomyak.multitasking_0
public fun interface ContinuationHandler<R, C : Continuation<R>> {
public fun linkContinuation(continuation: C)
}

View File

@ -0,0 +1,11 @@
package ru.landgrafhomyak.multitasking_0
public interface SingleThreadEventLoop {
public fun callAfter(delayMillis: Long, continuation: Continuation<Unit>)
public fun <R> yield(handler: ContinuationHandler<R, Continuation<R>>): R
public fun <R> yieldCancellable(handler: ContinuationHandler<R, Continuation.Cancellable<R>>): R
public fun runUntilSuspended()
public fun suspendEventLoop()
}

View File

@ -0,0 +1,10 @@
package ru.landgrafhomyak.multitasking_0
import kotlin.RuntimeException
public class TaskCancelledException : RuntimeException {
public constructor() : super()
public constructor(message: String?) : super(message)
public constructor(message: String?, cause: Throwable?) : super(message, cause)
public constructor(cause: Throwable?) : super(cause)
}