OSDN Git Service

Fix race condition for SensorManager looper.
authorYifan Hong <elsk@google.com>
Wed, 14 Jun 2017 01:04:25 +0000 (18:04 -0700)
committerYifan Hong <elsk@google.com>
Wed, 14 Jun 2017 01:56:07 +0000 (18:56 -0700)
Before the fix there could be two background threads started for two
loopers. Add a guard to prevent this from happening.

Test: pass
Bug: 62404135
Change-Id: I8230addaaa6b83670d7b83c96809b00fca95d352

services/sensorservice/hidl/SensorManager.cpp

index 004a5c5..25a3dc5 100644 (file)
@@ -143,6 +143,10 @@ sp<::android::Looper> SensorManager::getLooper() {
             }
 
             std::unique_lock<std::mutex> lock(mutex);
+            if (looper != nullptr) {
+                LOG(INFO) << "Another thread has already set the looper, exiting this one.";
+                return;
+            }
             looper = Looper::prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS /* opts */);
             lock.unlock();