Skip to content

Commit

Permalink
[Improve] MutexFactory does not need to be async
Browse files Browse the repository at this point in the history
  • Loading branch information
chipweinberger committed Nov 16, 2023
1 parent 2235473 commit f1e8eef
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 26 deletions.
6 changes: 3 additions & 3 deletions lib/src/bluetooth_characteristic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class BluetoothCharacteristic {
}

// Only allow a single ble operation to be underway at a time
_Mutex mtx = await _MutexFactory.getMutexForKey("global");
_Mutex mtx = _MutexFactory.getMutexForKey("global");
await mtx.take();

// return value
Expand Down Expand Up @@ -172,7 +172,7 @@ class BluetoothCharacteristic {
}

// Only allow a single ble operation to be underway at a time
_Mutex mtx = await _MutexFactory.getMutexForKey("global");
_Mutex mtx = _MutexFactory.getMutexForKey("global");
await mtx.take();

try {
Expand Down Expand Up @@ -238,7 +238,7 @@ class BluetoothCharacteristic {
}

// Only allow a single ble operation to be underway at a time
_Mutex mtx = await _MutexFactory.getMutexForKey("global");
_Mutex mtx = _MutexFactory.getMutexForKey("global");
await mtx.take();

try {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/bluetooth_descriptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class BluetoothDescriptor {
}

// Only allow a single ble operation to be underway at a time
_Mutex mtx = await _MutexFactory.getMutexForKey("global");
_Mutex mtx = _MutexFactory.getMutexForKey("global");
await mtx.take();

// return value
Expand Down Expand Up @@ -134,7 +134,7 @@ class BluetoothDescriptor {
}

// Only allow a single ble operation to be underway at a time
_Mutex mtx = await _MutexFactory.getMutexForKey("global");
_Mutex mtx = _MutexFactory.getMutexForKey("global");
await mtx.take();

try {
Expand Down
18 changes: 9 additions & 9 deletions lib/src/bluetooth_device.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ class BluetoothDevice {
int? mtu = 512,
}) async {
// make sure no one else is calling disconnect
_Mutex dmtx = await _MutexFactory.getMutexForKey("disconnect");
_Mutex dmtx = _MutexFactory.getMutexForKey("disconnect");
bool dtook = await dmtx.take();

// Only allow a single ble operation to be underway at a time
_Mutex mtx = await _MutexFactory.getMutexForKey("global");
_Mutex mtx = _MutexFactory.getMutexForKey("global");
await mtx.take();

try {
Expand Down Expand Up @@ -165,11 +165,11 @@ class BluetoothDevice {
/// of the fbp operation queue, which is useful to cancel an in-progress connection attempt.
Future<void> disconnect({int timeout = 35, bool queue = true}) async {
// Only allow a single disconnect operation at a time
_Mutex dtx = await _MutexFactory.getMutexForKey("disconnect");
_Mutex dtx = _MutexFactory.getMutexForKey("disconnect");
await dtx.take();

// Only allow a single ble operation to be underway at a time?
_Mutex mtx = await _MutexFactory.getMutexForKey("global");
_Mutex mtx = _MutexFactory.getMutexForKey("global");
if (queue) {
await mtx.take();
}
Expand Down Expand Up @@ -209,7 +209,7 @@ class BluetoothDevice {
}

// Only allow a single ble operation to be underway at a time
_Mutex mtx = await _MutexFactory.getMutexForKey("global");
_Mutex mtx = _MutexFactory.getMutexForKey("global");
await mtx.take();

List<BluetoothService> result = [];
Expand Down Expand Up @@ -324,7 +324,7 @@ class BluetoothDevice {
}

// Only allow a single ble operation to be underway at a time
_Mutex mtx = await _MutexFactory.getMutexForKey("global");
_Mutex mtx = _MutexFactory.getMutexForKey("global");
await mtx.take();

int rssi = 0;
Expand Down Expand Up @@ -375,7 +375,7 @@ class BluetoothDevice {
}

// Only allow a single ble operation to be underway at a time
_Mutex mtx = await _MutexFactory.getMutexForKey("global");
_Mutex mtx = _MutexFactory.getMutexForKey("global");
await mtx.take();

var mtu = 0;
Expand Down Expand Up @@ -482,7 +482,7 @@ class BluetoothDevice {
}

// Only allow a single ble operation to be underway at a time
_Mutex mtx = await _MutexFactory.getMutexForKey("global");
_Mutex mtx = _MutexFactory.getMutexForKey("global");
await mtx.take();

try {
Expand Down Expand Up @@ -525,7 +525,7 @@ class BluetoothDevice {
}

// Only allow a single ble operation to be underway at a time
_Mutex mtx = await _MutexFactory.getMutexForKey("global");
_Mutex mtx = _MutexFactory.getMutexForKey("global");
await mtx.take();

try {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/flutter_blue_plus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ class FlutterBluePlus {
dynamic out;

// only allow 1 invocation at a time (guarentees that hot restart finishes)
_Mutex mtx = await _MutexFactory.getMutexForKey("invokeMethod");
_Mutex mtx = _MutexFactory.getMutexForKey("invokeMethod");
await mtx.take();

try {
Expand Down
14 changes: 3 additions & 11 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -338,18 +338,10 @@ class _Mutex {

// Create mutexes in a parrallel-safe way,
class _MutexFactory {
static final _Mutex _global = _Mutex();
static final Map<String, _Mutex> _all = {};

static Future<_Mutex> getMutexForKey(String key) async {
_Mutex? value;
await _global.take();
{
_all[key] ??= _Mutex();
value = _all[key];
}
_global.give();
return value!;
static _Mutex getMutexForKey(String key) {
_all[key] ??= _Mutex();
return _all[key]!;
}
}

Expand Down

0 comments on commit f1e8eef

Please sign in to comment.