mirror of
https://github.com/boostorg/beast.git
synced 2025-07-31 21:34:46 +02:00
Update bind_handler test to use standard executor form
This commit is contained in:
committed by
Richard Hodges
parent
ca95101a88
commit
107b01ad24
@@ -14,6 +14,7 @@
|
||||
#include <boost/beast/core/buffer_traits.hpp>
|
||||
#include <boost/beast/core/detail/service_base.hpp>
|
||||
#include <boost/beast/core/detail/is_invocable.hpp>
|
||||
#include <boost/asio/any_io_executor.hpp>
|
||||
#include <boost/asio/dispatch.hpp>
|
||||
#include <boost/asio/post.hpp>
|
||||
#include <mutex>
|
||||
@@ -74,7 +75,11 @@ class stream::read_op : public stream::read_op_base
|
||||
Handler h_;
|
||||
boost::weak_ptr<state> wp_;
|
||||
Buffers b_;
|
||||
#if defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
net::any_io_executor wg2_;
|
||||
#else // defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
net::executor_work_guard<ex2_type> wg2_;
|
||||
#endif // defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
|
||||
lambda(lambda&&) = default;
|
||||
lambda(lambda const&) = default;
|
||||
@@ -87,8 +92,15 @@ class stream::read_op : public stream::read_op_base
|
||||
: h_(std::forward<Handler_>(h))
|
||||
, wp_(s)
|
||||
, b_(b)
|
||||
#if defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
, wg2_(net::prefer(
|
||||
net::get_associated_executor(
|
||||
h_, s->ioc.get_executor()),
|
||||
net::execution::outstanding_work.tracked))
|
||||
#else // defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
, wg2_(net::get_associated_executor(
|
||||
h_, s->ioc.get_executor()))
|
||||
#endif // defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -124,15 +136,26 @@ class stream::read_op : public stream::read_op_base
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
net::dispatch(wg2_,
|
||||
beast::bind_front_handler(std::move(h_),
|
||||
ec, bytes_transferred));
|
||||
wg2_ = net::any_io_executor(); // probably unnecessary
|
||||
#else // defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
net::dispatch(wg2_.get_executor(),
|
||||
beast::bind_front_handler(std::move(h_),
|
||||
ec, bytes_transferred));
|
||||
wg2_.reset();
|
||||
#endif // defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
}
|
||||
};
|
||||
|
||||
lambda fn_;
|
||||
#if defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
net::any_io_executor wg1_;
|
||||
#else // defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
net::executor_work_guard<ex1_type> wg1_;
|
||||
#endif // defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
|
||||
public:
|
||||
template<class Handler_>
|
||||
@@ -141,16 +164,26 @@ public:
|
||||
boost::shared_ptr<state> const& s,
|
||||
Buffers const& b)
|
||||
: fn_(std::forward<Handler_>(h), s, b)
|
||||
#if defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
, wg1_(net::prefer(s->ioc.get_executor(),
|
||||
net::execution::outstanding_work.tracked))
|
||||
#else // defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
, wg1_(s->ioc.get_executor())
|
||||
#endif // defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
operator()(error_code ec) override
|
||||
{
|
||||
#if defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
net::post(wg1_, beast::bind_front_handler(std::move(fn_), ec));
|
||||
wg1_ = net::any_io_executor(); // probably unnecessary
|
||||
#else // defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
net::post(wg1_.get_executor(),
|
||||
beast::bind_front_handler(std::move(fn_), ec));
|
||||
wg1_.reset();
|
||||
#endif // defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
}
|
||||
};
|
||||
|
||||
|
@@ -359,7 +359,11 @@ teardown(
|
||||
stream
|
||||
connect(stream& to)
|
||||
{
|
||||
#if defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
stream from{net::query(to.get_executor(), net::execution::context)};
|
||||
#else // defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
stream from{to.get_executor().context()};
|
||||
#endif // defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
from.connect(to);
|
||||
return from;
|
||||
}
|
||||
|
@@ -63,14 +63,26 @@ class saved_handler::impl final : public base
|
||||
};
|
||||
|
||||
ebo_pair v_;
|
||||
#if defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
std::decay_t<decltype(net::prefer(
|
||||
std::declval<net::associated_executor_t<Handler>>(),
|
||||
net::execution::outstanding_work.tracked))> wg2_;
|
||||
#else // defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
net::executor_work_guard<
|
||||
net::associated_executor_t<Handler>> wg2_;
|
||||
#endif // defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
|
||||
public:
|
||||
template<class Handler_>
|
||||
impl(alloc_type const& a, Handler_&& h)
|
||||
: v_(a, std::forward<Handler_>(h))
|
||||
#if defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
, wg2_(net::prefer(
|
||||
net::get_associated_executor(v_.h),
|
||||
net::execution::outstanding_work.tracked))
|
||||
#else // defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
, wg2_(net::get_associated_executor(v_.h))
|
||||
#endif // defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
{
|
||||
}
|
||||
|
||||
|
@@ -80,7 +80,18 @@ public:
|
||||
class test_executor
|
||||
{
|
||||
bind_handler_test& s_;
|
||||
net::io_context::executor_type ex_;
|
||||
net::any_io_executor ex_;
|
||||
|
||||
// Storing the blocking property as a member is not strictly necessary,
|
||||
// as we could simply forward the calls
|
||||
// require(ex_, blocking.possibly)
|
||||
// and
|
||||
// require(ex_, blocking.never)
|
||||
// to the underlying executor, and then
|
||||
// query(ex_, blocking)
|
||||
// when required. This forwarding approach is used here for the
|
||||
// outstanding_work property.
|
||||
net::execution::blocking_t blocking_;
|
||||
|
||||
public:
|
||||
test_executor(
|
||||
@@ -91,6 +102,7 @@ public:
|
||||
net::io_context& ioc)
|
||||
: s_(s)
|
||||
, ex_(ioc.get_executor())
|
||||
, blocking_(net::execution::blocking.possibly)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -106,7 +118,67 @@ public:
|
||||
return ex_ != other.ex_;
|
||||
}
|
||||
|
||||
net::io_context&
|
||||
net::execution_context& query(net::execution::context_t c) const noexcept
|
||||
{
|
||||
return net::query(ex_, c);
|
||||
}
|
||||
|
||||
net::execution::blocking_t query(net::execution::blocking_t) const noexcept
|
||||
{
|
||||
return blocking_;
|
||||
}
|
||||
|
||||
net::execution::outstanding_work_t query(net::execution::outstanding_work_t w) const noexcept
|
||||
{
|
||||
return net::query(ex_, w);
|
||||
}
|
||||
|
||||
test_executor require(net::execution::blocking_t::possibly_t b) const
|
||||
{
|
||||
test_executor new_ex(*this);
|
||||
new_ex.blocking_ = b;
|
||||
return new_ex;
|
||||
}
|
||||
|
||||
test_executor require(net::execution::blocking_t::never_t b) const
|
||||
{
|
||||
test_executor new_ex(*this);
|
||||
new_ex.blocking_ = b;
|
||||
return new_ex;
|
||||
}
|
||||
|
||||
test_executor prefer(net::execution::outstanding_work_t::untracked_t w) const
|
||||
{
|
||||
test_executor new_ex(*this);
|
||||
new_ex.ex_ = net::prefer(ex_, w);
|
||||
return new_ex;
|
||||
}
|
||||
|
||||
test_executor prefer(net::execution::outstanding_work_t::tracked_t w) const
|
||||
{
|
||||
test_executor new_ex(*this);
|
||||
new_ex.ex_ = net::prefer(ex_, w);
|
||||
return new_ex;
|
||||
}
|
||||
|
||||
template<class F>
|
||||
void execute(F&& f) const
|
||||
{
|
||||
if (blocking_ == net::execution::blocking.possibly)
|
||||
{
|
||||
s_.on_invoke();
|
||||
net::execution::execute(ex_, std::forward<F>(f));
|
||||
}
|
||||
else
|
||||
{
|
||||
// shouldn't be called since the enclosing
|
||||
// networking wrapper only uses dispatch
|
||||
BEAST_FAIL();
|
||||
}
|
||||
}
|
||||
|
||||
#if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
net::execution_context&
|
||||
context() const noexcept
|
||||
{
|
||||
return ex_.context();
|
||||
@@ -144,6 +216,7 @@ public:
|
||||
// networking wrapper only uses dispatch
|
||||
BEAST_FAIL();
|
||||
}
|
||||
#endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
};
|
||||
|
||||
class test_cb
|
||||
|
Reference in New Issue
Block a user