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(
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;
}
}
@@ -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<api::BleSocket> Connect(
api::BlePeripheral& peripheral, const std::string& service_id,
CancellationFlag* cancellation_flag) override ABSL_LOCKS_EXCLUDED(mutex_);
std::unique_ptr<api::BleSocket> 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_;
@@ -25,7 +25,6 @@
#include <string>
#include <utility>
#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<int>(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<api::BluetoothSocket> 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<api::BluetoothServerSocket>
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();
});
@@ -19,7 +19,6 @@
#include <memory>
#include <string>
#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<BluetoothServerSocket> 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
@@ -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
@@ -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<api::WifiLanSocket> WifiLanMedium::ConnectToService(
std::unique_ptr<api::WifiLanServerSocket> 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<api::WifiLanServerSocket> 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);