Skip to content

Add fiber type to better support custom fiber APIs #7105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 7, 2021
Merged

Add fiber type to better support custom fiber APIs #7105

merged 1 commit into from
Jun 7, 2021

Conversation

kooldev
Copy link
Contributor

@kooldev kooldev commented Jun 5, 2021

Adds a "kind" field to zend_fiber_context to make sure that different fiber implementations do not get mixed up. The new field is set to zend_ce_fiber for PHP fibers and checked in Fiber::suspend() and Fiber::this() to ensure that these methods can only be called from within a running fiber.

The main reason for allowing different fiber implementations is to be prepared for async support that might come to PHP in a future version (or be provided by PHP extensions starting with PHP 8.1). Consider an example like this:

$task = async $client->sendRequest($request);
$response = await $task;

In this case the async expression should evaluate into a PHP object that is backed by a fiber but does not expose the same API as Fiber to userland. It would be possible to use the existing ZEND_API to create a Fiber and store that as a property of the new object, but that would be wasting memory for additional objects. While this is feasible in a userland implementation (due to lack of alternatives) it would not be reasonable to implement this in C. Having Fiber in PHP core is great and it should not be changed / restricted to async-only as there are other use cases for fiber (for example emitting values from deep within a callstack without turning everything into generators).

The great thing about this is that one can easily develop an async implementation as a PHP extension (of course without keyword support) and everybody has a chance to try and evaluate it before going for an RFC. It is also possible for Swoole to use the same approach and reuse some of the code that PHP 8.1 provides for their implementation.

void *handle; \
/* Pointer that identifies the fiber type. */ \
void *kind; \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this use zend_class_entry *, if a CE is used for identification?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is specified as void * to support fibers that are not exposed to PHP userland (pure C fibers) aswell hence restricting it to zend_class_entry * would be difficult due to no class entry being present. In essence this pointer just needs to be a unique address for each type of fiber. I decided to use zend_fiber_ce because it satisfies this requirement and is easy to associate with the fiber.

@trowski trowski merged commit b76a9db into php:master Jun 7, 2021
@kooldev kooldev deleted the fiber-api branch June 10, 2021 07:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants