mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-16 20:28:04 +01:00
Now that I'm starting to write a real async scheduler on top of Wren's basic fiber API, I have a better feel for what it needs. It turns out run() is not it. - Remove run() methods. - Add transfer() which leaves the caller of the invoked fiber alone. - Add suspend() to return control to the host application. - Add Timer.schedule() to start a new independently scheduled fiber. - Change Timer.sleep() so that it only transfers control to explicitly scheduled fibers, not any one.
28 lines
506 B
Plaintext
28 lines
506 B
Plaintext
import "timer" for Timer
|
|
|
|
Timer.schedule {
|
|
IO.print("a before")
|
|
Timer.sleep(0)
|
|
IO.print("a after")
|
|
}
|
|
|
|
Timer.schedule {
|
|
IO.print("b before")
|
|
Timer.sleep(0)
|
|
IO.print("b after")
|
|
}
|
|
|
|
IO.print("main")
|
|
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
|
|
// expect: a before
|
|
// expect: b before
|
|
// expect: main after
|
|
// expect: a after
|
|
// expect: b after
|
|
// expect: done
|