mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 14:46:12 -04:00
Make TimerImpl thread-safe.
PiperOrigin-RevId: 813938502
This commit is contained in:
committed by
Copybara-Service
parent
05ab63f71a
commit
2c7f4de3e6
+1
-16
@@ -253,26 +253,11 @@ cc_library(
|
||||
"timer_impl.h",
|
||||
],
|
||||
visibility = [
|
||||
"//connections:__subpackages__",
|
||||
"//internal/account:__subpackages__",
|
||||
"//internal/auth:__subpackages__",
|
||||
"//internal/auth/credential_store:__subpackages__",
|
||||
"//internal/base:__subpackages__",
|
||||
"//internal/crypto:__subpackages__",
|
||||
"//internal/data:__subpackages__",
|
||||
"//internal/interop:__pkg__",
|
||||
"//internal/network:__subpackages__",
|
||||
"//internal/platform:__subpackages__",
|
||||
"//internal/preferences:__subpackages__",
|
||||
"//internal/proto/analytics:__subpackages__",
|
||||
"//internal/test:__subpackages__",
|
||||
"//internal/weave:__subpackages__",
|
||||
"//:__subpackages__",
|
||||
"//location/nearby/apps:__subpackages__",
|
||||
"//location/nearby/cpp:__subpackages__",
|
||||
"//location/nearby/sharing/sdk:__subpackages__",
|
||||
"//location/nearby/testing/nearby_native:__subpackages__",
|
||||
"//presence:__subpackages__",
|
||||
"//sharing:__subpackages__",
|
||||
],
|
||||
deps = [
|
||||
":base",
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <utility>
|
||||
|
||||
#include "absl/functional/any_invocable.h"
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "internal/platform/implementation/platform.h"
|
||||
#include "internal/platform/logging.h"
|
||||
|
||||
@@ -24,17 +25,18 @@ namespace nearby {
|
||||
|
||||
bool TimerImpl::Start(int delay, int period,
|
||||
absl::AnyInvocable<void()> callback) {
|
||||
if (internal_timer_ != nullptr) {
|
||||
LOG(INFO) << "The timer is already running.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (delay < 0) {
|
||||
delay = 0;
|
||||
}
|
||||
if (period < 0) {
|
||||
period = 0;
|
||||
}
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (internal_timer_ != nullptr) {
|
||||
LOG(INFO) << "The timer is already running.";
|
||||
return false;
|
||||
}
|
||||
|
||||
internal_timer_ = api::ImplementationPlatform::CreateTimer();
|
||||
if (!internal_timer_->Create(delay, period, std::move(callback))) {
|
||||
LOG(INFO) << "Failed to create timer.";
|
||||
@@ -45,6 +47,7 @@ bool TimerImpl::Start(int delay, int period,
|
||||
}
|
||||
|
||||
void TimerImpl::Stop() {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (internal_timer_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
@@ -53,6 +56,9 @@ void TimerImpl::Stop() {
|
||||
internal_timer_ = nullptr;
|
||||
}
|
||||
|
||||
bool TimerImpl::IsRunning() { return (internal_timer_ != nullptr); }
|
||||
bool TimerImpl::IsRunning() {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
return (internal_timer_ != nullptr);
|
||||
}
|
||||
|
||||
} // namespace nearby
|
||||
|
||||
@@ -17,7 +17,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/timer.h"
|
||||
|
||||
@@ -32,7 +34,8 @@ class TimerImpl : public Timer {
|
||||
bool IsRunning() override;
|
||||
|
||||
private:
|
||||
std::unique_ptr<api::Timer> internal_timer_ = nullptr;
|
||||
absl::Mutex mutex_;
|
||||
std::unique_ptr<api::Timer> internal_timer_ ABSL_GUARDED_BY(mutex_) = nullptr;
|
||||
};
|
||||
|
||||
} // namespace nearby
|
||||
|
||||
Reference in New Issue
Block a user