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

Commit 54211151 authored by Shunkai Yao's avatar Shunkai Yao Committed by Automerger Merge Worker
Browse files

Merge "Fix race condition in thread creation" am: 7d5fae05

parents d4fe5b98 7d5fae05
Loading
Loading
Loading
Loading
+4 −3
Original line number Original line Diff line number Diff line
@@ -174,7 +174,6 @@ class SensorPoseProviderImpl : public SensorPoseProvider {
    sp<Looper> mLooper;
    sp<Looper> mLooper;
    Listener* const mListener;
    Listener* const mListener;
    SensorManager* const mSensorManager;
    SensorManager* const mSensorManager;
    std::thread mThread;
    std::mutex mMutex;
    std::mutex mMutex;
    std::map<int32_t, SensorEnableGuard> mEnabledSensors;
    std::map<int32_t, SensorEnableGuard> mEnabledSensors;
    std::map<int32_t, SensorExtra> mEnabledSensorsExtra GUARDED_BY(mMutex);
    std::map<int32_t, SensorExtra> mEnabledSensorsExtra GUARDED_BY(mMutex);
@@ -187,11 +186,13 @@ class SensorPoseProviderImpl : public SensorPoseProvider {
    // the worker thread and that thread would notify, via the promise below whenever initialization
    // the worker thread and that thread would notify, via the promise below whenever initialization
    // is finished, and whether it was successful.
    // is finished, and whether it was successful.
    std::promise<bool> mInitPromise;
    std::promise<bool> mInitPromise;
    std::thread mThread;


    SensorPoseProviderImpl(const char* packageName, Listener* listener)
    SensorPoseProviderImpl(const char* packageName, Listener* listener)
        : mListener(listener),
        : mListener(listener),
          mSensorManager(&SensorManager::getInstanceForPackage(String16(packageName))),
          mSensorManager(&SensorManager::getInstanceForPackage(String16(packageName))) {
          mThread([this] { threadFunc(); }) {}
        mThread = std::thread([this] { threadFunc(); });
    }


    void initFinished(bool success) { mInitPromise.set_value(success); }
    void initFinished(bool success) { mInitPromise.set_value(success); }


+4 −1
Original line number Original line Diff line number Diff line
@@ -100,7 +100,10 @@ SpatializerPoseController::SpatializerPoseController(Listener* listener,
              .screenStillnessRotationalThreshold = kScreenStillnessRotationThreshold,
              .screenStillnessRotationalThreshold = kScreenStillnessRotationThreshold,
      })),
      })),
      mPoseProvider(SensorPoseProvider::create("headtracker", this)),
      mPoseProvider(SensorPoseProvider::create("headtracker", this)),
      mThread([this, maxUpdatePeriod] {
      mThread([this, maxUpdatePeriod] { // It's important that mThread is initialized after
                                        // everything else because it runs a member
                                        // function that may use any member
                                        // of this class.
          while (true) {
          while (true) {
              Pose3f headToStage;
              Pose3f headToStage;
              std::optional<HeadTrackingMode> modeIfChanged;
              std::optional<HeadTrackingMode> modeIfChanged;
+4 −1
Original line number Original line Diff line number Diff line
@@ -123,12 +123,15 @@ class SpatializerPoseController : private media::SensorPoseProvider::Listener {
    int32_t mHeadSensor = media::SensorPoseProvider::INVALID_HANDLE;
    int32_t mHeadSensor = media::SensorPoseProvider::INVALID_HANDLE;
    int32_t mScreenSensor = media::SensorPoseProvider::INVALID_HANDLE;
    int32_t mScreenSensor = media::SensorPoseProvider::INVALID_HANDLE;
    std::optional<media::HeadTrackingMode> mActualMode;
    std::optional<media::HeadTrackingMode> mActualMode;
    std::thread mThread;
    std::condition_variable mCondVar;
    std::condition_variable mCondVar;
    bool mShouldCalculate = true;
    bool mShouldCalculate = true;
    bool mShouldExit = false;
    bool mShouldExit = false;
    bool mCalculated = false;
    bool mCalculated = false;


    // It's important that mThread is the last variable in this class
    // since we starts mThread in initializer list
    std::thread mThread;

    void onPose(int64_t timestamp, int32_t sensor, const media::Pose3f& pose,
    void onPose(int64_t timestamp, int32_t sensor, const media::Pose3f& pose,
                const std::optional<media::Twist3f>& twist, bool isNewReference) override;
                const std::optional<media::Twist3f>& twist, bool isNewReference) override;