mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 14:46:12 -04:00
Use a shared executor for g3 timers.
PiperOrigin-RevId: 933431037
This commit is contained in:
committed by
Copybara-Service
parent
a4a8e91a0b
commit
0cceb4079f
@@ -59,10 +59,11 @@ cc_library(
|
||||
"//internal/platform/implementation:types",
|
||||
"//internal/platform/implementation/shared:count_down_latch",
|
||||
"//internal/platform/implementation/shared:posix_mutex",
|
||||
"//internal/test",
|
||||
"@com_google_absl//absl/base:core_headers",
|
||||
"@com_google_absl//absl/base:nullability",
|
||||
"@com_google_absl//absl/container:btree",
|
||||
"@com_google_absl//absl/container:flat_hash_map",
|
||||
"@com_google_absl//absl/functional:any_invocable",
|
||||
"@com_google_absl//absl/strings",
|
||||
"@com_google_absl//absl/strings:str_format",
|
||||
"@com_google_absl//absl/synchronization",
|
||||
@@ -217,6 +218,7 @@ cc_library(
|
||||
"//internal/platform/implementation/shared:file",
|
||||
"//third_party/gloop/thread",
|
||||
"@com_google_absl//absl/base:core_headers",
|
||||
"@com_google_absl//absl/base:no_destructor",
|
||||
"@com_google_absl//absl/status",
|
||||
"@com_google_absl//absl/status:statusor",
|
||||
"@com_google_absl//absl/strings",
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include <string>
|
||||
|
||||
#include "absl/base/attributes.h"
|
||||
#include "absl/base/no_destructor.h"
|
||||
#include "absl/status/status.h"
|
||||
#include "absl/status/statusor.h"
|
||||
#include "absl/strings/str_cat.h"
|
||||
@@ -239,7 +240,8 @@ ImplementationPlatform::CreateConditionVariable(Mutex* mutex) {
|
||||
}
|
||||
|
||||
std::unique_ptr<Timer> ImplementationPlatform::CreateTimer() {
|
||||
return std::make_unique<g3::Timer>();
|
||||
static absl::NoDestructor<g3::ScheduledExecutor> timer_executor;
|
||||
return std::make_unique<g3::Timer>(timer_executor.get());
|
||||
}
|
||||
|
||||
std::unique_ptr<nearby::api::DeviceInfo>
|
||||
|
||||
@@ -19,10 +19,13 @@
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/base/nullability.h"
|
||||
#include "absl/base/thread_annotations.h"
|
||||
#include "absl/functional/any_invocable.h"
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "absl/time/time.h"
|
||||
#include "internal/platform/implementation/g3/scheduled_executor.h"
|
||||
#include "internal/platform/implementation/cancelable.h"
|
||||
#include "internal/platform/implementation/scheduled_executor.h"
|
||||
#include "internal/platform/implementation/timer.h"
|
||||
|
||||
namespace nearby {
|
||||
@@ -30,8 +33,11 @@ namespace g3 {
|
||||
|
||||
class Timer : public api::Timer {
|
||||
public:
|
||||
Timer() = default;
|
||||
~Timer() override = default;
|
||||
explicit Timer(api::ScheduledExecutor* absl_nonnull executor)
|
||||
: executor_(executor) {};
|
||||
~Timer() override {
|
||||
Stop();
|
||||
};
|
||||
|
||||
bool Create(int delay, int interval,
|
||||
absl::AnyInvocable<void()> callback) override {
|
||||
@@ -52,13 +58,16 @@ class Timer : public api::Timer {
|
||||
task_.reset();
|
||||
return result;
|
||||
}
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
bool Schedule(absl::Duration delay) {
|
||||
absl::MutexLock lock(mutex_);
|
||||
task_ = executor_.Schedule([this]() { TriggerCallback(); }, delay);
|
||||
if (is_stopped_) {
|
||||
return false;
|
||||
}
|
||||
task_ = executor_->Schedule([this]() { TriggerCallback(); }, delay);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -77,7 +86,7 @@ class Timer : public api::Timer {
|
||||
std::atomic_bool is_stopped_;
|
||||
absl::Duration interval_;
|
||||
std::shared_ptr<api::Cancelable> task_ ABSL_GUARDED_BY(mutex_);
|
||||
ScheduledExecutor executor_;
|
||||
api::ScheduledExecutor* absl_nonnull const executor_;
|
||||
};
|
||||
|
||||
} // namespace g3
|
||||
|
||||
Reference in New Issue
Block a user