From 9e23624fba85f1fdb41f9c5d01c32bee719f573b Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Thu, 20 Mar 2025 17:50:09 -0700 Subject: [PATCH] Run scheduled executor callback on executor thread PiperOrigin-RevId: 738998896 --- .../flags/nearby_platform_feature_flags.h | 4 ++++ .../windows/scheduled_executor.cc | 18 +++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/internal/platform/flags/nearby_platform_feature_flags.h b/internal/platform/flags/nearby_platform_feature_flags.h index 72cd9172..a145add5 100644 --- a/internal/platform/flags/nearby_platform_feature_flags.h +++ b/internal/platform/flags/nearby_platform_feature_flags.h @@ -89,6 +89,10 @@ constexpr auto kEnableBlockingSocket = constexpr auto kSocketSendBufferSize = flags::Flag(kConfigPackage, "45673785", 524288); +// Run scheduled executor callback on executor thread. +constexpr auto kRunScheduledExecutorCallbackOnExecutorThread = + flags::Flag(kConfigPackage, "45686494", false); + } // namespace nearby_platform_feature } // namespace config_package_nearby } // namespace platform diff --git a/internal/platform/implementation/windows/scheduled_executor.cc b/internal/platform/implementation/windows/scheduled_executor.cc index 427191b8..d1bcbf12 100644 --- a/internal/platform/implementation/windows/scheduled_executor.cc +++ b/internal/platform/implementation/windows/scheduled_executor.cc @@ -20,6 +20,8 @@ #include #include "absl/time/time.h" +#include "internal/flags/nearby_flags.h" +#include "internal/platform/flags/nearby_platform_feature_flags.h" #include "internal/platform/implementation/cancelable.h" #include "internal/platform/logging.h" #include "internal/platform/runnable.h" @@ -38,12 +40,22 @@ ScheduledExecutor::ScheduledExecutor() std::shared_ptr ScheduledExecutor::Schedule( Runnable&& runnable, absl::Duration duration) { if (shut_down_) { - LOG(ERROR) << __func__ - << ": Attempt to Schedule on a shut down executor."; + LOG(ERROR) << __func__ << ": Attempt to Schedule on a shut down executor."; return nullptr; } - return task_scheduler_.Schedule(std::move(runnable), duration); + + if (NearbyFlags::GetInstance().GetBoolFlag( + platform::config_package_nearby::nearby_platform_feature:: + kRunScheduledExecutorCallbackOnExecutorThread)) { + return task_scheduler_.Schedule( + [this, runnable = std::move(runnable)]() mutable { + Execute(std::move(runnable)); + }, + duration); + } else { + return task_scheduler_.Schedule(std::move(runnable), duration); + } } void ScheduledExecutor::Execute(Runnable&& runnable) {