diff options
author | François Laignel <fengalin@free.fr> | 2022-09-03 18:51:30 +0300 |
---|---|---|
committer | Sebastian Dröge <slomo@coaxion.net> | 2022-09-13 10:29:50 +0300 |
commit | af12bce141f5f3ec03e0128ef34a203fa7df9691 (patch) | |
tree | 0182c0c20a5716be79179eee9669aa7c11a188d4 /generic | |
parent | 61c62ee1e85d48578a11e4e84366885fcd0fe4ac (diff) |
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.
Diffstat (limited to 'generic')
-rw-r--r-- | generic/threadshare/src/runtime/executor/reactor.rs | 17 | ||||
-rw-r--r-- | generic/threadshare/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; |