From 7ccbfef89bc0b2d1de063a5f53a33a067d4731f4 Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Tue, 18 Jun 2024 10:18:32 -0700 Subject: [PATCH] internal fix PiperOrigin-RevId: 644430280 --- internal/platform/implementation/g3/BUILD | 1 + .../implementation/g3/scheduled_executor.cc | 24 ++++++++++++------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/internal/platform/implementation/g3/BUILD b/internal/platform/implementation/g3/BUILD index 2482a124..cb728406 100644 --- a/internal/platform/implementation/g3/BUILD +++ b/internal/platform/implementation/g3/BUILD @@ -51,6 +51,7 @@ cc_library( "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/log:log_streamer", "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:str_format", "@com_google_absl//absl/synchronization", "@com_google_absl//absl/time", "@com_google_absl//absl/types:span", diff --git a/internal/platform/implementation/g3/scheduled_executor.cc b/internal/platform/implementation/g3/scheduled_executor.cc index e2994064..b23ef77a 100644 --- a/internal/platform/implementation/g3/scheduled_executor.cc +++ b/internal/platform/implementation/g3/scheduled_executor.cc @@ -16,8 +16,12 @@ #include #include +#include #include +#include "absl/strings/str_format.h" +#include "absl/synchronization/mutex.h" +#include "absl/time/time.h" #include "internal/platform/implementation/cancelable.h" #include "internal/platform/medium_environment.h" #include "internal/platform/runnable.h" @@ -32,13 +36,14 @@ class ScheduledCancelable : public api::Cancelable { public: bool Cancel() override { Status expected = kNotRun; - while (expected == kNotRun) { - if (status_.compare_exchange_strong(expected, kCanceled)) { - return true; - } + if (status_.compare_exchange_strong(expected, kCanceled)) { + return true; } return false; } + + bool IsCanceled() const { return status_ == kCanceled; } + bool MarkExecuted() { Status expected = kNotRun; while (expected == kNotRun) { @@ -61,7 +66,7 @@ class ScheduledCancelable : public api::Cancelable { } // namespace ScheduledExecutor::ScheduledExecutor() { - absl::optional fake_clock = + std::optional fake_clock = MediumEnvironment::Instance().GetSimulatedClock(); if (fake_clock.has_value()) { name_ = absl::StrFormat("G3 scheduled executor %p", this); @@ -70,7 +75,7 @@ ScheduledExecutor::ScheduledExecutor() { } ScheduledExecutor::~ScheduledExecutor() { - absl::optional fake_clock = + std::optional fake_clock = MediumEnvironment::Instance().GetSimulatedClock(); if (fake_clock.has_value()) { (*fake_clock)->RemoveObserver(name_); @@ -86,11 +91,12 @@ std::shared_ptr ScheduledExecutor::Schedule( } Runnable task = [this, scheduled_cancelable, runnable = std::move(runnable)]() mutable { - if (!executor_.InShutdown() && scheduled_cancelable->MarkExecuted()) { + if (!executor_.InShutdown() && !scheduled_cancelable->IsCanceled() && + scheduled_cancelable->MarkExecuted()) { runnable(); } }; - absl::optional fake_clock = + std::optional fake_clock = MediumEnvironment::Instance().GetSimulatedClock(); if (fake_clock.has_value()) { absl::Time trigger_time = (*fake_clock)->Now() + delay; @@ -104,7 +110,7 @@ std::shared_ptr ScheduledExecutor::Schedule( } void ScheduledExecutor::RunReadyTasks() { - absl::optional fake_clock = + std::optional fake_clock = MediumEnvironment::Instance().GetSimulatedClock(); if (executor_.InShutdown()) { return;