mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Merge branch 'google3' to roll forward up to cl/353292511.
This commit is contained in:
@@ -94,6 +94,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,
|
||||
@@ -143,6 +165,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 = [
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -41,7 +41,7 @@ class WebRtcSignalingMessenger : public api::WebRtcSignalingMessenger {
|
||||
void StopReceivingMessages() override;
|
||||
|
||||
private:
|
||||
absl::string_view self_id_;
|
||||
std::string self_id_;
|
||||
connections::LocationHint location_hint_;
|
||||
};
|
||||
|
||||
|
||||
@@ -52,7 +52,6 @@ cc_library(
|
||||
"//platform/base:logging",
|
||||
"//platform/base:util",
|
||||
"//absl/base:core_headers",
|
||||
"//absl/container:flat_hash_map",
|
||||
"//absl/time",
|
||||
],
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user