Fiber improvements
This commit is contained in:
parent
a453e53a14
commit
9d847df01c
@ -20,7 +20,7 @@ plugins {
|
||||
}
|
||||
|
||||
group = "ru.landgrafhomyak.multitasking"
|
||||
version = "1.0-SNAPSHOT"
|
||||
version = "0.1"
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
||||
@ -0,0 +1,9 @@
|
||||
package ru.landgrafhomyak.multitasking_0
|
||||
|
||||
public enum class ExecutionState {
|
||||
CREATED,
|
||||
RUNNING,
|
||||
MANUALLY_SUSPENDED,
|
||||
FINISHED_SUCCESSFULLY,
|
||||
FINISHED_WITH_ERROR
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
package ru.landgrafhomyak.multitasking_0
|
||||
|
||||
/**
|
||||
* Handle of concurrency primitive with call stack saving.
|
||||
* Concurrency primitive with call stack.
|
||||
*/
|
||||
public interface Fiber {
|
||||
/**
|
||||
@ -10,10 +10,40 @@ public interface Fiber {
|
||||
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 runningOnThread: Thread?*/
|
||||
public val name: String
|
||||
|
||||
/**
|
||||
* 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.
|
||||
@ -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.
|
||||
*/
|
||||
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
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
package ru.landgrafhomyak.multitasking_0
|
||||
|
||||
public fun interface FiberRoutine {
|
||||
public fun runFiber(fiber: Fiber)
|
||||
}
|
||||
@ -19,7 +19,7 @@ public interface SingleThreadEventLoop {
|
||||
* Adds [task][kernel] to scheduler's queue.
|
||||
* 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.
|
||||
|
||||
@ -1,5 +0,0 @@
|
||||
package ru.landgrafhomyak.multitasking_0
|
||||
|
||||
public fun interface TaskKernel {
|
||||
public fun run(task: Task)
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
package ru.landgrafhomyak.multitasking_0
|
||||
|
||||
public fun interface TaskRoutine {
|
||||
public fun runTask(task: Task)
|
||||
}
|
||||
@ -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()
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user