From 0cceb4079ff099dd1a8d4bf91db49061a945fcf1 Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Tue, 16 Jun 2026 19:28:32 -0700 Subject: [PATCH] Use a shared executor for g3 timers. PiperOrigin-RevId: 933431037 --- internal/platform/implementation/g3/BUILD | 4 +++- .../platform/implementation/g3/platform.cc | 4 +++- internal/platform/implementation/g3/timer.h | 21 +++++++++++++------ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/internal/platform/implementation/g3/BUILD b/internal/platform/implementation/g3/BUILD index 6fb81f20..d49fc83a 100644 --- a/internal/platform/implementation/g3/BUILD +++ b/internal/platform/implementation/g3/BUILD @@ -59,10 +59,11 @@ cc_library( "//internal/platform/implementation:types", "//internal/platform/implementation/shared:count_down_latch", "//internal/platform/implementation/shared:posix_mutex", - "//internal/test", "@com_google_absl//absl/base:core_headers", + "@com_google_absl//absl/base:nullability", "@com_google_absl//absl/container:btree", "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/functional:any_invocable", "@com_google_absl//absl/strings", "@com_google_absl//absl/strings:str_format", "@com_google_absl//absl/synchronization", @@ -217,6 +218,7 @@ cc_library( "//internal/platform/implementation/shared:file", "//third_party/gloop/thread", "@com_google_absl//absl/base:core_headers", + "@com_google_absl//absl/base:no_destructor", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", diff --git a/internal/platform/implementation/g3/platform.cc b/internal/platform/implementation/g3/platform.cc index dfa62fae..3de2e8f7 100644 --- a/internal/platform/implementation/g3/platform.cc +++ b/internal/platform/implementation/g3/platform.cc @@ -20,6 +20,7 @@ #include #include "absl/base/attributes.h" +#include "absl/base/no_destructor.h" #include "absl/status/status.h" #include "absl/status/statusor.h" #include "absl/strings/str_cat.h" @@ -239,7 +240,8 @@ ImplementationPlatform::CreateConditionVariable(Mutex* mutex) { } std::unique_ptr ImplementationPlatform::CreateTimer() { - return std::make_unique(); + static absl::NoDestructor timer_executor; + return std::make_unique(timer_executor.get()); } std::unique_ptr diff --git a/internal/platform/implementation/g3/timer.h b/internal/platform/implementation/g3/timer.h index 3faa779f..61ef431a 100644 --- a/internal/platform/implementation/g3/timer.h +++ b/internal/platform/implementation/g3/timer.h @@ -19,10 +19,13 @@ #include #include +#include "absl/base/nullability.h" #include "absl/base/thread_annotations.h" +#include "absl/functional/any_invocable.h" #include "absl/synchronization/mutex.h" #include "absl/time/time.h" -#include "internal/platform/implementation/g3/scheduled_executor.h" +#include "internal/platform/implementation/cancelable.h" +#include "internal/platform/implementation/scheduled_executor.h" #include "internal/platform/implementation/timer.h" namespace nearby { @@ -30,8 +33,11 @@ namespace g3 { class Timer : public api::Timer { public: - Timer() = default; - ~Timer() override = default; + explicit Timer(api::ScheduledExecutor* absl_nonnull executor) + : executor_(executor) {}; + ~Timer() override { + Stop(); + }; bool Create(int delay, int interval, absl::AnyInvocable callback) override { @@ -52,13 +58,16 @@ class Timer : public api::Timer { task_.reset(); return result; } - return false; + return true; } private: bool Schedule(absl::Duration delay) { absl::MutexLock lock(mutex_); - task_ = executor_.Schedule([this]() { TriggerCallback(); }, delay); + if (is_stopped_) { + return false; + } + task_ = executor_->Schedule([this]() { TriggerCallback(); }, delay); return true; } @@ -77,7 +86,7 @@ class Timer : public api::Timer { std::atomic_bool is_stopped_; absl::Duration interval_; std::shared_ptr task_ ABSL_GUARDED_BY(mutex_); - ScheduledExecutor executor_; + api::ScheduledExecutor* absl_nonnull const executor_; }; } // namespace g3