pub struct EventLoop { /* private fields */ }Implementations§
Source§impl EventLoop
impl EventLoop
Sourcepub fn init() -> PhpResult<Zval>
pub fn init() -> PhpResult<Zval>
Initializes the event loop, returning the stream to watch for readability, calling
wakeup() when it is readable:
$stream = Extension::init();
Revolt\EventLoop::onReadable($stream, fn () => Extension::wakeup());The stream is the same every time: don’t close it before shutdown() (on Windows, closing
a socket stream shuts the socket down). The futures run on RUNTIME, shared by every
request.
Sourcepub fn init_with(runtime: impl FnOnce() -> Result<Runtime>) -> PhpResult<Zval>
pub fn init_with(runtime: impl FnOnce() -> Result<Runtime>) -> PhpResult<Zval>
Initializes the event loop like init(), with the futures running on the runtime that
runtime builds, for example to choose its threads:
// In a PHP method.
php_tokio::EventLoop::init_with(|| {
tokio::runtime::Builder::new_multi_thread()
.worker_threads(1)
.enable_all()
.build()
})runtime is only called if the event loop isn’t initialized yet. The event loop owns the
runtime: shutdown() releases it, once the futures of suspend_on() and
try_suspend_on() running on it completed.
Sourcepub fn suspend_on<T: Send + 'static, F: Future<Output = T> + Send + 'static>(
future: F,
) -> T
pub fn suspend_on<T: Send + 'static, F: Future<Output = T> + Send + 'static>( future: F, ) -> T
Suspends the current fiber until future completes, returning its result.
If the fiber is interrupted by an exception (an event-loop error thrown into {main}, or
the fiber being destroyed), this waits for the future anyway, blocking the PHP thread, and
returns its result with the exception pending: the future may borrow from the caller’s
stack (see php_async_impl). For the same reason, shutdown() waits for the future.
Panics if the event loop is not initialized, or if the future panicked.
Sourcepub fn try_suspend_on<T: Send + 'static, F: Future<Output = T> + Send + 'static>(
future: F,
) -> PhpResult<Option<T>>
pub fn try_suspend_on<T: Send + 'static, F: Future<Output = T> + Send + 'static>( future: F, ) -> PhpResult<Option<T>>
Like suspend_on(), but fails instead of panicking, and never waits for the future if
the fiber is interrupted: the future completes on its own. It must therefore own all its
data: never pass it anything borrowed with borrow_unchecked.
Interrupted by an exception, returns it, unchanged. Interrupted by an exit (exit(), or
PHP destroying the fiber, at the end of the request or once nothing references it),
returns None: the exit stays pending, as PHP must keep unwinding the fiber, so nothing
may be thrown. Return any value then: PHP discards it.
Fails if the future panicked.
Sourcepub fn drain(timeout: Duration) -> PhpResult<()>
pub fn drain(timeout: Duration) -> PhpResult<()>
Waits up to timeout for the futures of the suspended callers to complete, and resumes
the callers whose future completed, like wakeup(). This doesn’t run the PHP event loop:
the resumed callers continue once it runs.
Use it before shutdown() when the pending futures are about to complete (their work was
finished or rejected), so that their callers are resumed.
Sourcepub fn shutdown()
pub fn shutdown()
Releases the event loop. Callers still suspended are never resumed: their suspensions are
released here, on the PHP thread. A runtime from init_with() is released once the
futures running on it completed.
Blocks until the futures of suspend_on() completed: they may borrow from their callers,
whose memory PHP frees without unwinding them after a fatal error (or a timeout).
Called at the end of every request, after the extensions’ RSHUTDOWN, and before PHP frees the request’s resources and objects, which the event loop holds: call it earlier (in RSHUTDOWN, or from PHP) to release the event loop sooner. Calling it again does nothing.