From 87e044fa89b65233113101acfa19c09b805a15f0 Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Thu, 6 Apr 2023 17:02:33 -0700 Subject: [PATCH] Internal bug fix PiperOrigin-RevId: 522474442 --- internal/platform/implementation/windows/BUILD | 1 + internal/platform/implementation/windows/timer.cc | 13 ++++++++++++- internal/platform/implementation/windows/timer.h | 5 ++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/internal/platform/implementation/windows/BUILD b/internal/platform/implementation/windows/BUILD index b1705b0e..f102b395 100644 --- a/internal/platform/implementation/windows/BUILD +++ b/internal/platform/implementation/windows/BUILD @@ -49,6 +49,7 @@ cc_library( "//internal/platform:base", "//internal/platform:logging", "//internal/platform/implementation:types", + "//internal/platform/implementation/windows:comm", "//internal/platform/implementation/windows/generated:types", "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/synchronization", diff --git a/internal/platform/implementation/windows/timer.cc b/internal/platform/implementation/windows/timer.cc index b6fbed51..c27846ef 100644 --- a/internal/platform/implementation/windows/timer.cc +++ b/internal/platform/implementation/windows/timer.cc @@ -92,7 +92,18 @@ bool Timer::FireNow() { return false; } - callback_(); + if (task_executor_ == nullptr) { + task_executor_ = std::make_unique(); + } + + if (task_executor_ == nullptr) { + NEARBY_LOGS(ERROR) + << "Failed to fire the task due to cannot create executor."; + return false; + } + + task_executor_->Execute([&]() { callback_(); }); + return true; } diff --git a/internal/platform/implementation/windows/timer.h b/internal/platform/implementation/windows/timer.h index 9c56c253..8b9d9806 100644 --- a/internal/platform/implementation/windows/timer.h +++ b/internal/platform/implementation/windows/timer.h @@ -22,6 +22,7 @@ #include "absl/base/thread_annotations.h" #include "absl/synchronization/mutex.h" #include "internal/platform/implementation/timer.h" +#include "internal/platform/implementation/windows/submittable_executor.h" namespace nearby { namespace windows { @@ -43,9 +44,11 @@ class Timer : public api::Timer { mutable absl::Mutex mutex_; int delay_ ABSL_GUARDED_BY(mutex_); int interval_ ABSL_GUARDED_BY(mutex_); - absl::AnyInvocable callback_ ABSL_GUARDED_BY(mutex_); + absl::AnyInvocable callback_; HANDLE handle_ ABSL_GUARDED_BY(mutex_) = nullptr; HANDLE timer_queue_handle_ ABSL_GUARDED_BY(mutex_) = nullptr; + std::unique_ptr task_executor_ ABSL_GUARDED_BY(mutex_) = + nullptr; }; } // namespace windows