From 5df9ef5eca83e45e7fd4e5d11c4c4137e89c045d Mon Sep 17 00:00:00 2001 From: guogang Date: Fri, 5 Aug 2022 13:29:39 -0700 Subject: [PATCH] Removed API mutex from platform implementation. PiperOrigin-RevId: 465635705 --- .../implementation/windows/ble_medium.cc | 38 ++++++------------- .../implementation/windows/ble_medium.h | 26 +++++-------- .../windows/bluetooth_classic_medium.cc | 12 ------ .../windows/bluetooth_classic_medium.h | 4 -- .../implementation/windows/wifi_lan.h | 5 +-- .../implementation/windows/wifi_lan_medium.cc | 6 --- 6 files changed, 21 insertions(+), 70 deletions(-) diff --git a/internal/platform/implementation/windows/ble_medium.cc b/internal/platform/implementation/windows/ble_medium.cc index b48f84e9..a011d3c3 100644 --- a/internal/platform/implementation/windows/ble_medium.cc +++ b/internal/platform/implementation/windows/ble_medium.cc @@ -131,8 +131,6 @@ BleMedium::BleMedium(api::BluetoothAdapter& adapter) bool BleMedium::StartAdvertising( const std::string& service_id, const ByteArray& advertisement_bytes, const std::string& fast_advertisement_service_uuid) { - absl::MutexLock lock(&mutex_); - try { if (!adapter_->IsEnabled()) { NEARBY_LOGS(WARNING) << "BLE cannot start advertising because the " @@ -219,8 +217,6 @@ bool BleMedium::StartAdvertising( } bool BleMedium::StopAdvertising(const std::string& service_id) { - absl::MutexLock lock(&mutex_); - try { if (!adapter_->IsEnabled()) { NEARBY_LOGS(WARNING) << "BLE cannot stop advertising because the " @@ -262,8 +258,6 @@ bool BleMedium::StartScanning( const std::string& service_id, const std::string& fast_advertisement_service_uuid, DiscoveredPeripheralCallback callback) { - absl::MutexLock lock(&mutex_); - try { if (!adapter_->IsEnabled()) { NEARBY_LOGS(WARNING) << "BLE cannot start scanning because the " @@ -317,8 +311,6 @@ bool BleMedium::StartScanning( } bool BleMedium::StopScanning(const std::string& service_id) { - absl::MutexLock lock(&mutex_); - try { if (!adapter_->IsEnabled()) { NEARBY_LOGS(WARNING) << "BLE cannot stop scanning because the " @@ -470,15 +462,11 @@ void BleMedium::PublisherHandler( } // The publisher is stopped. Clean up the running publisher - { - absl::MutexLock lock(&mutex_); - - if (publisher_ != nullptr) { - NEARBY_LOGS(ERROR) << "Nearby BLE Medium cleaned the publisher."; - publisher_.StatusChanged(publisher_token_); - publisher_ = nullptr; - is_publisher_started_ = false; - } + if (publisher_ != nullptr) { + NEARBY_LOGS(ERROR) << "Nearby BLE Medium cleaned the publisher."; + publisher_.StatusChanged(publisher_token_); + publisher_ = nullptr; + is_publisher_started_ = false; } } @@ -535,16 +523,12 @@ void BleMedium::WatcherHandler( // No matter the reason, should clean up the watcher if it is not empty. // The BLE V1 interface doesn't have API to return the error to upper layer. - { - absl::MutexLock lock(&mutex_); - - if (watcher_ != nullptr) { - NEARBY_LOGS(ERROR) << "Nearby BLE Medium cleaned the watcher."; - watcher_.Stopped(watcher_token_); - watcher_.Received(advertisement_received_token_); - watcher_ = nullptr; - is_watcher_started_ = false; - } + if (watcher_ != nullptr) { + NEARBY_LOGS(ERROR) << "Nearby BLE Medium cleaned the watcher."; + watcher_.Stopped(watcher_token_); + watcher_.Received(advertisement_received_token_); + watcher_ = nullptr; + is_watcher_started_ = false; } } diff --git a/internal/platform/implementation/windows/ble_medium.h b/internal/platform/implementation/windows/ble_medium.h index 336bbee6..28ee984c 100644 --- a/internal/platform/implementation/windows/ble_medium.h +++ b/internal/platform/implementation/windows/ble_medium.h @@ -49,39 +49,33 @@ class BleMedium : public api::BleMedium { bool StartAdvertising( const std::string& service_id, const ByteArray& advertisement_bytes, - const std::string& fast_advertisement_service_uuid) override - ABSL_LOCKS_EXCLUDED(mutex_); + const std::string& fast_advertisement_service_uuid) override; - bool StopAdvertising(const std::string& service_id) override - ABSL_LOCKS_EXCLUDED(mutex_); + bool StopAdvertising(const std::string& service_id) override; // Returns true once the BLE scan has been initiated. bool StartScanning(const std::string& service_id, const std::string& fast_advertisement_service_uuid, - DiscoveredPeripheralCallback callback) override - ABSL_LOCKS_EXCLUDED(mutex_); + DiscoveredPeripheralCallback callback) override; // Returns true once BLE scanning for service_id is well and truly stopped; // after this returns, there must be no more invocations of the // DiscoveredPeripheralCallback passed in to StartScanning() for service_id. - bool StopScanning(const std::string& service_id) override - ABSL_LOCKS_EXCLUDED(mutex_); + bool StopScanning(const std::string& service_id) override; // Returns true once BLE socket connection requests to service_id can be // accepted. bool StartAcceptingConnections(const std::string& service_id, - AcceptedConnectionCallback callback) override - ABSL_LOCKS_EXCLUDED(mutex_); + AcceptedConnectionCallback callback) override; - bool StopAcceptingConnections(const std::string& service_id) override - ABSL_LOCKS_EXCLUDED(mutex_); + bool StopAcceptingConnections(const std::string& service_id) override; // Connects to a BLE peripheral. // On success, returns a new BleSocket. // On error, returns nullptr. - std::unique_ptr Connect( - api::BlePeripheral& peripheral, const std::string& service_id, - CancellationFlag* cancellation_flag) override ABSL_LOCKS_EXCLUDED(mutex_); + std::unique_ptr Connect(api::BlePeripheral& peripheral, + const std::string& service_id, + CancellationFlag* cancellation_flag); private: void PublisherHandler( @@ -99,9 +93,7 @@ class BleMedium : public api::BleMedium { ::winrt::Windows::Devices::Bluetooth::Advertisement:: BluetoothLEAdvertisementWatcherStoppedEventArgs args); - absl::Mutex mutex_; BluetoothAdapter* adapter_; - ByteArray advertisement_byte_ ABSL_GUARDED_BY(mutex_); std::string service_id_; DiscoveredPeripheralCallback advertisement_received_callback_; diff --git a/internal/platform/implementation/windows/bluetooth_classic_medium.cc b/internal/platform/implementation/windows/bluetooth_classic_medium.cc index 0ba8bc47..23adfdb8 100644 --- a/internal/platform/implementation/windows/bluetooth_classic_medium.cc +++ b/internal/platform/implementation/windows/bluetooth_classic_medium.cc @@ -25,7 +25,6 @@ #include #include -#include "absl/synchronization/mutex.h" #include "internal/platform/cancellation_flag.h" #include "internal/platform/cancellation_flag_listener.h" #include "internal/platform/exception.h" @@ -61,8 +60,6 @@ BluetoothClassicMedium::~BluetoothClassicMedium() {} void BluetoothClassicMedium::OnScanModeChanged( BluetoothAdapter::ScanMode scanMode) { - absl::MutexLock lock(&mutex_); - NEARBY_LOGS(INFO) << __func__ << ": OnScanModeChanged is called with scanMode: " << static_cast(scanMode); @@ -113,7 +110,6 @@ void BluetoothClassicMedium::OnScanModeChanged( bool BluetoothClassicMedium::StartDiscovery( BluetoothClassicMedium::DiscoveryCallback discovery_callback) { - absl::MutexLock lock(&mutex_); NEARBY_LOGS(INFO) << "StartDiscovery is called."; bool result = false; @@ -127,7 +123,6 @@ bool BluetoothClassicMedium::StartDiscovery( } bool BluetoothClassicMedium::StopDiscovery() { - absl::MutexLock lock(&mutex_); NEARBY_LOGS(INFO) << "StopDiscovery is called."; bool result = false; @@ -178,7 +173,6 @@ void BluetoothClassicMedium::InitializeDeviceWatcher() { std::unique_ptr BluetoothClassicMedium::ConnectToService( api::BluetoothDevice& remote_device, const std::string& service_uuid, CancellationFlag* cancellation_flag) { - absl::MutexLock lock(&mutex_); NEARBY_LOGS(INFO) << "ConnectToService is called."; if (service_uuid.empty()) { NEARBY_LOGS(ERROR) << __func__ << ": service_uuid not specified."; @@ -390,7 +384,6 @@ bool BluetoothClassicMedium::CheckSdp(RfcommDeviceService requestedService) { std::unique_ptr BluetoothClassicMedium::ListenForService(const std::string& service_name, const std::string& service_uuid) { - absl::MutexLock lock(&mutex_); NEARBY_LOGS(INFO) << "ListenForService is called with service name: " << service_name << "."; if (service_uuid.empty()) { @@ -464,7 +457,6 @@ bool BluetoothClassicMedium::StopScanning() { winrt::fire_and_forget BluetoothClassicMedium::DeviceWatcher_Added( DeviceWatcher sender, DeviceInformation deviceInfo) { - absl::MutexLock lock(&mutex_); NEARBY_LOGS(INFO) << "Device added " << winrt::to_string(deviceInfo.Id()); if (IsWatcherStarted()) { // Represents a Bluetooth device. @@ -506,8 +498,6 @@ winrt::fire_and_forget BluetoothClassicMedium::DeviceWatcher_Added( winrt::fire_and_forget BluetoothClassicMedium::DeviceWatcher_Updated( DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) { - absl::MutexLock lock(&mutex_); - NEARBY_LOGS(INFO) << "Device updated " << discovered_devices_by_id_[deviceInfoUpdate.Id()]->GetName() << " (" @@ -536,7 +526,6 @@ winrt::fire_and_forget BluetoothClassicMedium::DeviceWatcher_Updated( winrt::fire_and_forget BluetoothClassicMedium::DeviceWatcher_Removed( DeviceWatcher sender, DeviceInformationUpdate deviceInfo) { - absl::MutexLock lock(&mutex_); NEARBY_LOGS(INFO) << "Device removed " << discovered_devices_by_id_[deviceInfo.Id()]->GetName() << " (" << winrt::to_string(deviceInfo.Id()) << ")"; @@ -618,7 +607,6 @@ bool BluetoothClassicMedium::StartAdvertising(bool radio_discoverable) { } server_socket_->SetCloseNotifier([&]() { - absl::MutexLock lock(&mutex_); StopAdvertising(); }); diff --git a/internal/platform/implementation/windows/bluetooth_classic_medium.h b/internal/platform/implementation/windows/bluetooth_classic_medium.h index ef90cf75..6b11fe11 100644 --- a/internal/platform/implementation/windows/bluetooth_classic_medium.h +++ b/internal/platform/implementation/windows/bluetooth_classic_medium.h @@ -19,7 +19,6 @@ #include #include -#include "absl/synchronization/mutex.h" #include "internal/platform/implementation/bluetooth_classic.h" #include "internal/platform/implementation/windows/bluetooth_adapter.h" #include "internal/platform/implementation/windows/bluetooth_classic_device.h" @@ -206,9 +205,6 @@ class BluetoothClassicMedium : public api::BluetoothClassicMedium { std::unique_ptr server_socket_ = nullptr; BluetoothServerSocket* raw_server_socket_ = nullptr; bool is_radio_discoverable_ = false; - - // Used to enable thread safe for APIs. - absl::Mutex mutex_; }; } // namespace windows diff --git a/internal/platform/implementation/windows/wifi_lan.h b/internal/platform/implementation/windows/wifi_lan.h index 90674eea..fcecb4dc 100644 --- a/internal/platform/implementation/windows/wifi_lan.h +++ b/internal/platform/implementation/windows/wifi_lan.h @@ -335,14 +335,11 @@ class WifiLanMedium : public api::WifiLanMedium { // callback for discovery api::WifiLanMedium::DiscoveredServiceCallback discovered_service_callback_; - // Protects to access some members - absl::Mutex mutex_; - // Medium Status int medium_status_ = MEDIUM_STATUS_IDLE; // Keep the server socket listener pointer - WifiLanServerSocket* server_socket_ptr_ ABSL_GUARDED_BY(mutex_) = nullptr; + WifiLanServerSocket* server_socket_ptr_ = nullptr; }; } // namespace windows diff --git a/internal/platform/implementation/windows/wifi_lan_medium.cc b/internal/platform/implementation/windows/wifi_lan_medium.cc index 2f4f072d..2eff4c1c 100644 --- a/internal/platform/implementation/windows/wifi_lan_medium.cc +++ b/internal/platform/implementation/windows/wifi_lan_medium.cc @@ -30,7 +30,6 @@ #include "internal/platform/cancellation_flag_listener.h" #include "internal/platform/implementation/windows/utils.h" #include "internal/platform/logging.h" -#include "internal/platform/mutex_lock.h" namespace location { namespace nearby { @@ -45,8 +44,6 @@ bool WifiLanMedium::IsNetworkConnected() const { } bool WifiLanMedium::StartAdvertising(const NsdServiceInfo& nsd_service_info) { - absl::MutexLock lock(&mutex_); - if (!IsAccepting()) { NEARBY_LOGS(WARNING) << "cannot start advertising without accepting connetions."; @@ -329,8 +326,6 @@ std::unique_ptr WifiLanMedium::ConnectToService( std::unique_ptr WifiLanMedium::ListenForService( int port) { - absl::MutexLock lock(&mutex_); - // check current status if (IsAccepting()) { NEARBY_LOGS(WARNING) << "accepting connections already started on port " @@ -343,7 +338,6 @@ std::unique_ptr WifiLanMedium::ListenForService( server_socket_ptr_ = server_socket.get(); server_socket->SetCloseNotifier([this]() { - absl::MutexLock lock(&mutex_); NEARBY_LOGS(INFO) << "server socket was closed on port " << server_socket_ptr_->GetPort(); medium_status_ &= (~MEDIUM_STATUS_ACCEPTING);