Skip to content

Commit

Permalink
fix: Add support for ASIO_USE_TS_EXECUTOR_AS_DEFAULT and update vcpkg…
Browse files Browse the repository at this point in the history
… (Boost.Asio 1.86)
  • Loading branch information
Tradias committed Oct 19, 2024
1 parent a1f69e7 commit fc50c54
Show file tree
Hide file tree
Showing 22 changed files with 136 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ on:
- 'CMakePresets.json'

env:
VCPKG_VERSION: '821100d967e1737d96414a308e3f7cbe0d1abf18'
VCPKG_VERSION: 'd567b667adba0e72c5c3931ddbe745b66aa34b73'
CMAKE_ARGS: '-DCMAKE_BUILD_TYPE=Release -DVCPKG_MANIFEST_INSTALL=off -DASIO_GRPC_ENABLE_PKGCONFIG_FALLBACK=off -DCMAKE_COMPILE_WARNING_AS_ERROR=on -DCMAKE_FIND_PACKAGE_PREFER_CONFIG=on -DTHREADS_PREFER_PTHREAD_FLAG=on -DCMAKE_UNITY_BUILD=on'
CTEST_ARGS: '-T test --output-on-failure --timeout 180 --no-tests=error --parallel 25'

Expand Down
3 changes: 0 additions & 3 deletions example/snippets/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@
#include <grpcpp/create_channel.h>

#include <cassert>
#include <optional>

namespace asio = boost::asio;

static_assert(asio::is_executor<agrpc::GrpcExecutor>::value);

