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
impl Reactor
Sourcepub(crate) fn insert_io(&self, raw: Registration) -> Result<Arc<Source>>
pub(crate) fn insert_io(&self, raw: Registration) -> Result<Arc<Source>>
Registers an I/O source in the reactor.
Sourcepub(crate) fn remove_io(&self, source: &Source) -> Result<()>
pub(crate) fn remove_io(&self, source: &Source) -> Result<()>
Deregisters an I/O source from the reactor.
Sourcepub(crate) fn insert_timer(&self, when: Instant, waker: &Waker) -> usize
pub(crate) fn insert_timer(&self, when: Instant, waker: &Waker) -> usize
Registers a timer in the reactor.
Returns the inserted timer’s ID.
Sourcepub(crate) fn remove_timer(&self, when: Instant, id: usize)
pub(crate) fn remove_timer(&self, when: Instant, id: usize)
Deregisters a timer from the reactor.
Sourcepub(crate) fn lock(&self) -> ReactorLock<'_>
pub(crate) fn lock(&self) -> ReactorLock<'_>
Locks the reactor, potentially blocking if the lock is held by another thread.
Sourcepub(crate) fn try_lock(&self) -> Option<ReactorLock<'_>>
pub(crate) fn try_lock(&self) -> Option<ReactorLock<'_>>
Attempts to lock the reactor.
Sourcefn process_timers(&self, wakers: &mut Vec<Waker>) -> Option<Duration>
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.
Sourcefn process_timer_ops(
&self,
timers: &mut MutexGuard<'_, BTreeMap<(Instant, usize), Waker>>,
)
fn process_timer_ops( &self, timers: &mut MutexGuard<'_, BTreeMap<(Instant, usize), Waker>>, )
Processes queued timer operations.