Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 95ebcf9a authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add synchronization when EventFlag is created in sensors HAL init" into main

parents 5c79dab7 3c4c09d6
Loading
Loading
Loading
Loading
+19 −13
Original line number Diff line number Diff line
@@ -82,6 +82,7 @@ ScopedAStatus Sensors::initialize(
        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,8 +102,12 @@ ScopedAStatus Sensors::initialize(
    // Save a reference to the callback
    mCallback = in_sensorsCallback;

    {
        // Hold the lock to ensure that re-creation of event flag is atomic
        std::lock_guard<std::mutex> lock(mWriteLock);

        // Ensure that any existing EventFlag is properly deleted
    deleteEventFlag();
        deleteEventFlagLocked();

        // Create the EventFlag that is used to signal to the framework that sensor events have been
        // written to the Event FMQ
@@ -118,6 +123,7 @@ ScopedAStatus Sensors::initialize(
        if (!mCallback || !mEventQueue || !mWakeLockQueue || mEventQueueFlag == nullptr) {
            result = ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
        }
    }

    // Start the thread to read events from the Wake Lock FMQ
    mReadWakeLockQueueRun = true;
+8 −1
Original line number Diff line number Diff line
@@ -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 @@ class Sensors : public BnSensors, public ISensorsEventCallback {
    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 @@ class Sensors : public BnSensors, public ISensorsEventCallback {
    // 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.