internal update

PiperOrigin-RevId: 550729573
This commit is contained in:
Guogang Li
2023-07-24 18:15:44 -07:00
committed by Copybara-Service
parent 37b9119615
commit 99c5e19f3f
3 changed files with 21 additions and 43 deletions
@@ -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 <memory>
#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<void()> 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<WAITORTIMERCALLBACK>(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;
}
@@ -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 <memory>
#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<void()> callback_;
absl::AnyInvocable<void()> timer_callback_ = nullptr;
HANDLE handle_ ABSL_GUARDED_BY(mutex_) = nullptr;
HANDLE timer_queue_handle_ ABSL_GUARDED_BY(mutex_) = nullptr;
std::unique_ptr<SubmittableExecutor> task_executor_ ABSL_GUARDED_BY(mutex_) =
@@ -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 <chrono> // NOLINT
// NOLINT
#include <memory>
#include <thread> // 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);
}