Skip to main content

EventLoop

Struct EventLoop 

Source
pub struct EventLoop { /* private fields */ }

Implementations§

Source§

impl EventLoop

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn wakeup() -> PhpResult<()>

Resumes the callers whose future completed.

In a process forked from the one that initialized the event loop, does nothing: the notification socket is shared with the parent, whose notifications must stay there.

Source

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.

Source

pub fn pending() -> usize

The number of suspended callers.

Source

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.