ts/executor: clear the reactor instead of closing it...

... so that it can be reused on current thread for subsequent
Scheduler instantiations (e.g. block_on) without the need to
reallocate internal data structures.
This commit is contained in:
François Laignel 2022-09-03 17:51:30 +02:00 committed by Sebastian Dröge
parent 61c62ee1e8
commit af12bce141
2 changed files with 16 additions and 3 deletions

View file

@ -123,9 +123,22 @@ impl Reactor {
})
}
pub fn close() {
/// Clears the `Reactor`.
///
/// It will be ready for reuse on current thread without reallocating.
pub fn clear() {
let _ = CURRENT_REACTOR.try_with(|cur_reactor| {
*cur_reactor.borrow_mut() = None;
cur_reactor.borrow_mut().as_mut().map(|reactor| {
reactor.ticker = AtomicUsize::new(0);
reactor.wakers.clear();
reactor.sources.clear();
reactor.events.clear();
reactor.timers.clear();
reactor.after_timers.clear();
while !reactor.timer_ops.is_empty() {
let _ = reactor.timer_ops.pop();
}
})
});
}

View file

@ -242,7 +242,7 @@ impl Scheduler {
context_name,
);
Reactor::close();
Reactor::clear();
let _ = CURRENT_SCHEDULER.try_with(|cur_scheduler| {
*cur_scheduler.borrow_mut() = None;