This is an old revision of the document!
PHP RFC: Fiber
- Version: 0.1
- Date: 2017-09-13
- Author: Haitao Lv, i@lvht.net
- Status: Draft
- First Published at: http://wiki.php.net/rfc/fiber
Introduction
Fibers are primitives for implementing light weight cooperative concurrency in PHP. Basically they are a means of creating Closure that can be paused and resumed. The scheduling of fiber must be done by the programmer and not the VM.
As opposed to Generator (a stackless light weight concurrency implementation), each fiber comes with a stack. This enables the fiber to be paused from deeply nested function calls within the fiber block.
When a fiber is created it will not run automatically. Rather it must be explicitly asked to run using the Fiber::resume method. The code running inside the fiber can give up control by calling Fiber::yield in which case it yields control back to caller (the caller of the Fiber::resume).
Proposal
Why not make it as a Extension?
Implementation
final class Fiber { public const STATUS_SUSPENDED = 1; public const STATUS_RUNNING = 2; public const STATUS_FINISHED = 3; public const STATUS_DEAD = 4; public function __construct(Closure $closure = null, int stack_size = null) {} public static function yield($arg1) {} public function resume($arg1...) {} /** @throws \UnexpectedValueException */ public function __wakeup(): void {} /** @throws \Error */ private function __clone() {} }
function sub1() { return Fiber::yield(1); } $fiber = new Fiber(function ($a, $b) { $c = Fiber::yield($a + $b); $d = sub1(); return $d.$c; }); echo $fiber->resume(1, 2); // echo 3 echo $fiber->resume("world"); // echo 1 echo $fiber->resume("hello "); // echo "hello world"
Backward Incompatible Changes
“Fiber” are now globally defined classes, which might collide with user defined classes of the same name in the global namespace. However, the risk of the introduction of them is considered to be very low, since the global namespace should not be used by PHP users.
Proposed PHP Version(s)
7.3
RFC Impact
To SAPIs
None
To Existing Extensions
None
To Opcache
None
New Constants
None
php.ini Defaults
- fiber.stack_size default stack size for A fiber
Open Issues
From Nikita
What happens if there are internal calls on the call stack?Say something like array_map(function() { await; }, [1, 2, 3]); inside a fiber. Internal calls (using the C stack rather than the VM stack) are usually the problem with this kind of endeavor.
Fiber does not support yielding during the internal call. Calling Fiber::yield in a internal call will cause core dump.
Unaffected PHP Functionality
None
Future Scope
This sections details areas where the feature might be improved in future, but that are not currently proposed in this RFC.
Proposed Voting Choices
Simple 50%+1 majority vote.
Patches and Tests
coming soon
Implementation
After the project is implemented, this section should contain
- the version(s) it was merged to
- a link to the git commit(s)
- a link to the PHP manual entry for the feature
- a link to the language specification section (if any)
References
Rejected Features
Keep this updated with features that were discussed on the mail lists.