Merge "Add synchronization when EventFlag is created in sensors HAL init" into main
diff --git a/sensors/aidl/default/Sensors.cpp b/sensors/aidl/default/Sensors.cpp
index 65dd304..9e6bea5 100644
--- a/sensors/aidl/default/Sensors.cpp
+++ b/sensors/aidl/default/Sensors.cpp
@@ -82,6 +82,7 @@
         const MQDescriptor<int32_t, SynchronizedReadWrite>& in_wakeLockDescriptor,
         const std::shared_ptr<::aidl::android::hardware::sensors::ISensorsCallback>&
                 in_sensorsCallback) {
+    ALOGI("Sensors initializing");
     ScopedAStatus result = ScopedAStatus::ok();
 
     mEventQueue = std::make_unique<AidlMessageQueue<Event, SynchronizedReadWrite>>(
@@ -101,22 +102,27 @@
     // Save a reference to the callback
     mCallback = in_sensorsCallback;
 
-    // Ensure that any existing EventFlag is properly deleted
-    deleteEventFlag();
+    {
+        // Hold the lock to ensure that re-creation of event flag is atomic
+        std::lock_guard<std::mutex> lock(mWriteLock);
 
-    // Create the EventFlag that is used to signal to the framework that sensor events have been
-    // written to the Event FMQ
-    if (EventFlag::createEventFlag(mEventQueue->getEventFlagWord(), &mEventQueueFlag) != OK) {
-        result = ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
-    }
+        // Ensure that any existing EventFlag is properly deleted
+        deleteEventFlagLocked();
 
-    // Create the Wake Lock FMQ that is used by the framework to communicate whenever WAKE_UP
-    // events have been successfully read and handled by the framework.
-    mWakeLockQueue = std::make_unique<AidlMessageQueue<int32_t, SynchronizedReadWrite>>(
-            in_wakeLockDescriptor, true /* resetPointers */);
+        // Create the EventFlag that is used to signal to the framework that sensor events have been
+        // written to the Event FMQ
+        if (EventFlag::createEventFlag(mEventQueue->getEventFlagWord(), &mEventQueueFlag) != OK) {
+            result = ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
+        }
 
-    if (!mCallback || !mEventQueue || !mWakeLockQueue || mEventQueueFlag == nullptr) {
-        result = ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
+        // Create the Wake Lock FMQ that is used by the framework to communicate whenever WAKE_UP
+        // events have been successfully read and handled by the framework.
+        mWakeLockQueue = std::make_unique<AidlMessageQueue<int32_t, SynchronizedReadWrite>>(
+                in_wakeLockDescriptor, true /* resetPointers */);
+
+        if (!mCallback || !mEventQueue || !mWakeLockQueue || mEventQueueFlag == nullptr) {
+            result = ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
+        }
     }
 
     // Start the thread to read events from the Wake Lock FMQ
diff --git a/sensors/aidl/default/include/sensors-impl/Sensors.h b/sensors/aidl/default/include/sensors-impl/Sensors.h
index c90db69..8336429 100644
--- a/sensors/aidl/default/include/sensors-impl/Sensors.h
+++ b/sensors/aidl/default/include/sensors-impl/Sensors.h
@@ -23,6 +23,8 @@
 #include <map>
 #include "Sensor.h"
 
+#include <android-base/thread_annotations.h>
+
 namespace aidl {
 namespace android {
 namespace hardware {
@@ -125,6 +127,11 @@
     void deleteEventFlag() {
         // Hold the lock to ensure we don't delete the flag while it's being used in postEvents()
         std::lock_guard<std::mutex> lock(mWriteLock);
+        deleteEventFlagLocked();
+    }
+
+    // Expects mWriteLock to be locked prior to invocation
+    void deleteEventFlagLocked() {
         if (mEventQueueFlag != nullptr) {
             status_t status = EventFlag::deleteEventFlag(&mEventQueueFlag);
             if (status != OK) {
@@ -193,7 +200,7 @@
     // The Wake Lock FMQ that is read to determine when the framework has handled WAKE_UP events
     std::unique_ptr<AidlMessageQueue<int32_t, SynchronizedReadWrite>> mWakeLockQueue;
     // Event Flag to signal to the framework when sensor events are available to be read
-    EventFlag* mEventQueueFlag;
+    EventFlag* mEventQueueFlag GUARDED_BY(mWriteLock);
     // Callback for asynchronous events, such as dynamic sensor connections.
     std::shared_ptr<::aidl::android::hardware::sensors::ISensorsCallback> mCallback;
     // A map of the available sensors.