Skip to content

Commit

Permalink
style: Implement create_and_submit_no_arg_operation in terms of Sende…
Browse files Browse the repository at this point in the history
…rImplementationOperation
  • Loading branch information
Tradias committed Oct 6, 2023
1 parent c77e03e commit 05ce0ee
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 34 deletions.
39 changes: 8 additions & 31 deletions src/agrpc/detail/allocate_operation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ auto allocate_local_operation(agrpc::GrpcContext& grpc_context, Handler&& handle
}

template <template <class> class OperationTemplate, class Handler, class... Args>
auto allocate_operation(agrpc::GrpcContext& grpc_context, Handler&& handler, Args&&... args)
auto allocate_operation(bool is_running_in_this_thread, agrpc::GrpcContext& grpc_context, Handler&& handler,
Args&&... args)
{
if (detail::GrpcContextImplementation::running_in_this_thread(grpc_context))
if (is_running_in_this_thread)
{
return detail::allocate_local_operation<OperationTemplate>(grpc_context, static_cast<Handler&&>(handler),
static_cast<Args&&>(args)...);
Expand All @@ -77,36 +78,12 @@ auto allocate_operation(agrpc::GrpcContext& grpc_context, Handler&& handler, Arg
static_cast<Args&&>(args)...);
}

template <bool IsBlockingNever, class Handler>
void create_and_submit_no_arg_operation(agrpc::GrpcContext& grpc_context, Handler&& handler)
template <template <class> class OperationTemplate, class Handler, class... Args>
auto allocate_operation(agrpc::GrpcContext& grpc_context, Handler&& handler, Args&&... args)
{
if AGRPC_UNLIKELY (detail::GrpcContextImplementation::is_shutdown(grpc_context))
{
return;
}
const auto is_running_in_this_thread = detail::GrpcContextImplementation::running_in_this_thread(grpc_context);
if constexpr (!IsBlockingNever)
{
if (is_running_in_this_thread)
{
auto op{static_cast<Handler&&>(handler)};
std::move(op)();
return;
}
}
detail::StartWorkAndGuard guard{grpc_context};
if (is_running_in_this_thread)
{
auto operation =
detail::allocate_local_operation<detail::NoArgOperation>(grpc_context, static_cast<Handler&&>(handler));
detail::GrpcContextImplementation::add_local_operation(grpc_context, operation);
}
else
{
auto operation = detail::allocate_custom_operation<detail::NoArgOperation>(static_cast<Handler&&>(handler));
detail::GrpcContextImplementation::add_remote_operation(grpc_context, operation);
}
guard.release();
return detail::allocate_operation<OperationTemplate>(
detail::GrpcContextImplementation::running_in_this_thread(grpc_context), grpc_context,
static_cast<Handler&&>(handler), static_cast<Args&&>(args)...);
}
}

Expand Down
71 changes: 71 additions & 0 deletions src/agrpc/detail/create_and_submit_no_arg_operation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2023 Dennis Hezel
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef AGRPC_DETAIL_CREATE_AND_SUBMIT_NO_ARG_OPERATION_HPP
#define AGRPC_DETAIL_CREATE_AND_SUBMIT_NO_ARG_OPERATION_HPP

#include <agrpc/detail/config.hpp>
#include <agrpc/detail/grpc_context_implementation.hpp>
#include <agrpc/detail/schedule_sender.hpp>
#include <agrpc/detail/sender_implementation_operation.hpp>
#include <agrpc/detail/utility.hpp>
#include <agrpc/grpc_context.hpp>

AGRPC_NAMESPACE_BEGIN()

namespace detail
{
struct NoArgOperationInitiation
{
static void initiate(const agrpc::GrpcContext&, const detail::QueueableOperationBase*) noexcept {}
};

template <bool IsBlockingNever, class Handler>
void create_and_submit_no_arg_operation(agrpc::GrpcContext& grpc_context, Handler&& handler)
{
if AGRPC_UNLIKELY (detail::GrpcContextImplementation::is_shutdown(grpc_context))
{
return;
}
const auto is_running_in_this_thread = detail::GrpcContextImplementation::running_in_this_thread(grpc_context);
if constexpr (!IsBlockingNever)
{
if (is_running_in_this_thread)
{
auto op{static_cast<Handler&&>(handler)};
std::move(op)();
return;
}
}
using Template = detail::SenderImplementationOperationTemplate<detail::ScheduleSenderImplementation>;
if (is_running_in_this_thread)
{
auto operation = detail::allocate_local_operation<Template::Type>(
grpc_context, static_cast<Handler&&>(handler), grpc_context, detail::NoArgOperationInitiation{},
detail::ScheduleSenderImplementation{});
detail::GrpcContextImplementation::add_local_operation(grpc_context, operation);
}
else
{
auto operation = detail::allocate_custom_operation<Template::Type>(
static_cast<Handler&&>(handler), grpc_context, detail::NoArgOperationInitiation{},
detail::ScheduleSenderImplementation{});
detail::GrpcContextImplementation::add_remote_operation(grpc_context, operation);
}
}
}

AGRPC_NAMESPACE_END

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

#include <agrpc/detail/config.hpp>
#include <agrpc/detail/create_and_submit_no_arg_operation.hpp>
#include <agrpc/detail/intrusive_list.hpp>
#include <agrpc/detail/intrusive_list_hook.hpp>
#include <agrpc/detail/sender_implementation.hpp>
Expand Down
6 changes: 3 additions & 3 deletions src/agrpc/detail/sender_implementation_operation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ struct SenderImplementationOperation : public detail::BaseForSenderImplementatio
return &do_complete<detail::AllocationType::CUSTOM>;
}

template <class Initation>
SenderImplementationOperation(detail::AllocationType allocation_type, CompletionHandler&& completion_handler,
template <class Ch, class Initation>
SenderImplementationOperation(detail::AllocationType allocation_type, Ch&& completion_handler,
agrpc::GrpcContext& grpc_context, const Initation& initiation,
Implementation&& implementation)
: Base(get_on_complete(allocation_type)),
impl_(static_cast<CompletionHandler&&>(completion_handler), static_cast<Implementation&&>(implementation))
impl_(static_cast<Ch&&>(completion_handler), static_cast<Implementation&&>(implementation))
{
grpc_context.work_started();
emplace_stop_callback(initiation);
Expand Down
1 change: 1 addition & 0 deletions src/agrpc/grpc_executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <agrpc/detail/allocate_operation.hpp>
#include <agrpc/detail/asio_forward.hpp>
#include <agrpc/detail/config.hpp>
#include <agrpc/detail/create_and_submit_no_arg_operation.hpp>
#include <agrpc/detail/forward.hpp>
#include <agrpc/detail/get_completion_queue.hpp>
#include <agrpc/detail/grpc_executor_base.hpp>
Expand Down

0 comments on commit 05ce0ee

Please sign in to comment.