mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Roll forward to cl/318932159
Signed-off-by: Alexey Polyudov <apolyudov@google.com> Change-Id: Ic18f5d83a59deaf81afec14dcf2187b8c70195eb
This commit is contained in:
@@ -53,7 +53,6 @@ cc_library(
|
||||
"//absl/base:core_headers",
|
||||
"//absl/container:flat_hash_map",
|
||||
"//absl/time",
|
||||
"//absl/types:any",
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include "platform_v2/public/logging.h"
|
||||
#include "platform_v2/public/mutex.h"
|
||||
#include "platform_v2/public/single_thread_executor.h"
|
||||
#include "platform_v2/public/system_clock.h"
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/time/time.h"
|
||||
@@ -68,7 +69,12 @@ TEST(ConditionVariableTest, WaitTerminatesOnTimeoutWithoutNotify) {
|
||||
Mutex mutex;
|
||||
ConditionVariable cond{&mutex};
|
||||
MutexLock lock(&mutex);
|
||||
EXPECT_EQ(cond.Wait(absl::Milliseconds(100)), Exception{Exception::kTimeout});
|
||||
|
||||
const absl::Duration kWaitTime = absl::Milliseconds(100);
|
||||
absl::Time start = SystemClock::ElapsedRealtime();
|
||||
cond.Wait(kWaitTime);
|
||||
absl::Duration duration = SystemClock::ElapsedRealtime() - start;
|
||||
EXPECT_GE(duration, kWaitTime);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -64,6 +64,12 @@ class ScheduledExecutor final {
|
||||
DoShutdown();
|
||||
}
|
||||
|
||||
int GetTid(int index) const {
|
||||
MutexLock lock(&mutex_);
|
||||
return impl_->GetTid(index);
|
||||
}
|
||||
int Tid() const { return GetTid(0); }
|
||||
|
||||
Cancelable Schedule(Runnable&& runnable, absl::Duration duration)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_) {
|
||||
MutexLock lock(&mutex_);
|
||||
@@ -79,7 +85,7 @@ class ScheduledExecutor final {
|
||||
}
|
||||
}
|
||||
|
||||
Mutex mutex_;
|
||||
mutable Mutex mutex_;
|
||||
std::unique_ptr<api::ScheduledExecutor> ABSL_GUARDED_BY(mutex_) impl_;
|
||||
};
|
||||
|
||||
|
||||
@@ -71,8 +71,8 @@ class SettableFuture : public api::SettableFuture<T> {
|
||||
MutexLock lock(&mutex_);
|
||||
while (!done_) {
|
||||
absl::Time start_time = SystemClock::ElapsedRealtime();
|
||||
if (completed_.Wait(timeout).Raised(Exception::kTimeout)) {
|
||||
SetExceptionLocked({Exception::kTimeout});
|
||||
if (completed_.Wait(timeout).Raised(Exception::kInterrupted)) {
|
||||
SetExceptionLocked({Exception::kInterrupted});
|
||||
break;
|
||||
}
|
||||
absl::Duration spent = SystemClock::ElapsedRealtime() - start_time;
|
||||
|
||||
@@ -32,6 +32,7 @@ class SingleThreadExecutor final : public SubmittableExecutor {
|
||||
~SingleThreadExecutor() override = default;
|
||||
SingleThreadExecutor(SingleThreadExecutor&&) = default;
|
||||
SingleThreadExecutor& operator=(SingleThreadExecutor&&) = default;
|
||||
int Tid() const { return GetTid(0); }
|
||||
};
|
||||
|
||||
} // namespace nearby
|
||||
|
||||
@@ -31,6 +31,8 @@
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
|
||||
inline int GetCurrentTid() { return api::GetCurrentTid(); }
|
||||
|
||||
// Main interface to be used by platform as a base class for
|
||||
// - MultiThreadExecutor
|
||||
// - SingleThreadExecutor
|
||||
@@ -55,6 +57,11 @@ class SubmittableExecutor : public api::SubmittableExecutor {
|
||||
if (impl_) impl_->Execute(std::move(runnable));
|
||||
}
|
||||
|
||||
int GetTid(int index) const ABSL_LOCKS_EXCLUDED(mutex_) override {
|
||||
MutexLock lock(&mutex_);
|
||||
return impl_ ? impl_->GetTid(index) : 0;
|
||||
}
|
||||
|
||||
void Shutdown() ABSL_LOCKS_EXCLUDED(mutex_) override {
|
||||
MutexLock lock(&mutex_);
|
||||
DoShutdown();
|
||||
@@ -100,7 +107,7 @@ class SubmittableExecutor : public api::SubmittableExecutor {
|
||||
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_) override {
|
||||
return impl_ ? impl_->DoSubmit(std::move(wrapped_callable)) : false;
|
||||
}
|
||||
Mutex mutex_;
|
||||
mutable Mutex mutex_;
|
||||
std::unique_ptr<api::SubmittableExecutor> ABSL_GUARDED_BY(mutex_) impl_;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user