2015-01-18 23:36:36 +00:00
|
|
|
^title Fiber Class
|
|
|
|
|
|
2015-11-07 19:09:04 +00:00
|
|
|
A lightweight coroutine. [Here][fibers] is a gentle introduction.
|
|
|
|
|
|
2015-11-08 21:31:22 +00:00
|
|
|
[fibers]: ../../concurrency.html
|
2015-01-18 23:36:36 +00:00
|
|
|
|
2015-07-10 16:18:22 +00:00
|
|
|
### Fiber.**new**(function)
|
2015-01-18 23:36:36 +00:00
|
|
|
|
|
|
|
|
Creates a new fiber that executes `function` in a separate coroutine when the
|
|
|
|
|
fiber is run. Does not immediately start running the fiber.
|
|
|
|
|
|
2015-09-22 14:59:54 +00:00
|
|
|
:::wren
|
2015-07-10 16:18:22 +00:00
|
|
|
var fiber = Fiber.new {
|
2015-09-15 14:46:09 +00:00
|
|
|
System.print("I won't get printed")
|
2015-01-18 23:36:36 +00:00
|
|
|
}
|
|
|
|
|
|
2015-03-27 14:43:36 +00:00
|
|
|
## Static Methods
|
|
|
|
|
|
2015-03-07 20:32:11 +00:00
|
|
|
### Fiber.**current**
|
|
|
|
|
|
|
|
|
|
The currently executing fiber.
|
|
|
|
|
|
2015-08-31 05:15:37 +00:00
|
|
|
### Fiber.**suspend**()
|
|
|
|
|
|
|
|
|
|
Pauses the current fiber, and stops the interpreter. Control returns to the
|
|
|
|
|
host application.
|
|
|
|
|
|
|
|
|
|
To resume execution, the host application will need to invoke the interpreter
|
|
|
|
|
again. If there is still a reference to the suspended fiber, it can be resumed.
|
|
|
|
|
|
2015-02-27 07:08:36 +00:00
|
|
|
### Fiber.**yield**()
|
2015-01-18 23:36:36 +00:00
|
|
|
|
|
|
|
|
Pauses the current fiber and transfers control to the parent fiber. "Parent"
|
|
|
|
|
here means the last fiber that was started using `call` and not `run`.
|
|
|
|
|
|
2015-09-22 14:59:54 +00:00
|
|
|
:::wren
|
2015-07-10 16:18:22 +00:00
|
|
|
var fiber = Fiber.new {
|
2015-09-15 14:46:09 +00:00
|
|
|
System.print("Before yield")
|
2015-02-27 07:08:36 +00:00
|
|
|
Fiber.yield()
|
2015-09-15 14:46:09 +00:00
|
|
|
System.print("After yield")
|
2015-01-18 23:36:36 +00:00
|
|
|
}
|
|
|
|
|
|
2015-10-18 22:56:52 +00:00
|
|
|
fiber.call() //> Before yield
|
|
|
|
|
System.print("After call") //> After call
|
|
|
|
|
fiber.call() //> After yield
|
2015-01-18 23:36:36 +00:00
|
|
|
|
2015-02-27 07:08:36 +00:00
|
|
|
When resumed, the parent fiber's `call()` method returns `null`.
|
2015-01-18 23:36:36 +00:00
|
|
|
|
|
|
|
|
If a yielded fiber is resumed by calling `call()` or `run()` with an argument,
|
2015-02-27 07:08:36 +00:00
|
|
|
`yield()` returns that value.
|
2015-01-18 23:36:36 +00:00
|
|
|
|
2015-09-22 14:59:54 +00:00
|
|
|
:::wren
|
2015-07-10 16:18:22 +00:00
|
|
|
var fiber = Fiber.new {
|
2015-10-18 22:56:52 +00:00
|
|
|
System.print(Fiber.yield()) //> value
|
2015-01-18 23:36:36 +00:00
|
|
|
}
|
|
|
|
|
|
2015-02-27 07:08:36 +00:00
|
|
|
fiber.call() // Run until the first yield.
|
2015-01-18 23:36:36 +00:00
|
|
|
fiber.call("value") // Resume the fiber.
|
|
|
|
|
|
2015-02-27 07:08:36 +00:00
|
|
|
If it was resumed by calling `call()` or `run()` with no argument, it returns
|
|
|
|
|
`null`.
|
2015-01-18 23:36:36 +00:00
|
|
|
|
2015-03-07 20:32:11 +00:00
|
|
|
If there is no parent fiber to return to, this exits the interpreter. This can
|
|
|
|
|
be useful to pause execution until the host application wants to resume it
|
|
|
|
|
later.
|
2015-01-18 23:36:36 +00:00
|
|
|
|
2015-09-22 14:59:54 +00:00
|
|
|
:::wren
|
2015-03-07 20:32:11 +00:00
|
|
|
Fiber.yield()
|
2015-09-15 14:46:09 +00:00
|
|
|
System.print("this does not get reached")
|
2015-01-18 23:36:36 +00:00
|
|
|
|
|
|
|
|
### Fiber.**yield**(value)
|
|
|
|
|
|
|
|
|
|
Similar to `Fiber.yield` but provides a value to return to the parent fiber's
|
|
|
|
|
`call`.
|
|
|
|
|
|
2015-09-22 14:59:54 +00:00
|
|
|
:::wren
|
2015-07-10 16:18:22 +00:00
|
|
|
var fiber = Fiber.new {
|
2015-01-18 23:36:36 +00:00
|
|
|
Fiber.yield("value")
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-18 22:56:52 +00:00
|
|
|
System.print(fiber.call()) //> value
|
2015-01-18 23:36:36 +00:00
|
|
|
|
2015-03-27 14:43:36 +00:00
|
|
|
## Methods
|
|
|
|
|
|
2015-02-27 07:08:36 +00:00
|
|
|
### **call**()
|
2015-01-18 23:36:36 +00:00
|
|
|
|
2015-04-22 14:45:20 +00:00
|
|
|
Starts or resumes the fiber if it is in a paused state.
|
2015-04-04 23:50:40 +00:00
|
|
|
|
2015-09-22 14:59:54 +00:00
|
|
|
:::wren
|
2015-07-10 16:18:22 +00:00
|
|
|
var fiber = Fiber.new {
|
2015-09-15 14:46:09 +00:00
|
|
|
System.print("Fiber called")
|
2015-04-04 23:50:40 +00:00
|
|
|
Fiber.yield()
|
2015-09-15 14:46:09 +00:00
|
|
|
System.print("Fiber called again")
|
2015-04-04 23:50:40 +00:00
|
|
|
}
|
2015-04-22 14:45:20 +00:00
|
|
|
|
|
|
|
|
fiber.call() // Start it.
|
|
|
|
|
fiber.call() // Resume after the yield() call.
|
|
|
|
|
|
|
|
|
|
When the called fiber yields, control is transferred back to the fiber that
|
|
|
|
|
called it.
|
|
|
|
|
|
|
|
|
|
If the called fiber is resuming from a yield, the `yield()` method returns
|
|
|
|
|
`null` in the called fiber.
|
|
|
|
|
|
2015-09-22 14:59:54 +00:00
|
|
|
:::wren
|
2015-07-10 16:18:22 +00:00
|
|
|
var fiber = Fiber.new {
|
2015-09-15 14:46:09 +00:00
|
|
|
System.print(Fiber.yield())
|
2015-04-22 14:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
2015-04-04 23:50:40 +00:00
|
|
|
fiber.call()
|
2015-10-18 22:56:52 +00:00
|
|
|
fiber.call() //> null
|
2015-01-18 23:36:36 +00:00
|
|
|
|
|
|
|
|
### **call**(value)
|
|
|
|
|
|
2015-04-04 23:50:40 +00:00
|
|
|
Invokes the fiber or resumes the fiber if it is in a paused state and sets
|
|
|
|
|
`value` as the returned value of the fiber's call to `yield`.
|
|
|
|
|
|
2015-09-22 14:59:54 +00:00
|
|
|
:::wren
|
2015-07-10 16:18:22 +00:00
|
|
|
var fiber = Fiber.new {
|
2015-09-15 14:46:09 +00:00
|
|
|
System.print(Fiber.yield())
|
2015-04-04 23:50:40 +00:00
|
|
|
}
|
2015-04-22 14:45:20 +00:00
|
|
|
|
2015-04-04 23:50:40 +00:00
|
|
|
fiber.call()
|
2015-10-18 22:56:52 +00:00
|
|
|
fiber.call("value") //> value
|
2015-01-18 23:36:36 +00:00
|
|
|
|
|
|
|
|
### **isDone**
|
|
|
|
|
|
|
|
|
|
Whether the fiber's main function has completed and the fiber can no longer be
|
|
|
|
|
run. This returns `false` if the fiber is currently running or has yielded.
|
|
|
|
|
|
2015-08-31 05:15:37 +00:00
|
|
|
### **transfer**()
|
2015-01-18 23:36:36 +00:00
|
|
|
|
|
|
|
|
**TODO**
|
|
|
|
|
|
2015-08-31 05:15:37 +00:00
|
|
|
### **transfer**(value)
|
2015-01-18 23:36:36 +00:00
|
|
|
|
|
|
|
|
**TODO**
|
2015-09-30 05:57:03 +00:00
|
|
|
|
|
|
|
|
### **transferError**(error)
|
|
|
|
|
|
|
|
|
|
**TODO**
|