forked from boostorg/beast
Support BOOST_ASIO_NO_TS_EXECUTORS
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
#include <boost/core/ignore_unused.hpp>
|
||||
#include <stdexcept>
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
namespace boost {
|
||||
@@ -32,6 +33,52 @@ namespace beast {
|
||||
|
||||
namespace {
|
||||
|
||||
#if defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
struct ex1_type
|
||||
{
|
||||
|
||||
net::execution_context &
|
||||
query(net::execution::context_t c) const noexcept
|
||||
{ return *reinterpret_cast<net::execution_context *>(0); }
|
||||
|
||||
net::execution::blocking_t
|
||||
query(net::execution::blocking_t) const noexcept
|
||||
{ return net::execution::blocking; };
|
||||
|
||||
net::execution::outstanding_work_t
|
||||
query(net::execution::outstanding_work_t w) const noexcept
|
||||
{ return net::execution::outstanding_work; }
|
||||
|
||||
ex1_type
|
||||
require(net::execution::blocking_t::possibly_t b) const
|
||||
{ return *this; }
|
||||
|
||||
ex1_type
|
||||
require(net::execution::blocking_t::never_t b) const
|
||||
{ return *this; };
|
||||
|
||||
ex1_type
|
||||
prefer(net::execution::outstanding_work_t::untracked_t w) const
|
||||
{ return *this; };
|
||||
|
||||
ex1_type
|
||||
prefer(net::execution::outstanding_work_t::tracked_t w) const
|
||||
{ return *this; };
|
||||
|
||||
template<class F>
|
||||
void
|
||||
execute(F &&) const
|
||||
{}
|
||||
|
||||
bool
|
||||
operator==(ex1_type const &) const noexcept
|
||||
{ return true; }
|
||||
bool
|
||||
operator!=(ex1_type const &) const noexcept
|
||||
{ return false; }
|
||||
};
|
||||
BOOST_STATIC_ASSERT(net::execution::is_executor<ex1_type>::value);
|
||||
#else
|
||||
struct ex1_type
|
||||
{
|
||||
void* context() { return nullptr; }
|
||||
@@ -41,6 +88,9 @@ struct ex1_type
|
||||
template<class F> void post(F&&) {}
|
||||
template<class F> void defer(F&&) {}
|
||||
};
|
||||
BOOST_STATIC_ASSERT(net::is_executor<ex1_type>::value);
|
||||
#endif
|
||||
|
||||
|
||||
struct no_alloc
|
||||
{
|
||||
@@ -430,11 +480,13 @@ public:
|
||||
}
|
||||
{
|
||||
net::io_context ioc;
|
||||
async_base<
|
||||
test::handler,
|
||||
net::io_context::executor_type> op(
|
||||
test::any_handler(), ioc.get_executor());
|
||||
op.complete(false);
|
||||
auto op = new
|
||||
async_base<
|
||||
test::handler,
|
||||
net::io_context::executor_type>(
|
||||
test::any_handler(), ioc.get_executor());
|
||||
op->complete(false);
|
||||
delete op;
|
||||
ioc.run();
|
||||
}
|
||||
{
|
||||
@@ -506,12 +558,13 @@ public:
|
||||
net::io_context ioc1;
|
||||
net::io_context ioc2;
|
||||
auto h = net::bind_executor(ioc2, test::any_handler());
|
||||
stable_async_base<
|
||||
auto op = new stable_async_base<
|
||||
decltype(h),
|
||||
net::io_context::executor_type> op(
|
||||
net::io_context::executor_type>(
|
||||
std::move(h),
|
||||
ioc1.get_executor());
|
||||
op.complete(false);
|
||||
op->complete(false);
|
||||
delete op;
|
||||
BEAST_EXPECT(ioc1.run() == 0);
|
||||
BEAST_EXPECT(ioc2.run() == 1);
|
||||
}
|
||||
@@ -678,7 +731,9 @@ public:
|
||||
: base_type(std::move(handler), stream.get_executor())
|
||||
, stream_(stream)
|
||||
, repeats_(repeats)
|
||||
, data_(allocate_stable<temporary_data>(*this, std::move(message), stream.get_executor().context()))
|
||||
, data_(allocate_stable<temporary_data>(*this,
|
||||
std::move(message),
|
||||
net::query(stream.get_executor(), net::execution::context)))
|
||||
{
|
||||
(*this)(); // start the operation
|
||||
}
|
||||
|
||||
@@ -1226,6 +1226,7 @@ public:
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
void process_http_1 (tcp_stream& stream, net::yield_context yield)
|
||||
{
|
||||
flat_buffer buffer;
|
||||
|
||||
@@ -80,11 +80,9 @@ public:
|
||||
class test_executor
|
||||
{
|
||||
bind_handler_test& s_;
|
||||
|
||||
#if defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
net::any_io_executor ex_;
|
||||
#else
|
||||
net::io_context::executor_type ex_;
|
||||
#endif
|
||||
|
||||
// Storing the blocking property as a member is not strictly necessary,
|
||||
// as we could simply forward the calls
|
||||
@@ -97,6 +95,9 @@ public:
|
||||
// outstanding_work property.
|
||||
net::execution::blocking_t blocking_;
|
||||
|
||||
#else // defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
net::io_context::executor_type ex_;
|
||||
#endif // defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
public:
|
||||
test_executor(
|
||||
test_executor const&) = default;
|
||||
@@ -106,7 +107,9 @@ public:
|
||||
net::io_context& ioc)
|
||||
: s_(s)
|
||||
, ex_(ioc.get_executor())
|
||||
#if defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
, blocking_(net::execution::blocking.possibly)
|
||||
#endif
|
||||
{
|
||||
}
|
||||
|
||||
@@ -123,6 +126,7 @@ public:
|
||||
}
|
||||
|
||||
#if defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
|
||||
net::execution_context& query(net::execution::context_t c) const noexcept
|
||||
{
|
||||
return net::query(ex_, c);
|
||||
@@ -182,7 +186,6 @@ public:
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
net::execution_context&
|
||||
context() const noexcept
|
||||
@@ -247,7 +250,7 @@ public:
|
||||
BOOST_STATIC_ASSERT(std::is_nothrow_destructible<T>::value);
|
||||
BOOST_STATIC_ASSERT(boost::asio::traits::equality_comparable<T>::is_valid);
|
||||
BOOST_STATIC_ASSERT(boost::asio::traits::equality_comparable<T>::is_noexcept);
|
||||
BOOST_STATIC_ASSERT(net::execution::is_executor_v<test_executor>);
|
||||
BOOST_STATIC_ASSERT(net::execution::is_executor<test_executor>::value);
|
||||
#else
|
||||
BOOST_STATIC_ASSERT(net::is_executor<test_executor>::value);
|
||||
#endif
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
#include <boost/asio/buffer.hpp>
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/read.hpp>
|
||||
#include <boost/asio/spawn.hpp>
|
||||
#include <boost/asio/strand.hpp>
|
||||
#include <boost/optional.hpp>
|
||||
#if BOOST_ASIO_HAS_CO_AWAIT
|
||||
@@ -42,9 +41,10 @@ public:
|
||||
buffered_read_stream<test::stream, multi_buffer> srs(ioc);
|
||||
buffered_read_stream<test::stream, multi_buffer> srs2(std::move(srs));
|
||||
srs = std::move(srs2);
|
||||
BEAST_EXPECT(&srs.get_executor().context() == &ioc);
|
||||
BEAST_EXPECT(&net::query(srs.get_executor(), net::execution::context) == &ioc);
|
||||
BEAST_EXPECT(
|
||||
&srs.get_executor().context() == &srs2.get_executor().context());
|
||||
&net::query(srs.get_executor(), net::execution::context) ==
|
||||
&net::query(srs2.get_executor(), net::execution::context));
|
||||
}
|
||||
{
|
||||
test::stream ts{ioc};
|
||||
@@ -238,7 +238,6 @@ public:
|
||||
{
|
||||
testRead(yield);
|
||||
});
|
||||
|
||||
testAsyncLoop();
|
||||
|
||||
#if BOOST_ASIO_HAS_CO_AWAIT
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
#include <boost/beast/core/error.hpp>
|
||||
#include <boost/beast/core/stream_traits.hpp>
|
||||
#include <boost/asio/buffer.hpp>
|
||||
#include <boost/asio/spawn.hpp>
|
||||
#include <boost/asio/use_future.hpp>
|
||||
|
||||
namespace boost {
|
||||
|
||||
@@ -57,7 +57,7 @@ class simple_executor
|
||||
std::size_t id_;
|
||||
|
||||
public:
|
||||
simple_executor()
|
||||
simple_executor() noexcept
|
||||
: id_([]
|
||||
{
|
||||
static std::size_t n = 0;
|
||||
@@ -66,12 +66,19 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
#if defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
void* query(net::execution::context_t) const { return nullptr; }
|
||||
template<class F>
|
||||
void execute(F&&) const {}
|
||||
simple_executor prefer(net::execution::outstanding_work_t::tracked_t) const { return *this; }
|
||||
#else
|
||||
void* context() { return nullptr; }
|
||||
void on_work_started() {}
|
||||
void on_work_finished() {}
|
||||
template<class F> void dispatch(F&&) {}
|
||||
template<class F> void post(F&&) {}
|
||||
template<class F> void defer(F&&) {}
|
||||
#endif
|
||||
|
||||
friend
|
||||
bool operator==(
|
||||
@@ -90,6 +97,15 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
#if defined(BOOST_ASIO_NO_TS_EXECUTORS)
|
||||
static_assert(net::execution::can_execute<simple_executor, net::execution::invocable_archetype>::value, "");
|
||||
static_assert(std::is_nothrow_copy_constructible<simple_executor>::value, "");
|
||||
static_assert(std::is_nothrow_destructible<simple_executor>::value, "");
|
||||
static_assert(net::traits::equality_comparable<simple_executor>::is_valid, "");
|
||||
static_assert(net::traits::equality_comparable<simple_executor>::is_noexcept, "");
|
||||
static_assert(net::execution::is_executor<simple_executor>::value, "");
|
||||
#endif
|
||||
|
||||
// A move-only handler
|
||||
struct move_only_handler
|
||||
{
|
||||
|
||||
@@ -72,8 +72,7 @@ public:
|
||||
|
||||
std::ostream& log_;
|
||||
net::io_context ioc_;
|
||||
net::executor_work_guard<
|
||||
net::io_context::executor_type> work_;
|
||||
net::any_io_executor work_;
|
||||
static_buffer<buf_size> buffer_;
|
||||
test::stream ts_;
|
||||
std::thread t_;
|
||||
@@ -86,7 +85,7 @@ public:
|
||||
std::ostream& log,
|
||||
kind k = kind::sync)
|
||||
: log_(log)
|
||||
, work_(ioc_.get_executor())
|
||||
, work_(net::prefer(ioc_.get_executor(), net::execution::outstanding_work.tracked))
|
||||
, ts_(ioc_)
|
||||
, ws_(ts_)
|
||||
{
|
||||
@@ -115,7 +114,7 @@ public:
|
||||
|
||||
~echo_server()
|
||||
{
|
||||
work_.reset();
|
||||
work_ = {};
|
||||
t_.join();
|
||||
}
|
||||
|
||||
@@ -1249,8 +1248,8 @@ public:
|
||||
{
|
||||
error_code ec;
|
||||
ws.async_accept(handler(ec));
|
||||
ws.get_executor().context().run();
|
||||
ws.get_executor().context().restart();
|
||||
net::query(ws.get_executor(), net::execution::context).run();
|
||||
net::query(ws.get_executor(), net::execution::context).restart();
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
}
|
||||
@@ -1266,8 +1265,8 @@ public:
|
||||
{
|
||||
error_code ec;
|
||||
ws.async_accept(buffers, handler(ec));
|
||||
ws.get_executor().context().run();
|
||||
ws.get_executor().context().restart();
|
||||
net::query(ws.get_executor(), net::execution::context).run();
|
||||
net::query(ws.get_executor(), net::execution::context).restart();
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
}
|
||||
@@ -1280,8 +1279,8 @@ public:
|
||||
{
|
||||
error_code ec;
|
||||
ws.async_accept(req, handler(ec));
|
||||
ws.get_executor().context().run();
|
||||
ws.get_executor().context().restart();
|
||||
net::query(ws.get_executor(), net::execution::context).run();
|
||||
net::query(ws.get_executor(), net::execution::context).restart();
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
}
|
||||
@@ -1296,8 +1295,8 @@ public:
|
||||
{
|
||||
error_code ec;
|
||||
ws.async_accept_ex(d, handler(ec));
|
||||
ws.get_executor().context().run();
|
||||
ws.get_executor().context().restart();
|
||||
net::query(ws.get_executor(), net::execution::context).run();
|
||||
net::query(ws.get_executor(), net::execution::context).restart();
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
}
|
||||
@@ -1314,8 +1313,8 @@ public:
|
||||
{
|
||||
error_code ec;
|
||||
ws.async_accept_ex(buffers, d, handler(ec));
|
||||
ws.get_executor().context().run();
|
||||
ws.get_executor().context().restart();
|
||||
net::query(ws.get_executor(), net::execution::context).run();
|
||||
net::query(ws.get_executor(), net::execution::context).restart();
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
}
|
||||
@@ -1331,8 +1330,8 @@ public:
|
||||
{
|
||||
error_code ec;
|
||||
ws.async_accept_ex(req, d, handler(ec));
|
||||
ws.get_executor().context().run();
|
||||
ws.get_executor().context().restart();
|
||||
net::query(ws.get_executor(), net::execution::context).run();
|
||||
net::query(ws.get_executor(), net::execution::context).restart();
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
}
|
||||
@@ -1350,8 +1349,8 @@ public:
|
||||
error_code ec;
|
||||
ws.async_accept_ex(
|
||||
req, buffers, d, handler(ec));
|
||||
ws.get_executor().context().run();
|
||||
ws.get_executor().context().restart();
|
||||
net::query(ws.get_executor(), net::execution::context).run();
|
||||
net::query(ws.get_executor(), net::execution::context).restart();
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
}
|
||||
@@ -1367,8 +1366,8 @@ public:
|
||||
error_code ec;
|
||||
ws.async_handshake(
|
||||
uri, path, handler(ec));
|
||||
ws.get_executor().context().run();
|
||||
ws.get_executor().context().restart();
|
||||
net::query(ws.get_executor(), net::execution::context).run();
|
||||
net::query(ws.get_executor(), net::execution::context).restart();
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
}
|
||||
@@ -1384,8 +1383,8 @@ public:
|
||||
error_code ec;
|
||||
ws.async_handshake(
|
||||
res, uri, path, handler(ec));
|
||||
ws.get_executor().context().run();
|
||||
ws.get_executor().context().restart();
|
||||
net::query(ws.get_executor(), net::execution::context).run();
|
||||
net::query(ws.get_executor(), net::execution::context).restart();
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
}
|
||||
@@ -1403,8 +1402,8 @@ public:
|
||||
error_code ec;
|
||||
ws.async_handshake_ex(
|
||||
uri, path, d, handler(ec));
|
||||
ws.get_executor().context().run();
|
||||
ws.get_executor().context().restart();
|
||||
net::query(ws.get_executor(), net::execution::context).run();
|
||||
net::query(ws.get_executor(), net::execution::context).restart();
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
}
|
||||
@@ -1423,8 +1422,8 @@ public:
|
||||
error_code ec;
|
||||
ws.async_handshake_ex(
|
||||
res, uri, path, d, handler(ec));
|
||||
ws.get_executor().context().run();
|
||||
ws.get_executor().context().restart();
|
||||
net::query(ws.get_executor(), net::execution::context).run();
|
||||
net::query(ws.get_executor(), net::execution::context).restart();
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
}
|
||||
@@ -1437,8 +1436,8 @@ public:
|
||||
{
|
||||
error_code ec;
|
||||
ws.async_ping(payload, handler(ec));
|
||||
ws.get_executor().context().run();
|
||||
ws.get_executor().context().restart();
|
||||
net::query(ws.get_executor(), net::execution::context).run();
|
||||
net::query(ws.get_executor(), net::execution::context).restart();
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
}
|
||||
@@ -1451,8 +1450,8 @@ public:
|
||||
{
|
||||
error_code ec;
|
||||
ws.async_pong(payload, handler(ec));
|
||||
ws.get_executor().context().run();
|
||||
ws.get_executor().context().restart();
|
||||
net::query(ws.get_executor(), net::execution::context).run();
|
||||
net::query(ws.get_executor(), net::execution::context).restart();
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
}
|
||||
@@ -1465,8 +1464,8 @@ public:
|
||||
{
|
||||
error_code ec;
|
||||
ws.async_close(cr, handler(ec));
|
||||
ws.get_executor().context().run();
|
||||
ws.get_executor().context().restart();
|
||||
net::query(ws.get_executor(), net::execution::context).run();
|
||||
net::query(ws.get_executor(), net::execution::context).restart();
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
}
|
||||
@@ -1482,8 +1481,8 @@ public:
|
||||
error_code ec;
|
||||
std::size_t n;
|
||||
ws.async_read(buffer, handler(ec, n));
|
||||
ws.get_executor().context().run();
|
||||
ws.get_executor().context().restart();
|
||||
net::query(ws.get_executor(), net::execution::context).run();
|
||||
net::query(ws.get_executor(), net::execution::context).restart();
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
return n;
|
||||
@@ -1501,8 +1500,8 @@ public:
|
||||
error_code ec;
|
||||
std::size_t n;
|
||||
ws.async_read_some(buffer, limit, handler(ec, n));
|
||||
ws.get_executor().context().run();
|
||||
ws.get_executor().context().restart();
|
||||
net::query(ws.get_executor(), net::execution::context).run();
|
||||
net::query(ws.get_executor(), net::execution::context).restart();
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
return n;
|
||||
@@ -1519,8 +1518,8 @@ public:
|
||||
error_code ec;
|
||||
std::size_t n;
|
||||
ws.async_read_some(buffers, handler(ec, n));
|
||||
ws.get_executor().context().run();
|
||||
ws.get_executor().context().restart();
|
||||
net::query(ws.get_executor(), net::execution::context).run();
|
||||
net::query(ws.get_executor(), net::execution::context).restart();
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
return n;
|
||||
@@ -1537,8 +1536,8 @@ public:
|
||||
error_code ec;
|
||||
std::size_t n;
|
||||
ws.async_write(buffers, handler(ec, n));
|
||||
ws.get_executor().context().run();
|
||||
ws.get_executor().context().restart();
|
||||
net::query(ws.get_executor(), net::execution::context).run();
|
||||
net::query(ws.get_executor(), net::execution::context).restart();
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
return n;
|
||||
@@ -1556,8 +1555,8 @@ public:
|
||||
error_code ec;
|
||||
std::size_t n;
|
||||
ws.async_write_some(fin, buffers, handler(ec, n));
|
||||
ws.get_executor().context().run();
|
||||
ws.get_executor().context().restart();
|
||||
net::query(ws.get_executor(), net::execution::context).run();
|
||||
net::query(ws.get_executor(), net::execution::context).restart();
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
return n;
|
||||
@@ -1575,8 +1574,8 @@ public:
|
||||
std::size_t n;
|
||||
net::async_write(ws.next_layer(),
|
||||
buffers, handler(ec, n));
|
||||
ws.get_executor().context().run();
|
||||
ws.get_executor().context().restart();
|
||||
net::query(ws.get_executor(), net::execution::context).run();
|
||||
net::query(ws.get_executor(), net::execution::context).restart();
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
return n;
|
||||
|
||||
@@ -38,7 +38,9 @@ namespace ssl = boost::asio::ssl;
|
||||
using tcp = net::ip::tcp;
|
||||
|
||||
net::io_context ioc;
|
||||
auto work = net::make_work_guard(ioc);
|
||||
auto work = net::any_io_executor(
|
||||
net::prefer(ioc.get_executor(),
|
||||
net::execution::outstanding_work.tracked));
|
||||
std::thread t{[&](){ ioc.run(); }};
|
||||
|
||||
error_code ec;
|
||||
|
||||
@@ -37,7 +37,10 @@ net::const_buffer get_next_chunk_body()
|
||||
void fxx() {
|
||||
|
||||
net::io_context ioc;
|
||||
auto work = net::make_work_guard(ioc);
|
||||
auto work = net::any_io_executor(
|
||||
net::prefer(
|
||||
ioc.get_executor(),
|
||||
net::execution::outstanding_work.tracked));
|
||||
std::thread t{[&](){ ioc.run(); }};
|
||||
net::ip::tcp::socket sock{ioc};
|
||||
|
||||
|
||||
@@ -17,7 +17,10 @@ using tcp = net::ip::tcp;
|
||||
|
||||
error_code ec;
|
||||
net::io_context ioc;
|
||||
auto work = net::make_work_guard(ioc);
|
||||
auto work = net::any_io_executor(
|
||||
net::prefer(
|
||||
ioc.get_executor(),
|
||||
net::execution::outstanding_work.tracked));
|
||||
std::thread t{[&](){ ioc.run(); }};
|
||||
|
||||
tcp::socket sock(ioc);
|
||||
|
||||
@@ -35,8 +35,8 @@ protected:
|
||||
net::io_context ioc_;
|
||||
|
||||
private:
|
||||
net::executor_work_guard<
|
||||
net::io_context::executor_type> work_;
|
||||
detail::select_work_guard_t<net::io_context::executor_type>
|
||||
work_;
|
||||
std::vector<std::thread> threads_;
|
||||
std::mutex m_;
|
||||
std::condition_variable cv_;
|
||||
@@ -49,7 +49,7 @@ public:
|
||||
|
||||
explicit
|
||||
enable_yield_to(std::size_t concurrency = 1)
|
||||
: work_(ioc_.get_executor())
|
||||
: work_(detail::make_work_guard(ioc_.get_executor()))
|
||||
{
|
||||
threads_.reserve(concurrency);
|
||||
while(concurrency--)
|
||||
@@ -136,4 +136,5 @@ spawn(F0&& f, FN&&... fn)
|
||||
} // beast
|
||||
} // boost
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user