Merge branch 'master' into release

Change-Id: Iaeb2144000bba05bf9532a55518b9549d7662361
This commit is contained in:
Alexey Polyudov
2020-06-30 00:29:25 -07:00
39 changed files with 1229 additions and 189 deletions
-1
View File
@@ -86,6 +86,5 @@ cc_library(
":types",
"//platform_v2/base",
"//absl/strings",
"//absl/types:any",
],
)
-1
View File
@@ -41,7 +41,6 @@ class ConditionVariable {
// Waits while timeout has not expired for Notify to be called.
// May return prematurely in case of interrupt, if supported by platform.
// Returns kSuccess, or kInterrupted on interrupt.
// If Timeout expired, and Notify was not called, returns kTimeout.
virtual Exception Wait(absl::Duration timeout) = 0;
};
+4
View File
@@ -21,6 +21,8 @@ namespace location {
namespace nearby {
namespace api {
int GetCurrentTid();
// This abstract class is the superclass of all classes representing an
// Executor.
class Executor {
@@ -33,6 +35,8 @@ class Executor {
// https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html#shutdown--
virtual void Shutdown() = 0;
virtual int GetTid(int index) const = 0;
};
} // namespace api
+6 -1
View File
@@ -43,7 +43,12 @@ class LogMessageVoidify {
location::nearby::api::LogMessage::Severity::kError
#define NEARBY_SEVERITY_FATAL \
location::nearby::api::LogMessage::Severity::kFatal
#if defined(_WIN32)
// wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets substituted
// with 0, and it expands to NEARBY_SEVERITY_0. To allow us to keep using this
// syntax, we define this macro to do the same thing as NEARBY_SEVERITY_ERROR.
#define NEARBY_SEVERITY_0 location::nearby::api::LogMessage::Severity::kError
#endif // defined(_WIN32)
#define NEARBY_SEVERITY(severity) NEARBY_SEVERITY_##severity
// Log enabling
-1
View File
@@ -45,7 +45,6 @@ cc_library(
"//absl/base:core_headers",
"//absl/synchronization",
"//absl/time",
"//absl/types:any",
"//thread",
],
)
+2 -3
View File
@@ -34,9 +34,8 @@ class ConditionVariable : public api::ConditionVariable {
return {Exception::kSuccess};
}
Exception Wait(absl::Duration timeout) override {
return cond_var_.WaitWithTimeout(mutex_, timeout)
? Exception{Exception::kTimeout}
: Exception{Exception::kSuccess};
cond_var_.WaitWithTimeout(mutex_, timeout);
return {Exception::kSuccess};
}
void Notify() override { cond_var_.SignalAll(); }
@@ -47,6 +47,11 @@ class MultiThreadExecutor : public api::SubmittableExecutor {
void Shutdown() override { DoShutdown(); }
~MultiThreadExecutor() override { DoShutdown(); }
int GetTid(int index) const override {
const auto* thread = thread_pool_.thread(index);
return thread ? thread->tid() : 0;
}
void ScheduleAfter(absl::Duration delay, Runnable&& runnable) {
if (shutdown_) return;
thread_pool_.ScheduleAt(absl::Now() + delay, std::move(runnable));
+5
View File
@@ -61,6 +61,11 @@ std::string GetPayloadPath(PayloadId payload_id) {
}
} // namespace
int GetCurrentTid() {
const LiveThread* my = Thread_GetMyLiveThread();
return LiveThread_Pthread_TID(my);
}
std::unique_ptr<SubmittableExecutor>
ImplementationPlatform::CreateSingleThreadExecutor() {
return absl::make_unique<g3::SingleThreadExecutor>();
@@ -45,6 +45,9 @@ class ScheduledExecutor final : public api::ScheduledExecutor {
absl::Duration delay) override;
void Shutdown() override { executor_.Shutdown(); }
int GetTid(int index) const override {
return executor_.GetTid(index);
}
private:
SingleThreadExecutor executor_;
};
+1
View File
@@ -65,5 +65,6 @@ cc_test(
"//file/util:temp_path",
"//platform_v2/base",
"//testing/base/public:gunit_main",
"//absl/strings",
],
)
+2 -1
View File
@@ -22,6 +22,7 @@
#include "file/util/temp_path.h"
#include "platform_v2/base/byte_array.h"
#include "gtest/gtest.h"
#include "absl/strings/string_view.h"
namespace location {
namespace nearby {
@@ -36,7 +37,7 @@ class FileTest : public ::testing::Test {
file_ = std::fstream(path_, std::fstream::in | std::fstream::out);
}
void WriteToFile(const std::string& text) {
void WriteToFile(absl::string_view text) {
file_ << text;
file_.flush();
size_ += text.size();
-1
View File
@@ -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
+7 -1
View File
@@ -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_;
};
+2 -2
View File
@@ -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_;
};