Ensure call_stacks are accessed only from implementation files.

This commit is contained in:
Christopher Kohlhoff 2021-03-02 23:08:04 +11:00
parent eb61fc9b93
commit 845d8bfb3c
22 changed files with 136 additions and 101 deletions

View File

@ -119,6 +119,7 @@ nobase_include_HEADERS = \
asio/detail/impl/strand_executor_service.ipp \
asio/detail/impl/strand_service.hpp \
asio/detail/impl/strand_service.ipp \
asio/detail/impl/thread_context.ipp \
asio/detail/impl/throw_error.ipp \
asio/detail/impl/timer_queue_ptime.ipp \
asio/detail/impl/timer_queue_set.ipp \

View File

@ -61,7 +61,7 @@ inline void* allocate(std::size_t s, Handler& h)
(void)h;
#if !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
return asio::detail::thread_info_base::allocate(
asio::detail::thread_context::thread_call_stack::top(), s);
asio::detail::thread_context::top_of_thread_call_stack(), s);
#else // !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
return ::operator new(size);
#endif // !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
@ -82,7 +82,7 @@ inline void deallocate(void* p, std::size_t s, Handler& h)
(void)h;
#if !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
asio::detail::thread_info_base::deallocate(
asio::detail::thread_context::thread_call_stack::top(), p, s);
asio::detail::thread_context::top_of_thread_call_stack(), p, s);
#else // !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
(void)s;
::operator delete(p);

View File