asio::awaitable<void> agrpc_notify_on_state_change(agrpc::GrpcContext& grpc_context, const std::string& host)
{
/* [notify_on_state_change] */
Expand Down
6 changes: 6 additions & 0 deletions src/agrpc/detail/allocate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ namespace detail
template <class T, class Allocator>
using RebindAllocatorTraits = typename std::allocator_traits<Allocator>::template rebind_traits<T>;

template <class T>
inline constexpr bool IS_STD_ALLOCATOR = false;

template <class T>
inline constexpr bool IS_STD_ALLOCATOR<std::allocator<T>> = true;

template <class Traits>
class AllocationGuard
{
Expand Down
6 changes: 0 additions & 6 deletions src/agrpc/detail/allocate_operation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ AGRPC_NAMESPACE_BEGIN()

namespace detail
{
template <class T>
inline constexpr bool IS_STD_ALLOCATOR = false;

template <class T>
inline constexpr bool IS_STD_ALLOCATOR<std::allocator<T>> = true;

template <template <class> class OperationTemplate, class Handler, class... Args>
auto allocate_operation(Handler&& handler, Args&&... args)
{
Expand Down
12 changes: 12 additions & 0 deletions src/agrpc/detail/asio_forward.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <asio/associated_allocator.hpp>
#include <asio/associated_executor.hpp>
#include <asio/async_result.hpp>
#include <asio/dispatch.hpp>
#include <asio/error.hpp>
#include <asio/execution/allocator.hpp>
#include <asio/execution/blocking.hpp>
Expand All @@ -29,9 +30,14 @@
#include <asio/execution/outstanding_work.hpp>
#include <asio/execution/relationship.hpp>
#include <asio/execution_context.hpp>
#include <asio/post.hpp>
#include <asio/query.hpp>
#include <asio/system_executor.hpp>

#ifdef ASIO_USE_TS_EXECUTOR_AS_DEFAULT
#include <boost/asio/executor_work_guard.hpp>
#endif

#ifdef AGRPC_ASIO_HAS_CANCELLATION_SLOT
#include <asio/associated_cancellation_slot.hpp>
#include <asio/bind_cancellation_slot.hpp>
Expand All @@ -44,6 +50,7 @@
#include <boost/asio/associated_allocator.hpp>
#include <boost/asio/associated_executor.hpp>
#include <boost/asio/async_result.hpp>
#include <boost/asio/dispatch.hpp>
#include <boost/asio/error.hpp>
#include <boost/asio/execution/allocator.hpp>
#include <boost/asio/execution/blocking.hpp>
Expand All @@ -52,9 +59,14 @@
#include <boost/asio/execution/outstanding_work.hpp>
#include <boost/asio/execution/relationship.hpp>
#include <boost/asio/execution_context.hpp>
#include <boost/asio/post.hpp>
#include <boost/asio/query.hpp>
#include <boost/asio/system_executor.hpp>

#ifdef BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT
#include <boost/asio/executor_work_guard.hpp>
#endif

#ifdef AGRPC_ASIO_HAS_CANCELLATION_SLOT
#include <boost/asio/associated_cancellation_slot.hpp>
#include <boost/asio/bind_cancellation_slot.hpp>
Expand Down
51 changes: 27 additions & 24 deletions src/agrpc/detail/asio_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
#define AGRPC_DETAIL_ASIO_UTILS_HPP

#include <agrpc/detail/asio_forward.hpp>
#include <agrpc/detail/association.hpp>
#include <agrpc/detail/execution.hpp>
#include <agrpc/detail/utility.hpp>

#include <agrpc/detail/asio_macros.hpp>
#include <agrpc/detail/config.hpp>
Expand All @@ -33,18 +35,25 @@ inline constexpr bool IS_EXECUTOR_PROVIDER = false;
template <class T>
inline constexpr bool IS_EXECUTOR_PROVIDER<T, decltype((void)std::declval<T>().get_executor())> = true;

template <class Executor, class Function, class Allocator>
void post_with_allocator(Executor&& executor, Function&& function, const Allocator& allocator)
template <class Handler, class Function>
struct AllocatorAssociator
{
asio::prefer(asio::require(static_cast<Executor&&>(executor), asio::execution::blocking_t::never),
asio::execution::relationship_t::fork, asio::execution::allocator(allocator))
.execute(static_cast<Function&&>(function));
}
using allocator_type = asio::associated_allocator_t<Handler>;

void operator()() { static_cast<Function&&>(function_)(static_cast<Handler&&>(handler_)); }

allocator_type get_allocator() const noexcept { return asio::get_associated_allocator(handler_); }

Handler handler_;
Function function_;
};

template <class Handler, class Function>
AllocatorAssociator(const Handler&, const Function&) -> AllocatorAssociator<Handler, Function>;

template <class CompletionHandler, class Function, class IOExecutor>
void complete_immediately(CompletionHandler&& completion_handler, Function&& function, const IOExecutor& io_executor)
{
const auto allocator = asio::get_associated_allocator(completion_handler);
#ifdef AGRPC_ASIO_HAS_IMMEDIATE_EXECUTOR
auto executor = asio::get_associated_immediate_executor(
completion_handler,
Expand All @@ -62,28 +71,22 @@ void complete_immediately(CompletionHandler&& completion_handler, Function&& fun
return (io_executor);
}
}());
asio::prefer(std::move(executor), asio::execution::allocator(allocator))
.execute(
[ch = static_cast<CompletionHandler&&>(completion_handler), f = static_cast<Function&&>(function)]() mutable
{
static_cast<Function&&>(f)(static_cast<CompletionHandler&&>(ch));
});
asio::dispatch(std::move(executor),
AllocatorAssociator{static_cast<CompletionHandler&&>(completion_handler),
[f = static_cast<Function&&>(function)](auto&& ch) mutable
{
static_cast<Function&&>(f)(static_cast<decltype(ch)&&>(ch));
}});
#else
auto executor = asio::get_associated_executor(completion_handler, io_executor);
detail::post_with_allocator(
std::move(executor),
[ch = static_cast<CompletionHandler&&>(completion_handler), f = static_cast<Function&&>(function)]() mutable
{
static_cast<Function&&>(f)(static_cast<CompletionHandler&&>(ch));
},
allocator);
asio::post(std::move(executor), AllocatorAssociator{static_cast<CompletionHandler&&>(completion_handler),
[f = static_cast<Function&&>(function)](auto&& ch) mutable
{
static_cast<Function&&>(f)(static_cast<decltype(ch)&&>(ch));
}});
#endif
}

struct UncancellableSlot
{
};

template <class Object>
auto get_cancellation_slot([[maybe_unused]] const Object& object) noexcept
{
Expand Down
6 changes: 5 additions & 1 deletion src/agrpc/detail/association_asio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@
#ifndef AGRPC_DETAIL_ASSOCIATION_ASIO_HPP
#define AGRPC_DETAIL_ASSOCIATION_ASIO_HPP

#include <agrpc/detail/asio_utils.hpp>
#include <agrpc/detail/execution.hpp>
#include <agrpc/detail/forward.hpp>

#include <agrpc/detail/config.hpp>

AGRPC_NAMESPACE_BEGIN()

namespace detail
{
struct UncancellableSlot
{
};

template <class CancellationSlot>
inline constexpr bool IS_CANCELLATION_SLOT = !exec::stoppable_token<CancellationSlot>;

Expand Down
1 change: 1 addition & 0 deletions src/agrpc/detail/manual_reset_event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define AGRPC_DETAIL_MANUAL_RESET_EVENT_HPP

#include <agrpc/detail/allocate.hpp>
#include <agrpc/detail/asio_utils.hpp>
#include <agrpc/detail/association.hpp>
#include <agrpc/detail/execution.hpp>
#include <agrpc/detail/forward.hpp>
Expand Down
23 changes: 16 additions & 7 deletions src/agrpc/detail/work_tracking_completion_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define AGRPC_DETAIL_WORK_TRACKING_COMPLETION_HANDLER_HPP

#include <agrpc/detail/asio_forward.hpp>
#include <agrpc/detail/asio_utils.hpp>
#include <agrpc/detail/association.hpp>
#include <agrpc/detail/tuple.hpp>
#include <agrpc/detail/utility.hpp>
Expand Down Expand Up @@ -44,6 +45,13 @@ inline constexpr bool
template <class Executor, bool = detail::IS_INLINE_EXECUTOR<Executor>>
class WorkTracker
{
#if defined(ASIO_USE_TS_EXECUTOR_AS_DEFAULT) || defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
public:
explicit WorkTracker(Executor&& executor) : work_(std::move(executor)) {}

private:
asio::executor_work_guard<Executor> work_;
#else
public:
explicit WorkTracker(Executor&& executor)
: work_(asio::prefer(std::move(executor), asio::execution::outstanding_work_t::tracked))
Expand All @@ -52,6 +60,7 @@ class WorkTracker

private:
typename asio::prefer_result<Executor, asio::execution::outstanding_work_t::tracked_t>::type work_;
#endif
};

template <class Executor>
Expand All @@ -64,13 +73,13 @@ class WorkTracker<Executor, true>
template <class Handler, class... Args>
void dispatch_with_args(Handler&& handler, Args&&... args)
{
asio::prefer(asio::get_associated_executor(handler), asio::execution::blocking_t::possibly,
asio::execution::allocator(asio::get_associated_allocator(handler)))
.execute(
[h = static_cast<Handler&&>(handler), arg_tuple = detail::Tuple{static_cast<Args&&>(args)...}]() mutable
{
detail::apply(std::move(h), std::move(arg_tuple));
});
auto executor = asio::get_associated_executor(handler);
asio::dispatch(std::move(executor),
AllocatorAssociator{static_cast<Handler&&>(handler),
[arg_tuple = detail::Tuple{static_cast<Args&&>(args)...}](auto&& ch) mutable
{
detail::apply(static_cast<decltype(ch)&&>(ch), std::move(arg_tuple));
}});
}

template <class AllocationGuard, class... Args>
Expand Down
2 changes: 1 addition & 1 deletion test/cmake/subdirectory/src/vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
"gtest",
"stdexec"
],
"builtin-baseline": "821100d967e1737d96414a308e3f7cbe0d1abf18"
"builtin-baseline": "d567b667adba0e72c5c3931ddbe745b66aa34b73"
}
2 changes: 1 addition & 1 deletion test/cmake/superbuild/src/vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
"gtest",
"stdexec"
],
"builtin-baseline": "821100d967e1737d96414a308e3f7cbe0d1abf18"
"builtin-baseline": "d567b667adba0e72c5c3931ddbe745b66aa34b73"
}
2 changes: 2 additions & 0 deletions test/src/test_alarm_17.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ TEST_CASE_TEMPLATE("cancel agrpc::Alarm with cancellation_type::none", T, std::t
CHECK(ok);
}

