From 99c5e19f3f41f224457512a7ca646b940926e004 Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Mon, 24 Jul 2023 18:14:47 -0700 Subject: [PATCH] internal update PiperOrigin-RevId: 550729573 --- .../platform/implementation/windows/timer.cc | 25 ++++++--------- .../platform/implementation/windows/timer.h | 8 ++--- .../implementation/windows/timer_test.cc | 31 +++++-------------- 3 files changed, 21 insertions(+), 43 deletions(-) diff --git a/internal/platform/implementation/windows/timer.cc b/internal/platform/implementation/windows/timer.cc index c4e35241..c27846ef 100644 --- a/internal/platform/implementation/windows/timer.cc +++ b/internal/platform/implementation/windows/timer.cc @@ -1,4 +1,4 @@ -// Copyright 2021-2023 Google LLC +// Copyright 2021 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -14,9 +14,10 @@ #include "internal/platform/implementation/windows/timer.h" -#include "absl/functional/any_invocable.h" +#include + +#include "absl/synchronization/mutex.h" #include "internal/platform/logging.h" -#include "internal/platform/mutex_lock.h" namespace nearby { namespace windows { @@ -25,7 +26,7 @@ Timer::~Timer() { Stop(); } bool Timer::Create(int delay, int interval, absl::AnyInvocable callback) { - MutexLock lock(&mutex_); + absl::MutexLock lock(&mutex_); if ((delay < 0) || (interval < 0)) { NEARBY_LOGS(WARNING) << "Delay and interval shouldn\'t be negative value."; @@ -45,17 +46,10 @@ bool Timer::Create(int delay, int interval, delay_ = delay; interval_ = interval; callback_ = std::move(callback); - timer_callback_ = [&]() { - MutexLock lock(&mutex_); - if (timer_queue_handle_ != nullptr && callback_ != nullptr) { - callback_(); - } - }; if (!CreateTimerQueueTimer(&handle_, timer_queue_handle_, static_cast(TimerRoutine), - &timer_callback_, delay, interval, - WT_EXECUTEDEFAULT)) { + &callback_, delay, interval, WT_EXECUTEDEFAULT)) { if (!DeleteTimerQueueEx(timer_queue_handle_, nullptr)) { NEARBY_LOGS(ERROR) << "Failed to create timer in timer queue."; } @@ -67,7 +61,7 @@ bool Timer::Create(int delay, int interval, } bool Timer::Stop() { - MutexLock lock(&mutex_); + absl::MutexLock lock(&mutex_); if (timer_queue_handle_ == nullptr) { return true; @@ -92,7 +86,7 @@ bool Timer::Stop() { } bool Timer::FireNow() { - MutexLock lock(&mutex_); + absl::MutexLock lock(&mutex_); if (!timer_queue_handle_ || !callback_) { return false; @@ -107,7 +101,8 @@ bool Timer::FireNow() { << "Failed to fire the task due to cannot create executor."; return false; } - task_executor_->Execute([&]() { timer_callback_(); }); + + task_executor_->Execute([&]() { callback_(); }); return true; } diff --git a/internal/platform/implementation/windows/timer.h b/internal/platform/implementation/windows/timer.h index fe2d2ae9..8b9d9806 100644 --- a/internal/platform/implementation/windows/timer.h +++ b/internal/platform/implementation/windows/timer.h @@ -1,4 +1,4 @@ -// Copyright 2021-2023 Google LLC +// Copyright 2021 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -20,10 +20,9 @@ #include #include "absl/base/thread_annotations.h" -#include "absl/functional/any_invocable.h" +#include "absl/synchronization/mutex.h" #include "internal/platform/implementation/timer.h" #include "internal/platform/implementation/windows/submittable_executor.h" -#include "internal/platform/mutex.h" namespace nearby { namespace windows { @@ -42,11 +41,10 @@ class Timer : public api::Timer { private: static void CALLBACK TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired); - mutable RecursiveMutex mutex_; + mutable absl::Mutex mutex_; int delay_ ABSL_GUARDED_BY(mutex_); int interval_ ABSL_GUARDED_BY(mutex_); absl::AnyInvocable callback_; - absl::AnyInvocable timer_callback_ = nullptr; 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_) = diff --git a/internal/platform/implementation/windows/timer_test.cc b/internal/platform/implementation/windows/timer_test.cc index be3e7964..725397cd 100644 --- a/internal/platform/implementation/windows/timer_test.cc +++ b/internal/platform/implementation/windows/timer_test.cc @@ -1,4 +1,4 @@ -// Copyright 2021-2023 Google LLC +// Copyright 2021 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -14,8 +14,12 @@ #include "internal/platform/implementation/timer.h" +#include // NOLINT +// NOLINT +#include +#include // NOLINT + #include "gtest/gtest.h" -#include "absl/time/time.h" #include "internal/platform/implementation/platform.h" namespace nearby { @@ -42,7 +46,7 @@ TEST(Timer, DISABLED_TestRepeatTimer) { ASSERT_TRUE(timer != nullptr); EXPECT_TRUE(timer->Create(300, 300, [&]() { ++count; })); - absl::SleepFor(absl::Seconds(1)); + std::this_thread::sleep_for(std::chrono::seconds(1)); EXPECT_TRUE(timer->Stop()); EXPECT_EQ(count, 3); } @@ -53,27 +57,8 @@ TEST(Timer, DISABLED_TestFireNow) { auto timer = nearby::api::ImplementationPlatform::CreateTimer(); EXPECT_TRUE(timer != nullptr); - EXPECT_TRUE(timer->Create(3000, 3000, [&]() { - absl::SleepFor(absl::Milliseconds(1000)); - ++count; - })); + EXPECT_TRUE(timer->Create(3000, 3000, [&]() { ++count; })); EXPECT_TRUE(timer->FireNow()); - absl::SleepFor(absl::Milliseconds(100)); - EXPECT_TRUE(timer->Stop()); - EXPECT_EQ(count, 1); -} - -TEST(Timer, DISABLED_TestWaitForRunningCallback) { - int count = 0; - - auto timer = nearby::api::ImplementationPlatform::CreateTimer(); - - EXPECT_TRUE(timer != nullptr); - EXPECT_TRUE(timer->Create(1000, 0, [&]() { - absl::SleepFor(absl::Milliseconds(3000)); - ++count; - })); - absl::SleepFor(absl::Milliseconds(1050)); EXPECT_TRUE(timer->Stop()); EXPECT_EQ(count, 1); }