Made observer list thread safe

PiperOrigin-RevId: 520713657
This commit is contained in:
Guogang Li
2023-03-30 12:36:11 -07:00
committed by Copybara-Service
parent 14e7832be0
commit 8a66bff097
11 changed files with 54 additions and 29 deletions
@@ -26,11 +26,11 @@ void FakeFastPairScanner::RemoveObserver(Observer* observer) {
}
void FakeFastPairScanner::NotifyDeviceFound(const BlePeripheral& peripheral) {
for (auto& obs : observer_) obs->OnDeviceFound(peripheral);
for (auto& obs : observer_.GetObservers()) obs->OnDeviceFound(peripheral);
}
void FakeFastPairScanner::NotifyDeviceLost(const BlePeripheral& peripheral) {
for (auto& obs : observer_) obs->OnDeviceLost(peripheral);
for (auto& obs : observer_.GetObservers()) obs->OnDeviceLost(peripheral);
}
} // namespace fastpair
@@ -134,13 +134,13 @@ void FastPairScannerImpl::OnDeviceLost(const BlePeripheral& peripheral) {
<< peripheral.GetName();
device_address_advertisement_data_map_.erase(peripheral.GetName());
for (auto& observer : observer_) {
for (auto& observer : observer_.GetObservers()) {
observer->OnDeviceLost(peripheral);
}
}
void FastPairScannerImpl::NotifyDeviceFound(const BlePeripheral& peripheral) {
for (auto& observer : observer_) {
for (auto& observer : observer_.GetObservers()) {
observer->OnDeviceFound(peripheral);
}
}
+5 -6
View File
@@ -15,9 +15,9 @@
#define THIRD_PARTY_NEARBY_FASTPAIR_SCANNING_MOCK_SCANNER_BROKER_H_
#include "gmock/gmock.h"
#include "fastpair/common/fast_pair_device.h"
#include "fastpair/scanning/scanner_broker.h"
#include "internal/base/observer_list.h"
#include "fastpair/common/fast_pair_device.h"
namespace nearby {
namespace fastpair {
@@ -27,7 +27,6 @@ class MockScannerBroker : public ScannerBroker {
MOCK_METHOD(void, StartScanning, (Protocol), (override));
MOCK_METHOD(void, StopScanning, (Protocol), (override));
void AddObserver(Observer* observer) override {
observers_.AddObserver(observer);
}
@@ -37,13 +36,13 @@ class MockScannerBroker : public ScannerBroker {
}
void NotifyDeviceFound(const FastPairDevice& device) {
for (auto& observer : observers_){
observer->OnDeviceFound(device);
for (auto& observer : observers_.GetObservers()) {
observer->OnDeviceFound(device);
}
}
void NotifyDeviceLost(const FastPairDevice& device) {
for (auto& observer : observers_){
void NotifyDeviceLost(const FastPairDevice& device) {
for (auto& observer : observers_.GetObservers()) {
observer->OnDeviceLost(device);
}
}
+2 -2
View File
@@ -72,7 +72,7 @@ void ScannerBrokerImpl::StopFastPairScanning() {
void ScannerBrokerImpl::NotifyDeviceFound(const FastPairDevice& device) {
NEARBY_LOGS(INFO) << __func__ << ": Notifying device found, model id = "
<< device.GetModelId();
for (auto& observer : observers_) {
for (auto& observer : observers_.GetObservers()) {
observer->OnDeviceFound(device);
}
}
@@ -80,7 +80,7 @@ void ScannerBrokerImpl::NotifyDeviceFound(const FastPairDevice& device) {
void ScannerBrokerImpl::NotifyDeviceLost(const FastPairDevice& device) {
NEARBY_LOGS(INFO) << __func__ << ": Notifying device lost, model id = "
<< device.GetModelId();
for (auto& observer : observers_) {
for (auto& observer : observers_.GetObservers()) {
observer->OnDeviceLost(device);
}
}
@@ -27,7 +27,7 @@ void FastPairNotificationController::RemoveObserver(Observer* observer) {
void FastPairNotificationController::NotifyShowDiscovery(
const DeviceMetadata& device) {
for (Observer* observer : observers_) {
for (Observer* observer : observers_.GetObservers()) {
observer->OnUpdateDevice(device);
}
}
+2
View File
@@ -17,6 +17,8 @@ cc_library(
"//location/nearby/cpp/sharing:__subpackages__",
],
deps = [
"//internal/platform:types",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:flat_hash_set",
],
)
+31 -15
View File
@@ -15,10 +15,9 @@
#ifndef THIRD_PARTY_NEARBY_INTERNAL_BASE_OBSERVER_LIST_H_
#define THIRD_PARTY_NEARBY_INTERNAL_BASE_OBSERVER_LIST_H_
#include <string>
#include <vector>
#include "absl/base/thread_annotations.h"
#include "absl/container/flat_hash_set.h"
#include "internal/platform/mutex_lock.h"
namespace nearby {
@@ -29,28 +28,45 @@ class ObserverList {
using const_iterator =
typename absl::flat_hash_set<ObserverType*>::const_iterator;
void AddObserver(ObserverType* observer) { observers_.emplace(observer); }
void AddObserver(ObserverType* observer) ABSL_LOCKS_EXCLUDED(mutex_) {
MutexLock lock(&mutex_);
observers_.insert(observer);
}
void RemoveObserver(ObserverType* observer) { observers_.erase(observer); }
void RemoveObserver(ObserverType* observer) ABSL_LOCKS_EXCLUDED(mutex_) {
MutexLock lock(&mutex_);
observers_.erase(observer);
}
bool HasObserver(ObserverType* observer) {
bool HasObserver(ObserverType* observer) ABSL_LOCKS_EXCLUDED(mutex_) {
MutexLock lock(&mutex_);
return observers_.contains(observer);
}
void Clear() { observers_.clear(); }
void Clear() ABSL_LOCKS_EXCLUDED(mutex_) {
MutexLock lock(&mutex_);
observers_.clear();
}
bool empty() const { return observers_.empty(); }
bool empty() const ABSL_LOCKS_EXCLUDED(mutex_) {
MutexLock lock(&mutex_);
return observers_.empty();
}
int size() const { return observers_.size(); }
int size() const ABSL_LOCKS_EXCLUDED(mutex_) {
MutexLock lock(&mutex_);
return observers_.size();
}
// Supports iterators
iterator begin() { return observers_.begin(); }
iterator end() { return observers_.end(); }
const_iterator begin() const { return observers_.begin(); }
const_iterator end() const { return observers_.end(); }
absl::flat_hash_set<ObserverType*> GetObservers()
ABSL_LOCKS_EXCLUDED(mutex_) {
MutexLock lock(&mutex_);
return observers_;
}
private:
absl::flat_hash_set<ObserverType*> observers_;
mutable Mutex mutex_;
absl::flat_hash_set<ObserverType*> observers_ ABSL_GUARDED_BY(mutex_);
};
} // namespace nearby
+1
View File
@@ -340,6 +340,7 @@ cc_library(
visibility = [
"//connections:__subpackages__",
"//fastpair:__subpackages__",
"//internal/base:__subpackages__",
"//internal/flags:__subpackages__",
"//internal/platform/implementation/windows:__subpackages__",
"//internal/test:__subpackages__",
@@ -53,6 +53,10 @@
#include "internal/platform/os_name.h"
#include "internal/platform/payload_id.h"
#ifdef CreateMutex
#undef CreateMutex
#endif
namespace nearby {
namespace api {
@@ -13,7 +13,6 @@
// limitations under the License.
#ifndef PLATFORM_IMPL_WINDOWS_MUTEX_H_
#define PLATFORM_IMPL_WINDOWS_MUTEX_H_
#include <windows.h>
#include <stdio.h>
#include <synchapi.h>
@@ -65,6 +65,10 @@
#include "internal/platform/implementation/windows/wifi_lan.h"
#include "internal/platform/logging.h"
#ifdef CreateMutex
#undef CreateMutex
#endif
namespace nearby {
namespace api {