Fiber improvements

This commit is contained in:
Andrew Golovashevich 2025-09-07 05:13:55 +03:00
parent a453e53a14
commit 9d847df01c
8 changed files with 74 additions and 11 deletions

View File

@ -20,7 +20,7 @@ plugins {
} }
group = "ru.landgrafhomyak.multitasking" group = "ru.landgrafhomyak.multitasking"
version = "1.0-SNAPSHOT" version = "0.1"
repositories { repositories {
mavenCentral() mavenCentral()

View File

@ -0,0 +1,9 @@
package ru.landgrafhomyak.multitasking_0
public enum class ExecutionState {
CREATED,
RUNNING,
MANUALLY_SUSPENDED,
FINISHED_SUCCESSFULLY,
FINISHED_WITH_ERROR
}

View File

@ -1,7 +1,7 @@
package ru.landgrafhomyak.multitasking_0 package ru.landgrafhomyak.multitasking_0
/** /**
* Handle of concurrency primitive with call stack saving. * Concurrency primitive with call stack.
*/ */
public interface Fiber { public interface Fiber {
/** /**
@ -10,10 +10,40 @@ public interface Fiber {
public val ownerThread: Thread? public val ownerThread: Thread?
/** /**
* Returns `true` if this fiber running by any thread currently. Threadsafe getter. * Name of fiber to be shown in stacktrace or debug.
*/ */
public val isRunning: Boolean /*get() = this.runningOnThread != null*/ public val name: String
/*public val runningOnThread: Thread?*/
/**
* Current state of fiber.
* Thread-safe getter.
*/
public val state: ExecutionState
/**
* Thread in which [resume()][Fiber.resume] was called.
* Thread-safe getter.
*
* Returns `null` on all [states][Fiber.state] except [RUNNING][ExecutionState.RUNNING].
*
* On most implementations means that fiber actually runs on this [thread][Thread],
* but some emulations (e.g. [java's virtual threads](https://docs.oracle.com/en/java/javase/21/core/virtual-threads.html)) can't guarantee this.
*
* Even if fiber resumed from another fiber, it should inherit value.
*/
public val resumedOnThread: Thread?
/**
* Fiber in which [resume()][Fiber.resume] was called.
* Thread-safe getter.
*
* If [resumed][Fiber.resume] when [thread][Fiber.resumedOnThread] doesn't run any fiber, returns `null`.
* Returns `null` on all [states][Fiber.state] except [RUNNING][ExecutionState.RUNNING].
*
* Behavior of resuming fiber inside fiber is implementation-defined,
* so this property has only logical meaning for debugging (e.g. recovering async stacktrace).
*/
public val resumedOnFiber: Fiber?
/** /**
* Switches execution flow back to thread or fiber called [resume()][Fiber.resume] to run this fiber. * Switches execution flow back to thread or fiber called [resume()][Fiber.resume] to run this fiber.
@ -28,4 +58,10 @@ public interface Fiber {
* If fiber [bound to any thread][Fiber.ownerThread], switching must be done only inside this thread or fibers running in this thread. * If fiber [bound to any thread][Fiber.ownerThread], switching must be done only inside this thread or fibers running in this thread.
*/ */
public fun resume() public fun resume()
/**
* Returns uncaught exception that terminated this fiber.
* If current [state][Fiber.state] isn't [FINISHED_WITH_ERROR][ExecutionState.FINISHED_WITH_ERROR] will throw [IllegalStateException].
*/
public val uncaughtException: Throwable
} }

View File

@ -0,0 +1,5 @@
package ru.landgrafhomyak.multitasking_0
public fun interface FiberRoutine {
public fun runFiber(fiber: Fiber)
}

View File

@ -19,7 +19,7 @@ public interface SingleThreadEventLoop {
* Adds [task][kernel] to scheduler's queue. * Adds [task][kernel] to scheduler's queue.
* Returns immediately. Threadsafe. * Returns immediately. Threadsafe.
*/ */
public fun addTask(kernel: TaskKernel): Task public fun addTask(kernel: TaskRoutine): Task
/** /**
* Schedules [continuation.resumeWithSuccess(Unit)][Continuation.resumeWithSuccess] to be called after [delayMillis] milliseconds. * Schedules [continuation.resumeWithSuccess(Unit)][Continuation.resumeWithSuccess] to be called after [delayMillis] milliseconds.

View File

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

View File

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

View File

@ -0,0 +1,13 @@
package ru.landgrafhomyak.multitasking_0
public actual class Thread(
public actual val name: String,
) {
actual override fun toString(): String = TODO()
public actual var runningFiber: Fiber? = null
public actual companion object {
public actual fun getCurrentThread(): Thread = TODO()
}
}