Struct Reactor

Source
pub(crate) struct Reactor {
    pub(crate) poller: Poller,
    ticker: AtomicUsize,
    sources: Mutex<Slab<Arc<Source>>>,
    events: Mutex<Events>,
    timers: Mutex<BTreeMap<(Instant, usize), Waker>>,
    timer_ops: ConcurrentQueue<TimerOp>,
}
Expand description

The reactor.

There is only one global instance of this type, accessible by Reactor::get().

Fields§

§poller: Poller

Portable bindings to epoll/kqueue/event ports/IOCP.

This is where I/O is polled, producing I/O events.

§ticker: AtomicUsize

Ticker bumped before polling.

This is useful for checking what is the current “round” of ReactorLock::react() when synchronizing things in Source::readable() and Source::writable(). Both of those methods must make sure they don’t receive stale I/O events - they only accept events from a fresh “round” of ReactorLock::react().

§sources: Mutex<Slab<Arc<Source>>>

Registered sources.

§events: Mutex<Events>

Temporary storage for I/O events when polling the reactor.

Holding a lock on this event list implies the exclusive right to poll I/O.

§timers: Mutex<BTreeMap<(Instant, usize), Waker>>

An ordered map of registered timers.

Timers are in the order in which they fire. The usize in this type is a timer ID used to distinguish timers that fire at the same time. The Waker represents the task awaiting the timer.

§timer_ops: ConcurrentQueue<TimerOp>

A queue of timer operations (insert and remove).

When inserting or removing a timer, we don’t process it immediately - we just push it into this queue. Timers actually get processed when the queue fills up or the reactor is polled.

Implementations§

Source§

impl Reactor

Source

pub(crate) fn get() -> &'static Reactor

Returns a reference to the reactor.

Source

pub(crate) fn ticker(&self) -> usize

Returns the current ticker.

Source

pub(crate) fn insert_io(&self, raw: Registration) -> Result<Arc<Source>>

Registers an I/O source in the reactor.

Source

pub(crate) fn remove_io(&self, source: &Source) -> Result<()>

Deregisters an I/O source from the reactor.

Source

pub(crate) fn insert_timer(&self, when: Instant, waker: &Waker) -> usize

Registers a timer in the reactor.

Returns the inserted timer’s ID.

Source

pub(crate) fn remove_timer(&self, when: Instant, id: usize)

Deregisters a timer from the reactor.

Source

pub(crate) fn notify(&self)

Notifies the thread blocked on the reactor.

Source

pub(crate) fn lock(&self) -> ReactorLock<'_>

Locks the reactor, potentially blocking if the lock is held by another thread.

Source

pub(crate) fn try_lock(&self) -> Option<ReactorLock<'_>>

Attempts to lock the reactor.

Source

fn process_timers(&self, wakers: &mut Vec<Waker>) -> Option<Duration>

Processes ready timers and extends the list of wakers to wake.

Returns the duration until the next timer before this method was called.

Source

fn process_timer_ops( &self, timers: &mut MutexGuard<'_, BTreeMap<(Instant, usize), Waker>>, )

Processes queued timer operations.

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more