Removed API mutex from platform implementation.

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