From ea30ee38f971df1001285623b23acd2894558a02 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sun, 23 Feb 2020 13:24:20 -0800 Subject: [PATCH] ircd::ctx: Optimize asio timer cancel action for ctx::wake(). --- ircd/ctx.cc | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/ircd/ctx.cc b/ircd/ctx.cc index e5aa496a7..e5d56a569 100644 --- a/ircd/ctx.cc +++ b/ircd/ctx.cc @@ -281,6 +281,40 @@ catch(const std::exception &e) return false; } +// Optimize ctx::wake() by reimplementing the timer cancel's op scheduler to +// enqueue as a defer (private/priority queue) rather than to the post queue. +#if defined(BOOST_ASIO_HAS_EPOLL) +using epoll_time_traits = boost::asio::time_traits; + +template<> +inline std::size_t +boost::asio::detail::epoll_reactor::cancel_timer(timer_queue &queue, + typename timer_queue::per_timer_data &t, + std::size_t max) +{ + std::size_t ret; + op_queue ops; + { + const mutex::scoped_lock lock(mutex_); + ret = queue.cancel_timer(t, ops, max); + } + + auto *const thread_info + { + static_cast(scheduler::thread_call_stack::top()) + }; + + for(auto *op(ops.front()); op; ops.pop(), op = ops.front()) + { + static const bool is_continuation(true); + scheduler_.post_immediate_completion(op, is_continuation); + thread_info->private_outstanding_work -= is_continuation; + } + + return ret; +} +#endif + /// Throws if this context has been flagged for interruption and clears /// the flag. [[gnu::hot]]