Task interface

This commit is contained in:
Andrew Golovashevich 2025-09-07 01:20:01 +03:00
parent 01ae67c212
commit a453e53a14
3 changed files with 49 additions and 0 deletions

View File

@ -1,6 +1,30 @@
package ru.landgrafhomyak.multitasking_0 package ru.landgrafhomyak.multitasking_0
public interface SingleThreadEventLoop { public interface SingleThreadEventLoop {
/**
* Returns thread on which this event loop runs.
* Threadsafe getter.
*/
public val thread: Thread
/**
* Returns `true` if event loop running now.
* Threadsafe getter.
*
* Value `false` means that [runUntilSuspended()][SingleThreadEventLoop.runUntilSuspended] can be called.
*/
public val isRunning: Boolean
/**
* Adds [task][kernel] to scheduler's queue.
* Returns immediately. Threadsafe.
*/
public fun addTask(kernel: TaskKernel): Task
/**
* Schedules [continuation.resumeWithSuccess(Unit)][Continuation.resumeWithSuccess] to be called after [delayMillis] milliseconds.
* Threadsafe.
*/
public fun callAfter(delayMillis: Long, continuation: Continuation<Unit>) public fun callAfter(delayMillis: Long, continuation: Continuation<Unit>)
public fun <R> yield(handler: ContinuationHandler<R, Continuation<R>>): R public fun <R> yield(handler: ContinuationHandler<R, Continuation<R>>): R

View File

@ -0,0 +1,20 @@
package ru.landgrafhomyak.multitasking_0
public interface Task {
public val name: String
public val eventLoop: SingleThreadEventLoop
public fun cancel()
public val state: State
public val exception: Throwable
public enum class State {
PENDING,
RUNNING,
WAITING,
FINISHED_SUCCESSFULLY,
FINISHED_WITH_ERROR
}
}

View File

@ -0,0 +1,5 @@
package ru.landgrafhomyak.multitasking_0
public fun interface TaskKernel {
public fun run(task: Task)
}