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

Commit 9004695e authored by Andy Hung's avatar Andy Hung Committed by Automerger Merge Worker
Browse files

Merge "MelReporter, PatchCommandThread: Update to audio_utils mutex" into...

Merge "MelReporter, PatchCommandThread: Update to audio_utils mutex" into udc-dev-plus-aosp am: f6b4812d

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/av/+/24830500



Change-Id: Ib0dd388ed902cb753435cc614ade644556010118
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 05101a67 f6b4812d
Loading
Loading
Loading
Loading
+10 −10
Original line number Original line Diff line number Diff line
@@ -65,7 +65,7 @@ bool MelReporter::activateHalSoundDoseComputation(const std::string& module,


void MelReporter::activateInternalSoundDoseComputation() {
void MelReporter::activateInternalSoundDoseComputation() {
    {
    {
        std::lock_guard _l(mLock);
        audio_utils::lock_guard _l(mutex());
        if (!mUseHalSoundDoseInterface) {
        if (!mUseHalSoundDoseInterface) {
            // no need to start internal MEL on active patches
            // no need to start internal MEL on active patches
            return;
            return;
@@ -111,8 +111,8 @@ void MelReporter::updateMetadataForCsd(audio_io_handle_t streamHandle,
        return;
        return;
    }
    }


    std::lock_guard _laf(mAfMelReporterCallback->mutex());
    audio_utils::lock_guard _laf(mAfMelReporterCallback->mutex());
    std::lock_guard _l(mLock);
    audio_utils::lock_guard _l(mutex());
    auto activeMelPatchId = activePatchStreamHandle_l(streamHandle);
    auto activeMelPatchId = activePatchStreamHandle_l(streamHandle);
    if (!activeMelPatchId) {
    if (!activeMelPatchId) {
        ALOGV("%s stream handle %d does not have an active patch", __func__, streamHandle);
        ALOGV("%s stream handle %d does not have an active patch", __func__, streamHandle);
@@ -171,8 +171,8 @@ void MelReporter::onCreateAudioPatch(audio_patch_handle_t handle,
    }
    }


    if (!newPatch.deviceHandles.empty()) {
    if (!newPatch.deviceHandles.empty()) {
        std::lock_guard _afl(mAfMelReporterCallback->mutex());
        audio_utils::lock_guard _afl(mAfMelReporterCallback->mutex());
        std::lock_guard _l(mLock);
        audio_utils::lock_guard _l(mutex());
        ALOGV("%s add patch handle %d to active devices", __func__, handle);
        ALOGV("%s add patch handle %d to active devices", __func__, handle);
        startMelComputationForActivePatch_l(newPatch);
        startMelComputationForActivePatch_l(newPatch);
        newPatch.csdActive = true;
        newPatch.csdActive = true;
@@ -213,7 +213,7 @@ void MelReporter::onReleaseAudioPatch(audio_patch_handle_t handle) {


    ActiveMelPatch melPatch;
    ActiveMelPatch melPatch;
    {
    {
        std::lock_guard _l(mLock);
        audio_utils::lock_guard _l(mutex());


        auto patchIt = mActiveMelPatches.find(handle);
        auto patchIt = mActiveMelPatches.find(handle);
        if (patchIt == mActiveMelPatches.end()) {
        if (patchIt == mActiveMelPatches.end()) {
@@ -226,8 +226,8 @@ void MelReporter::onReleaseAudioPatch(audio_patch_handle_t handle) {
        mActiveMelPatches.erase(patchIt);
        mActiveMelPatches.erase(patchIt);
    }
    }


    std::lock_guard _afl(mAfMelReporterCallback->mutex());
    audio_utils::lock_guard _afl(mAfMelReporterCallback->mutex());
    std::lock_guard _l(mLock);
    audio_utils::lock_guard _l(mutex());
    stopMelComputationForPatch_l(melPatch);
    stopMelComputationForPatch_l(melPatch);
}
}


@@ -239,7 +239,7 @@ sp<media::ISoundDose> MelReporter::getSoundDoseInterface(


void MelReporter::stopInternalMelComputation() {
void MelReporter::stopInternalMelComputation() {
    ALOGV("%s", __func__);
    ALOGV("%s", __func__);
    std::lock_guard _l(mLock);
    audio_utils::lock_guard _l(mutex());
    mActiveMelPatches.clear();
    mActiveMelPatches.clear();
    mUseHalSoundDoseInterface = true;
    mUseHalSoundDoseInterface = true;
}
}
@@ -286,7 +286,7 @@ bool MelReporter::useHalSoundDoseInterface_l() {
}
}


std::string MelReporter::dump() {
std::string MelReporter::dump() {
    std::lock_guard _l(mLock);
    audio_utils::lock_guard _l(mutex());
    std::string output("\nSound Dose:\n");
    std::string output("\nSound Dose:\n");
    output.append(mSoundDoseManager->dump());
    output.append(mSoundDoseManager->dump());
    return output;
    return output;
+14 −13
Original line number Original line Diff line number Diff line
@@ -23,7 +23,6 @@
#include <audio_utils/mutex.h>
#include <audio_utils/mutex.h>
#include <sounddose/SoundDoseManager.h>
#include <sounddose/SoundDoseManager.h>


#include <mutex>
#include <unordered_map>
#include <unordered_map>


namespace android {
namespace android {
@@ -100,30 +99,32 @@ private:
    bool shouldComputeMelForDeviceType(audio_devices_t device);
    bool shouldComputeMelForDeviceType(audio_devices_t device);


    void stopInternalMelComputation();
    void stopInternalMelComputation();
    audio_utils::mutex& mutex() const { return mMutex; }


    /** Should be called with the following order of locks: mAudioFlinger.mLock -> mLock. */
    /** Should be called with the following order of locks: mAudioFlinger.mutex() -> mutex(). */
    void stopMelComputationForPatch_l(const ActiveMelPatch& patch) REQUIRES(mLock);
    void stopMelComputationForPatch_l(const ActiveMelPatch& patch) REQUIRES(mutex());


    /** Should be called with the following order of locks: mAudioFlinger.mLock -> mLock. */
    /** Should be called with the following order of locks: mAudioFlinger.mutex() -> mutex(). */
    void startMelComputationForActivePatch_l(const ActiveMelPatch& patch) REQUIRES(mLock);
    void startMelComputationForActivePatch_l(const ActiveMelPatch& patch) REQUIRES(mutex());


    std::optional<audio_patch_handle_t>
    std::optional<audio_patch_handle_t>
    activePatchStreamHandle_l(audio_io_handle_t streamHandle) REQUIRES(mLock);
    activePatchStreamHandle_l(audio_io_handle_t streamHandle) REQUIRES(mutex());


    bool useHalSoundDoseInterface_l() REQUIRES(mLock);
    bool useHalSoundDoseInterface_l() REQUIRES(mutex());


    const sp<IAfMelReporterCallback> mAfMelReporterCallback;
    const sp<IAfMelReporterCallback> mAfMelReporterCallback;


    sp<SoundDoseManager> mSoundDoseManager;
    /* const */ sp<SoundDoseManager> mSoundDoseManager;  // set onFirstRef


    /**
    /**
     * Lock for protecting the active mel patches. Do not mix with the AudioFlinger lock.
     * Lock for protecting the active mel patches. Do not mix with the AudioFlinger lock.
     * Locking order AudioFlinger::mLock -> PatchCommandThread::mLock -> MelReporter::mLock.
     * Locking order AudioFlinger::mutex() -> PatchCommandThread::mutex() -> MelReporter::mutex().
     */
     */
    std::mutex mLock;
    mutable audio_utils::mutex mMutex;
    std::unordered_map<audio_patch_handle_t, ActiveMelPatch> mActiveMelPatches GUARDED_BY(mLock);
    std::unordered_map<audio_patch_handle_t, ActiveMelPatch> mActiveMelPatches
    std::unordered_map<audio_port_handle_t, int> mActiveDevices GUARDED_BY(mLock);
            GUARDED_BY(mutex());
    bool mUseHalSoundDoseInterface GUARDED_BY(mLock) = false;
    std::unordered_map<audio_port_handle_t, int> mActiveDevices GUARDED_BY(mutex());
    bool mUseHalSoundDoseInterface GUARDED_BY(mutex()) = false;
};
};


}  // namespace android
}  // namespace android
+6 −7
Original line number Original line Diff line number Diff line
@@ -29,7 +29,7 @@ constexpr char kPatchCommandThreadName[] = "AudioFlinger_PatchCommandThread";
PatchCommandThread::~PatchCommandThread() {
PatchCommandThread::~PatchCommandThread() {
    exit();
    exit();


    std::lock_guard _l(mLock);
    audio_utils::lock_guard _l(mutex());
    mCommands.clear();
    mCommands.clear();
}
}


@@ -39,7 +39,7 @@ void PatchCommandThread::onFirstRef() {


void PatchCommandThread::addListener(const sp<PatchCommandListener>& listener) {
void PatchCommandThread::addListener(const sp<PatchCommandListener>& listener) {
    ALOGV("%s add listener %p", __func__, static_cast<void*>(listener.get()));
    ALOGV("%s add listener %p", __func__, static_cast<void*>(listener.get()));
    std::lock_guard _l(mListenerLock);
    audio_utils::lock_guard _l(listenerMutex());
    mListeners.emplace_back(listener);
    mListeners.emplace_back(listener);
}
}


@@ -59,9 +59,8 @@ void PatchCommandThread::releaseAudioPatch(audio_patch_handle_t handle) {
}
}


bool PatchCommandThread::threadLoop()
bool PatchCommandThread::threadLoop()
NO_THREAD_SAFETY_ANALYSIS  // bug in clang compiler.
{
{
    std::unique_lock _l(mLock);
    audio_utils::unique_lock _l(mutex());


    while (!exitPending()) {
    while (!exitPending()) {
        while (!mCommands.empty() && !exitPending()) {
        while (!mCommands.empty() && !exitPending()) {
@@ -71,7 +70,7 @@ NO_THREAD_SAFETY_ANALYSIS // bug in clang compiler.


            std::vector<wp<PatchCommandListener>> listenersCopy;
            std::vector<wp<PatchCommandListener>> listenersCopy;
            {
            {
                std::lock_guard _ll(mListenerLock);
                audio_utils::lock_guard _ll(listenerMutex());
                listenersCopy = mListeners;
                listenersCopy = mListeners;
            }
            }


@@ -122,7 +121,7 @@ NO_THREAD_SAFETY_ANALYSIS // bug in clang compiler.
}
}


void PatchCommandThread::sendCommand(const sp<Command>& command) {
void PatchCommandThread::sendCommand(const sp<Command>& command) {
    std::lock_guard _l(mLock);
    audio_utils::lock_guard _l(mutex());
    mCommands.emplace_back(command);
    mCommands.emplace_back(command);
    mWaitWorkCV.notify_one();
    mWaitWorkCV.notify_one();
}
}
@@ -148,7 +147,7 @@ void PatchCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle) {
void PatchCommandThread::exit() {
void PatchCommandThread::exit() {
    ALOGV("%s", __func__);
    ALOGV("%s", __func__);
    {
    {
        std::lock_guard _l(mLock);
        audio_utils::lock_guard _l(mutex());
        requestExit();
        requestExit();
        mWaitWorkCV.notify_one();
        mWaitWorkCV.notify_one();
    }
    }
+8 −5
Original line number Original line Diff line number Diff line
@@ -100,13 +100,16 @@ private:


    void sendCommand(const sp<Command>& command);
    void sendCommand(const sp<Command>& command);


    audio_utils::mutex& mutex() const { return mMutex; }
    audio_utils::mutex& listenerMutex() const { return mListenerMutex; }

    std::string mThreadName;
    std::string mThreadName;
    std::mutex mLock;
    mutable audio_utils::mutex mMutex;
    std::condition_variable mWaitWorkCV;
    audio_utils::condition_variable mWaitWorkCV;
    std::deque<sp<Command>> mCommands GUARDED_BY(mLock); // list of pending commands
    std::deque<sp<Command>> mCommands GUARDED_BY(mutex()); // list of pending commands


    std::mutex mListenerLock;
    mutable audio_utils::mutex mListenerMutex;
    std::vector<wp<PatchCommandListener>> mListeners GUARDED_BY(mListenerLock);
    std::vector<wp<PatchCommandListener>> mListeners GUARDED_BY(listenerMutex());
};
};


}  // namespace android
}  // namespace android
+2 −1
Original line number Original line Diff line number Diff line
@@ -20,6 +20,7 @@
#include "TrackBase.h"
#include "TrackBase.h"


#include <android/os/BnExternalVibrationController.h>
#include <android/os/BnExternalVibrationController.h>
#include <audio_utils/mutex.h>
#include <audio_utils/LinearMap.h>
#include <audio_utils/LinearMap.h>
#include <binder/AppOpsManager.h>
#include <binder/AppOpsManager.h>


@@ -467,7 +468,7 @@ private:
     */
     */
    SourceMetadatas mTrackMetadatas;
    SourceMetadatas mTrackMetadatas;
    /** Protects mTrackMetadatas against concurrent access. */
    /** Protects mTrackMetadatas against concurrent access. */
    mutable std::mutex mTrackMetadatasMutex;
    mutable audio_utils::mutex mTrackMetadatasMutex;
};  // end of OutputTrack
};  // end of OutputTrack


// playback track, used by PatchPanel
// playback track, used by PatchPanel
Loading