OSDN Git Service

Protect concurrent access to local state used during BLE scanning
authorJakub Pawlowski <jpawlowski@google.com>
Wed, 17 May 2017 22:23:22 +0000 (15:23 -0700)
committerJakub Pawlowski <jpawlowski@google.com>
Thu, 18 May 2017 06:26:18 +0000 (23:26 -0700)
Make sure that all access happens on jni thread

Bug: 38298938
Test: Code compilation
Change-Id: I38246626d2c6390586b314b27889dd9ba905f406
(cherry picked from commit eb43e9cf12139aeb227e26a8201a80974129c553)

btif/src/btif_ble_scanner.cc

index 2e24ecc..162935b 100644 (file)
@@ -83,6 +83,7 @@ struct equal_to<bt_bdaddr_t> {
 
 namespace {
 
+// all access to this variable should be done on the jni thread
 std::unordered_set<bt_bdaddr_t> p_dev_cb;
 
 void btif_gattc_add_remote_bdaddr(BD_ADDR p_bda, uint8_t addr_type) {
@@ -258,15 +259,20 @@ class BleScannerInterfaceImpl : public BleScannerInterface {
   }
 
   void Scan(bool start) override {
-    if (!start) {
-      do_in_bta_thread(FROM_HERE, Bind(&BTA_DmBleObserve, false, 0, nullptr));
-      return;
-    }
-
-    btif_gattc_init_dev_cb();
-    do_in_bta_thread(FROM_HERE,
-                     Bind(&BTA_DmBleObserve, true, 0,
-                          (tBTA_DM_SEARCH_CBACK*)bta_scan_results_cb));
+    do_in_jni_thread(Bind(
+        [](bool start) {
+          if (!start) {
+            do_in_bta_thread(FROM_HERE,
+                             Bind(&BTA_DmBleObserve, false, 0, nullptr));
+            return;
+          }
+
+          btif_gattc_init_dev_cb();
+          do_in_bta_thread(FROM_HERE,
+                           Bind(&BTA_DmBleObserve, true, 0,
+                                (tBTA_DM_SEARCH_CBACK*)bta_scan_results_cb));
+        },
+        start));
   }
 
   void ScanFilterParamSetup(