Skip to content

Commit

Permalink
Remove lock on BluetoothLEDevice and GattDeviceService (microsoft#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
jg4ye authored May 6, 2021
1 parent 55279da commit 4bb1643
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ private set
}
}

/// <summary>
/// Lock around the <see cref="BluetoothLEDevice"/>
/// </summary>
private SemaphoreSlim bluetoothLEDeviceLock = new SemaphoreSlim(1, 1);

/// <summary>
/// Source for <see cref="Glyph"/>
/// </summary>
Expand Down Expand Up @@ -449,18 +444,8 @@ public void Dispose()
{
Services.Clear();
var temp = bluetoothLEDevice;
try
{
bluetoothLEDeviceLock.Wait();
if (bluetoothLEDevice != null)
{
BluetoothLEDevice = null;
}
}
finally
{
bluetoothLEDeviceLock.Release();
}

BluetoothLEDevice = null;

if (temp != null)
{
Expand Down Expand Up @@ -538,7 +523,6 @@ await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatch
Debug.WriteLine(debugMsg + "In UI thread");
try
{
await bluetoothLEDeviceLock.WaitAsync();
if (bluetoothLEDevice == null)
{
Debug.WriteLine(debugMsg + "Calling BluetoothLEDevice.FromIdAsync");
Expand Down Expand Up @@ -636,10 +620,6 @@ await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatch
// Debugger break here so we can catch unknown exceptions
Debugger.Break();
}
finally
{
bluetoothLEDeviceLock.Release();
}
});

if (ret)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,14 @@ public GattDeviceService Service
}
}

/// <summary>
/// Lock around the <see cref="Service"/>.
/// </summary>
SemaphoreSlim serviceLock = new SemaphoreSlim(1, 1);

public void Dispose()
{
var temp = service;
try
Service = null;
if (temp != null)
{
serviceLock.Wait();
Service = null;
temp.Dispose();
}
finally
{
serviceLock.Release();
}
temp.Dispose();
}

/// <summary>
Expand Down Expand Up @@ -181,15 +171,7 @@ public string UUID
/// <param name="service">The service this class wraps</param>
public ObservableGattDeviceService(GattDeviceService service)
{
try
{
serviceLock.Wait();
Service = service;
}
finally
{
serviceLock.Release();
}
Service = service;
Name = GattServiceUuidHelper.ConvertUuidToName(service.Uuid);
UUID = service.Uuid.ToString();
}
Expand Down Expand Up @@ -370,8 +352,6 @@ private async Task GetAllCharacteristics()

try
{
await serviceLock.WaitAsync();

// Request the necessary access permissions for the service and abort
// if permissions are denied.
GattOpenStatus status = await service.OpenAsync(GattSharingMode.SharedReadAndWrite);
Expand Down Expand Up @@ -417,10 +397,6 @@ private async Task GetAllCharacteristics()
Debug.WriteLine("getAllCharacteristics: Exception - {0}" + ex.Message);
Name += " - Exception: " + ex.Message;
}
finally
{
serviceLock.Release();
}
}

/// <summary>
Expand Down

0 comments on commit 4bb1643

Please sign in to comment.