From e9a6cbc6a73831e9079cc5b97bd3db398ad21c72 Mon Sep 17 00:00:00 2001 From: Ben Tian Date: Tue, 3 Jun 2014 11:50:50 +0800 Subject: [PATCH] Bug 1019372 - Patch 6/6: [bluetooth2] Dispatch part of DiscoveryChangedCallback to main thread, r=echou --- .../bluedroid/BluetoothServiceBluedroid.cpp | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/dom/bluetooth2/bluedroid/BluetoothServiceBluedroid.cpp b/dom/bluetooth2/bluedroid/BluetoothServiceBluedroid.cpp index e365119c9d03..21f6525ac41d 100644 --- a/dom/bluetooth2/bluedroid/BluetoothServiceBluedroid.cpp +++ b/dom/bluetooth2/bluedroid/BluetoothServiceBluedroid.cpp @@ -50,12 +50,12 @@ USING_BLUETOOTH_NAMESPACE static nsString sAdapterBdAddress; static nsString sAdapterBdName; static InfallibleTArray sAdapterBondedAddressArray; -static nsTArray > sChangeDiscoveryRunnableArray; // Static variables below should only be used on *main thread* static const bt_interface_t* sBtInterface; static nsTArray > sControllerArray; static nsTArray sRequestedDeviceCountArray; +static nsTArray > sChangeDiscoveryRunnableArray; static nsTArray > sSetPropertyRunnableArray; static nsTArray > sGetDeviceRunnableArray; static nsTArray > sBondingRunnableArray; @@ -549,18 +549,33 @@ DeviceFoundCallback(int aNumProperties, bt_property_t *aProperties) } } +class DiscoveryStateChangedCallbackTask MOZ_FINAL : public nsRunnable +{ +public: + NS_IMETHOD + Run() + { + MOZ_ASSERT(NS_IsMainThread()); + + if (!sChangeDiscoveryRunnableArray.IsEmpty()) { + BluetoothValue values(true); + DispatchBluetoothReply(sChangeDiscoveryRunnableArray[0], + values, EmptyString()); + + sChangeDiscoveryRunnableArray.RemoveElementAt(0); + } + + return NS_OK; + } +}; + static void DiscoveryStateChangedCallback(bt_discovery_state_t aState) { MOZ_ASSERT(!NS_IsMainThread()); - if (!sChangeDiscoveryRunnableArray.IsEmpty()) { - BluetoothValue values(true); - DispatchBluetoothReply(sChangeDiscoveryRunnableArray[0], - values, EmptyString()); - - sChangeDiscoveryRunnableArray.RemoveElementAt(0); - } + // Redirect to main thread to avoid racing problem + NS_DispatchToMainThread(new DiscoveryStateChangedCallbackTask()); } static void