@ -324,6 +324,11 @@ void scheduler::compensating_work_started()
++static_cast<thread_info*>(this_thread)->private_outstanding_work;
}
bool scheduler::can_dispatch()
{
return thread_call_stack::contains(this) != 0;
}
void scheduler::capture_current_exception()
{
if (thread_info_base* this_thread = thread_call_stack::contains(this))

View File

@ -15,7 +15,6 @@
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include "asio/detail/call_stack.hpp"
#include "asio/detail/fenced_block.hpp"
#include "asio/detail/handler_invoke_helpers.hpp"
#include "asio/detail/recycling_allocator.hpp"
@ -103,13 +102,7 @@ public:
~on_invoker_exit()
{
this_->impl_->mutex_->lock();
this_->impl_->ready_queue_.push(this_->impl_->waiting_queue_);
bool more_handlers = this_->impl_->locked_ =
!this_->impl_->ready_queue_.empty();
this_->impl_->mutex_->unlock();
if (more_handlers)
if (push_waiting_to_ready(this_->impl_))
{
recycling_allocator<void> allocator;
execution::execute(
@ -124,21 +117,11 @@ public:
void operator()()
{
// Indicate that this strand is executing on the current thread.
call_stack<strand_impl>::context ctx(impl_.get());
// Ensure the next handler, if any, is scheduled on block exit.
on_invoker_exit on_exit = { this };
(void)on_exit;
// Run all ready handlers. No lock is required since the ready queue is
// accessed only within the strand.
asio::error_code ec;
while (scheduler_operation* o = impl_->ready_queue_.front())
{
impl_->ready_queue_.pop();
o->complete(impl_.get(), ec, 0);
}
run_ready_handlers(impl_);
}
private:
@ -188,13 +171,7 @@ public:
~on_invoker_exit()
{
this_->impl_->mutex_->lock();
this_->impl_->ready_queue_.push(this_->impl_->waiting_queue_);
bool more_handlers = this_->impl_->locked_ =
!this_->impl_->ready_queue_.empty();
this_->impl_->mutex_->unlock();
if (more_handlers)
if (push_waiting_to_ready(this_->impl_))
{
Executor ex(this_->work_.get_executor());
recycling_allocator<void> allocator;
@ -205,21 +182,11 @@ public:
void operator()()
{
// Indicate that this strand is executing on the current thread.
call_stack<strand_impl>::context ctx(impl_.get());
// Ensure the next handler, if any, is scheduled on block exit.
on_invoker_exit on_exit = { this };
(void)on_exit;
// Run all ready handlers. No lock is required since the ready queue is
// accessed only within the strand.
asio::error_code ec;
while (scheduler_operation* o = impl_->ready_queue_.front())
{
impl_->ready_queue_.pop();
o->complete(impl_.get(), ec, 0);
}
run_ready_handlers(impl_);
}
private:
@ -262,7 +229,7 @@ void strand_executor_service::do_execute(const implementation_type& impl,
// If the executor is not never-blocking, and we are already in the strand,
// then the function can run immediately.
if (asio::query(ex, execution::blocking) != execution::blocking.never
&& call_stack<strand_impl>::contains(impl.get()))
&& running_in_this_thread(impl))
{
// Make a local, non-const copy of the function.
function_type tmp(ASIO_MOVE_CAST(Function)(function));
@ -296,7 +263,7 @@ void strand_executor_service::dispatch(const implementation_type& impl,
typedef typename decay<Function>::type function_type;
// If we are already in the strand then the function can run immediately.
if (call_stack<strand_impl>::contains(impl.get()))
if (running_in_this_thread(impl))
{
// Make a local, non-const copy of the function.
function_type tmp(ASIO_MOVE_CAST(Function)(function));

View File

@ -126,6 +126,30 @@ bool strand_executor_service::running_in_this_thread(
return !!call_stack<strand_impl>::contains(impl.get());
}
bool strand_executor_service::push_waiting_to_ready(implementation_type& impl)
{
impl->mutex_->lock();
impl->ready_queue_.push(impl->waiting_queue_);
bool more_handlers = impl->locked_ = !impl->ready_queue_.empty();
impl->mutex_->unlock();
return more_handlers;
}
void strand_executor_service::run_ready_handlers(implementation_type& impl)
{
// Indicate that this strand is executing on the current thread.
call_stack<strand_impl>::context ctx(impl.get());
// Run all ready handlers. No lock is required since the ready queue is
// accessed only within the strand.
asio::error_code ec;
while (scheduler_operation* o = impl->ready_queue_.front())
{
impl->ready_queue_.pop();
o->complete(impl.get(), ec, 0);
}
}
} // namespace detail
} // namespace asio

View File

@ -15,7 +15,6 @@
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include "asio/detail/call_stack.hpp"
#include "asio/detail/completion_handler.hpp"
#include "asio/detail/fenced_block.hpp"
#include "asio/detail/handler_alloc_helpers.hpp"
@ -33,29 +32,12 @@ inline strand_service::strand_impl::strand_impl()
{
}
struct strand_service::on_dispatch_exit
{
io_context_impl* io_context_impl_;
strand_impl* impl_;
~on_dispatch_exit()
{
impl_->mutex_.lock();
impl_->ready_queue_.push(impl_->waiting_queue_);
bool more_handlers = impl_->locked_ = !impl_->ready_queue_.empty();
impl_->mutex_.unlock();
if (more_handlers)
io_context_impl_->post_immediate_completion(impl_, false);
}
};
template <typename Handler>
void strand_service::dispatch(strand_service::implementation_type& impl,
Handler& handler)
{
// If we are already in the strand then the handler can run immediately.
if (call_stack<strand_impl>::contains(impl))
if (running_in_this_thread(impl))
{
fenced_block b(fenced_block::full);
asio_handler_invoke_helpers::invoke(handler, handler);
@ -71,21 +53,9 @@ void strand_service::dispatch(strand_service::implementation_type& impl,
ASIO_HANDLER_CREATION((this->context(),
*p.p, "strand", impl, 0, "dispatch"));
bool dispatch_immediately = do_dispatch(impl, p.p);
operation* o = p.p;
p.v = p.p = 0;
if (dispatch_immediately)
{
// Indicate that this strand is executing on the current thread.
call_stack<strand_impl>::context ctx(impl);
// Ensure the next handler, if any, is scheduled on block exit.
on_dispatch_exit on_exit = { &io_context_impl_, impl };
(void)on_exit;
op::do_complete(&io_context_impl_, o, asio::error_code(), 0);
}
do_dispatch(impl, o);
}
// Request the io_context to invoke the given handler and return immediately.

View File

@ -91,7 +91,24 @@ bool strand_service::running_in_this_thread(
return call_stack<strand_impl>::contains(impl) != 0;
}
bool strand_service::do_dispatch(implementation_type& impl, operation* op)
struct strand_service::on_dispatch_exit
{
io_context_impl* io_context_impl_;
strand_impl* impl_;
~on_dispatch_exit()
{
impl_->mutex_.lock();
impl_->ready_queue_.push(impl_->waiting_queue_);
bool more_handlers = impl_->locked_ = !impl_->ready_queue_.empty();
impl_->mutex_.unlock();
if (more_handlers)
io_context_impl_->post_immediate_completion(impl_, false);
}
};
void strand_service::do_dispatch(implementation_type& impl, operation* op)
{
// If we are running inside the io_context, and no other handler already
// holds the strand lock, then the handler can run immediately.
@ -102,7 +119,16 @@ bool strand_service::do_dispatch(implementation_type& impl, operation* op)
// Immediate invocation is allowed.
impl->locked_ = true;
impl->mutex_.unlock();
return true;
// Indicate that this strand is executing on the current thread.
call_stack<strand_impl>::context ctx(impl);
// Ensure the next handler, if any, is scheduled on block exit.
on_dispatch_exit on_exit = { &io_context_impl_, impl };
(void)on_exit;
op->complete(&io_context_impl_, asio::error_code(), 0);
return;
}
if (impl->locked_)
@ -120,8 +146,6 @@ bool strand_service::do_dispatch(implementation_type& impl, operation* op)
impl->ready_queue_.push(op);
io_context_impl_.post_immediate_completion(impl, false);
}
return false;
}
void strand_service::do_post(implementation_type& impl,

View File

@ -0,0 +1,35 @@
//
// detail/impl/thread_context.ipp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef ASIO_DETAIL_IMPL_THREAD_CONTEXT_IPP
#define ASIO_DETAIL_IMPL_THREAD_CONTEXT_IPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include "asio/detail/config.hpp"
#include "asio/detail/push_options.hpp"
namespace asio {
namespace detail {
thread_info_base* thread_context::top_of_thread_call_stack()
{
return thread_call_stack::top();
}
} // namespace detail
} // namespace asio
#include "asio/detail/pop_options.hpp"
#endif // ASIO_DETAIL_IMPL_THREAD_CONTEXT_IPP

View File

@ -287,6 +287,11 @@ void win_iocp_io_context::stop()
}
}
bool win_iocp_io_context::can_dispatch()
{
return thread_call_stack::contains(this) != 0;
}
void win_iocp_io_context::capture_current_exception()
{
if (thread_info_base* this_thread = thread_call_stack::contains(this))

View File

@ -48,17 +48,15 @@ public:
T* allocate(std::size_t n)
{
typedef thread_context::thread_call_stack call_stack;
void* p = thread_info_base::allocate(Purpose(),
call_stack::top(), sizeof(T) * n);
thread_context::top_of_thread_call_stack(), sizeof(T) * n);
return static_cast<T*>(p);
}
void deallocate(T* p, std::size_t n)
{
typedef thread_context::thread_call_stack call_stack;
thread_info_base::deallocate(Purpose(),
call_stack::top(), p, sizeof(T) * n);
thread_context::top_of_thread_call_stack(), p, sizeof(T) * n);
}
};

View File

@ -99,10 +99,7 @@ public:
}
// Return whether a handler can be dispatched immediately.
bool can_dispatch()
{
return thread_call_stack::contains(this) != 0;
}
ASIO_DECL bool can_dispatch();
/// Capture the current exception so it can be rethrown from a run function.
ASIO_DECL void capture_current_exception();

View File

@ -131,6 +131,13 @@ private:
ASIO_DECL static bool enqueue(const implementation_type& impl,
scheduler_operation* op);
// Transfers waiting handlers to the ready queue. Returns true if one or more
// handlers were transferred.
ASIO_DECL static bool push_waiting_to_ready(implementation_type& impl);
// Invokes all ready-to-run handlers.
ASIO_DECL static void run_ready_handlers(implementation_type& impl);
// Helper function to request invocation of the given function.
template <typename Executor, typename Function, typename Allocator>
static void do_execute(const implementation_type& impl, Executor& ex,

View File

@ -96,11 +96,10 @@ public:
const implementation_type& impl) const;
private:
// Helper function to dispatch a handler. Returns true if the handler should
// be dispatched immediately.
ASIO_DECL bool do_dispatch(implementation_type& impl, operation* op);
// Helper function to dispatch a handler.
ASIO_DECL void do_dispatch(implementation_type& impl, operation* op);
// Helper fiunction to post a handler.
// Helper function to post a handler.
ASIO_DECL void do_post(implementation_type& impl,
operation* op, bool is_continuation);

View File

@ -30,6 +30,11 @@ class thread_info_base;
class thread_context
{
public:
// Obtain a pointer to the top of the thread call stack. Returns null when
// not running inside a thread context.
ASIO_DECL static thread_info_base* top_of_thread_call_stack();
protected:
// Per-thread call stack to track the state of each thread in the context.
typedef call_stack<thread_context, thread_info_base> thread_call_stack;
};
@ -39,4 +44,8 @@ public:
#include "asio/detail/pop_options.hpp"
#if defined(ASIO_HEADER_ONLY)
# include "asio/detail/impl/thread_context.ipp"
#endif // defined(ASIO_HEADER_ONLY)
#endif // ASIO_DETAIL_THREAD_CONTEXT_HPP

View File

@ -109,10 +109,7 @@ public:
}
// Return whether a handler can be dispatched immediately.
bool can_dispatch()
{
return thread_call_stack::contains(this) != 0;
}
ASIO_DECL bool can_dispatch();
/// Capture the current exception so it can be rethrown from a run function.
ASIO_DECL void capture_current_exception();

View File

@ -77,7 +77,7 @@ public:
{
return asio::detail::thread_info_base::allocate(
asio::detail::thread_info_base::awaitable_frame_tag(),
asio::detail::thread_context::thread_call_stack::top(),
asio::detail::thread_context::top_of_thread_call_stack(),
size);
}
@ -85,7 +85,7 @@ public:
{
asio::detail::thread_info_base::deallocate(
asio::detail::thread_info_base::awaitable_frame_tag(),
asio::detail::thread_context::thread_call_stack::top(),
asio::detail::thread_context::top_of_thread_call_stack(),
pointer, size);
}
#endif // !defined(ASIO_DISABLE_AWAITABLE_FRAME_RECYCLING)

View File

@ -22,7 +22,6 @@
#include "asio/detail/atomic_count.hpp"
#include "asio/detail/global.hpp"
#include "asio/detail/memory.hpp"
#include "asio/detail/recycling_allocator.hpp"
#include "asio/executor.hpp"
#include "asio/system_executor.hpp"

View File

@ -32,7 +32,7 @@ asio_handler_allocate(std::size_t size, ...)
return asio_handler_allocate_is_no_longer_used();
#elif !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
return detail::thread_info_base::allocate(
detail::thread_context::thread_call_stack::top(), size);
detail::thread_context::top_of_thread_call_stack(), size);
#else // !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
return ::operator new(size);
#endif // !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
@ -47,7 +47,7 @@ asio_handler_deallocate(void* pointer, std::size_t size, ...)
return asio_handler_deallocate_is_no_longer_used();
#elif !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
detail::thread_info_base::deallocate(
detail::thread_context::thread_call_stack::top(), pointer, size);
detail::thread_context::top_of_thread_call_stack(), pointer, size);
#else // !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
(void)size;
::operator delete(pointer);