#ifdef AGRPC_TEST_ASIO_PARALLEL_GROUP
TEST_CASE_FIXTURE(test::GrpcContextTest, "cancel agrpc::Alarm with parallel_group")
{
std::array<std::size_t, 2> completion_order;
Expand Down Expand Up @@ -234,6 +235,7 @@ TEST_CASE_FIXTURE(test::GrpcContextTest, "cancel agrpc::Alarm with parallel_grou
CHECK_FALSE(ok);
}
#endif
#endif

TEST_CASE_FIXTURE(test::GrpcContextTest, "asio::execution connect and start Alarm")
{
Expand Down
2 changes: 2 additions & 0 deletions test/src/test_alarm_20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ TEST_CASE_FIXTURE(test::GrpcContextTest,
CHECK(ok);
CHECK_EQ(expected_thread_id, actual_thread_id);
}
#endif

#ifdef AGRPC_TEST_ASIO_PARALLEL_GROUP
TEST_CASE_FIXTURE(test::GrpcContextTest, "cancel grpc::Alarm with awaitable operators")
{
std::size_t result_index{};
Expand Down
2 changes: 1 addition & 1 deletion test/src/test_client_rpc_17.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ TEST_CASE("ClientRPC derived class cannot access private base member")
CHECK_FALSE(decltype(std::declval<Derived>().grpc_context(0))::value);
}

#ifdef AGRPC_TEST_ASIO_HAS_CANCELLATION_SLOT
#ifdef AGRPC_TEST_ASIO_PARALLEL_GROUP
// gRPC requests seem to be uncancellable on platforms other than Windows
#ifdef _WIN32
TEST_CASE_TEMPLATE("Unary RPC::request can be cancelled", RPC, test::UnaryClientRPC, test::GenericUnaryClientRPC)
Expand Down
Loading

0 comments on commit fc50c54

Please sign in to comment.