Roll forward up to cl/353292511.

This commit is contained in:
hai007
2021-01-22 12:13:44 -08:00
parent 524bd1d2f3
commit 335b3a8d45
36 changed files with 592 additions and 155 deletions
+33
View File
@@ -80,6 +80,28 @@ cc_library(
],
)
cc_library(
name = "cancellation_flag",
srcs = [
"cancellation_flag.cc",
],
hdrs = [
"cancellation_flag.h",
],
visibility = [
"//core/internal:__subpackages__",
"//platform/api:__subpackages__",
"//platform/impl:__subpackages__",
"//platform/public:__pkg__",
],
deps = [
":base",
":util",
"//absl/container:flat_hash_set",
"//absl/synchronization",
],
)
cc_library(
name = "test_util",
testonly = True,
@@ -129,6 +151,17 @@ cc_test(
],
)
cc_test(
name = "cancellation_flag_test",
srcs = [
"cancellation_flag_test.cc",
],
deps = [
":cancellation_flag",
"//testing/base/public:gunit_main",
],
)
cc_with_non_compile_test(
name = "exception_test",
srcs = [
+32
View File
@@ -0,0 +1,32 @@
#include "platform/base/cancellation_flag.h"
namespace location {
namespace nearby {
CancellationFlag::CancellationFlag() {
mutex_ = std::make_unique<absl::Mutex>();
}
CancellationFlag::CancellationFlag(bool cancelled) {
mutex_ = std::make_unique<absl::Mutex>();
cancelled_ = cancelled;
}
void CancellationFlag::Cancel() {
absl::MutexLock lock(mutex_.get());
if (cancelled_) {
// Someone already cancelled. Return immediately.
return;
}
cancelled_ = true;
}
bool CancellationFlag::Cancelled() const {
absl::MutexLock lock(mutex_.get());
return cancelled_;
}
} // namespace nearby
} // namespace location
+37
View File
@@ -0,0 +1,37 @@
#ifndef PLATFORM_BASE_CANCELLATION_FLAG_H_
#define PLATFORM_BASE_CANCELLATION_FLAG_H_
#include <memory>
#include "absl/synchronization/mutex.h"
namespace location {
namespace nearby {
// A cancellation flag to mark an operation has been cancelled and should be
// cleaned up as soon as possible.
class CancellationFlag {
public:
CancellationFlag();
explicit CancellationFlag(bool cancelled);
CancellationFlag(const CancellationFlag &) = delete;
CancellationFlag &operator=(const CancellationFlag &) = delete;
CancellationFlag(CancellationFlag &&) = default;
CancellationFlag &operator=(CancellationFlag &&) = default;
virtual ~CancellationFlag() = default;
// Set the flag as cancelled.
void Cancel() ABSL_LOCKS_EXCLUDED(mutex_);
// Returns true if the flag has been set to cancelled.
bool Cancelled() const ABSL_LOCKS_EXCLUDED(mutex_);
private:
std::unique_ptr<absl::Mutex> mutex_;
bool cancelled_ ABSL_GUARDED_BY(mutex_) = false;
};
} // namespace nearby
} // namespace location
#endif // PLATFORM_BASE_CANCELLATION_FLAG_H_
@@ -0,0 +1,20 @@
#include "platform/base/cancellation_flag.h"
#include "gtest/gtest.h"
namespace location {
namespace nearby {
TEST(CancellationFlagTest, InitialValueIsFalse) {
CancellationFlag flag;
EXPECT_FALSE(flag.Cancelled());
}
TEST(CancellationFlagTest, CanCancel) {
CancellationFlag flag;
flag.Cancel();
EXPECT_TRUE(flag.Cancelled());
}
} // namespace nearby
} // namespace location
+1 -1
View File
@@ -27,7 +27,7 @@ class WebRtcSignalingMessenger : public api::WebRtcSignalingMessenger {
void StopReceivingMessages() override;
private:
absl::string_view self_id_;
std::string self_id_;
connections::LocationHint location_hint_;
};
-1
View File
@@ -38,7 +38,6 @@ cc_library(
"//platform/base:logging",
"//platform/base:util",
"//absl/base:core_headers",
"//absl/container:flat_hash_map",
"//absl/time",
],
)