Skip to content

Commit

Permalink
style: Turn run lambda inside process_work into free function
Browse files Browse the repository at this point in the history
  • Loading branch information
Tradias committed Dec 14, 2024
1 parent 4f27b50 commit f3963d9
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions src/agrpc/detail/grpc_context_implementation_definition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,40 +241,44 @@ inline DoOneResult GrpcContextImplementation::do_one(detail::GrpcContextThreadCo
return DoOneResult::from(handled_event, processed_local_work);
}

template <class LoopCondition>
inline bool GrpcContextImplementation::process_work(agrpc::GrpcContext& grpc_context, LoopCondition loop_condition,
::gpr_timespec deadline)
template <bool IsMultithreaded, class LoopCondition>
bool run_loop(detail::GrpcContextThreadContextImpl<IsMultithreaded>& thread_context, LoopCondition loop_condition,
::gpr_timespec deadline)
{
const auto run = [&loop_condition, deadline](auto& thread_context)
bool processed{};
DoOneResult result;
while (loop_condition())
{
bool processed{};
DoOneResult result;
while (loop_condition())
if constexpr (LoopCondition::COMPLETION_QUEUE_ONLY)
{
result = {GrpcContextImplementation::do_one_completion_queue_event(thread_context, deadline)};
}
else
{
result = GrpcContextImplementation::do_one(thread_context, deadline);
}
if (!result)
{
if constexpr (LoopCondition::COMPLETION_QUEUE_ONLY)
{
result = {GrpcContextImplementation::do_one_completion_queue_event(thread_context, deadline)};
}
else
{
result = GrpcContextImplementation::do_one(thread_context, deadline);
}
if (!result)
{
break;
}
processed = processed || loop_condition.has_processed(result);
break;
}
return processed;
};
processed = processed || loop_condition.has_processed(result);
}
return processed;
}

template <class LoopCondition>
inline bool GrpcContextImplementation::process_work(agrpc::GrpcContext& grpc_context, LoopCondition loop_condition,
::gpr_timespec deadline)
{
if (GrpcContextImplementation::running_in_this_thread(grpc_context))
{
auto& context = *detail::thread_local_grpc_context;
if (grpc_context.multithreaded_)
{
return run(static_cast<GrpcContextThreadContextImpl<true>&>(context));
return detail::run_loop(static_cast<GrpcContextThreadContextImpl<true>&>(context), loop_condition,
deadline);
}
return run(static_cast<GrpcContextThreadContextImpl<false>&>(context));
return detail::run_loop(static_cast<GrpcContextThreadContextImpl<false>&>(context), loop_condition, deadline);
}
if (grpc_context.outstanding_work_.load(std::memory_order_relaxed) == 0)
{
Expand All @@ -285,10 +289,10 @@ inline bool GrpcContextImplementation::process_work(agrpc::GrpcContext& grpc_con
if (grpc_context.multithreaded_)
{
detail::GrpcContextThreadContextImpl<true> thread_context{grpc_context};
return run(thread_context);
return detail::run_loop(thread_context, loop_condition, deadline);
}
detail::GrpcContextThreadContextImpl<false> thread_context{grpc_context};
return run(thread_context);
return detail::run_loop(thread_context, loop_condition, deadline);
}

inline void GrpcContextImplementation::drain_completion_queue(agrpc::GrpcContext& grpc_context) noexcept
Expand Down

0 comments on commit f3963d9

Please sign in to comment.