From af12bce141f5f3ec03e0128ef34a203fa7df9691 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Laignel?= Date: Sat, 3 Sep 2022 17:51:30 +0200 Subject: [PATCH] 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. --- .../threadshare/src/runtime/executor/reactor.rs | 17 +++++++++++++++-- .../src/runtime/executor/scheduler.rs | 2 +- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/generic/threadshare/src/runtime/executor/reactor.rs b/generic/threadshare/src/runtime/executor/reactor.rs index d93ba9a0..86fa079b 100644 --- a/generic/threadshare/src/runtime/executor/reactor.rs +++ b/generic/threadshare/src/runtime/executor/reactor.rs @@ -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(); + } + }) }); } diff --git a/generic/threadshare/src/runtime/executor/scheduler.rs b/generic/threadshare/src/runtime/executor/scheduler.rs index 9f862a66..ba4d6fa6 100644 --- a/generic/threadshare/src/runtime/executor/scheduler.rs +++ b/generic/threadshare/src/runtime/executor/scheduler.rs @@ -242,7 +242,7 @@ impl Scheduler { context_name, ); - Reactor::close(); + Reactor::clear(); let _ = CURRENT_SCHEDULER.try_with(|cur_scheduler| { *cur_scheduler.borrow_mut() = None;