View File

@ -20,7 +20,6 @@
#include "asio/detail/fenced_block.hpp"
#include "asio/detail/handler_type_requirements.hpp"
#include "asio/detail/non_const_lvalue.hpp"
#include "asio/detail/recycling_allocator.hpp"
#include "asio/detail/service_registry.hpp"
#include "asio/detail/throw_error.hpp"
#include "asio/detail/type_traits.hpp"

View File

@ -54,6 +54,7 @@
#include "asio/detail/impl/socket_select_interrupter.ipp"
#include "asio/detail/impl/strand_executor_service.ipp"
#include "asio/detail/impl/strand_service.ipp"
#include "asio/detail/impl/thread_context.ipp"
#include "asio/detail/impl/throw_error.ipp"
#include "asio/detail/impl/timer_queue_ptime.ipp"
#include "asio/detail/impl/timer_queue_set.ipp"

View File

@ -17,7 +17,6 @@
#include "asio/detail/executor_op.hpp"
#include "asio/detail/global.hpp"
#include "asio/detail/recycling_allocator.hpp"
#include "asio/detail/type_traits.hpp"
#include "asio/system_context.hpp"

View File

@ -20,7 +20,6 @@
#include "asio/detail/executor_op.hpp"
#include "asio/detail/fenced_block.hpp"
#include "asio/detail/non_const_lvalue.hpp"
#include "asio/detail/recycling_allocator.hpp"
#include "asio/detail/type_traits.hpp"
#include "asio/execution_context.hpp"