feat: replace fiber run() with transfer() and suspend() for scheduler-friendly semantics

Remove the run() method from fibers and introduce transfer() for non-stack-based fiber switching, along with suspend() to pause the interpreter and return control to the host application. Update Timer.sleep() to use runNextScheduled_() instead of Fiber.yield(), and add Timer.schedule() for creating independently scheduled fibers. Refactor the core fiber runtime to support transfer semantics, including proper error handling for aborted fibers and self-transfer edge cases. Fix a missing quote in test.py's runtime error validation string.
This commit is contained in:
Bob Nystrom
2015-08-31 05:15:37 +00:00
parent ca8e97521a
commit 7e111d19da
49 changed files with 469 additions and 350 deletions
+16 -16
View File
@@ -1,27 +1,27 @@
import "timer" for Timer
Fiber.new {
Timer.schedule {
IO.print("a before")
Timer.sleep(0)
IO.print("a")
}.call()
IO.print("a after")
}
Fiber.new {
Timer.schedule {
IO.print("b before")
Timer.sleep(0)
IO.print("b")
}.call()
Fiber.new {
Timer.sleep(0)
IO.print("c")
}.call()
IO.print("b after")
}
IO.print("main")
Timer.sleep(0) // This is enough to let the other fiber run.
Timer.sleep(0) // This is enough to let the other fibers run to their sleep.
IO.print("main after")
Timer.sleep(0) // And now their sleeps complete.
IO.print("done")
// expect: main
// Run in the order they were enqueued.
// expect: a
// expect: b
// expect: c
// expect: a before
// expect: b before
// expect: main after
// expect: a after
// expect: b after
// expect: done