MelReporter, PatchCommandThread: Update to audio_utils mutex
Test: atest AudioTrackTest AudioRecordTest
Test: atest AAudioTests AudioTrackOffloadTest
Test: atest AudioPlaybackCaptureTest
Test: Camera YouTube
Bug: 298534151
Change-Id: I3dc0999d66cc46db32e251ac879f92e36a93e7fc
diff --git a/services/audioflinger/MelReporter.cpp b/services/audioflinger/MelReporter.cpp
index 5dc2e8a..d16fdd2 100644
--- a/services/audioflinger/MelReporter.cpp
+++ b/services/audioflinger/MelReporter.cpp
@@ -62,7 +62,7 @@
void MelReporter::activateInternalSoundDoseComputation() {
{
- std::lock_guard _l(mLock);
+ audio_utils::lock_guard _l(mutex());
if (!mUseHalSoundDoseInterface) {
// no need to start internal MEL on active patches
return;
@@ -87,8 +87,8 @@
return;
}
- std::lock_guard _laf(mAfMelReporterCallback->mutex());
- std::lock_guard _l(mLock);
+ audio_utils::lock_guard _laf(mAfMelReporterCallback->mutex());
+ audio_utils::lock_guard _l(mutex());
auto activeMelPatchId = activePatchStreamHandle_l(streamHandle);
if (!activeMelPatchId) {
ALOGV("%s stream handle %d does not have an active patch", __func__, streamHandle);
@@ -154,8 +154,8 @@
}
if (!newPatch.deviceStates.empty() && newPatch.csdActive) {
- std::lock_guard _afl(mAfMelReporterCallback->mutex());
- std::lock_guard _l(mLock);
+ audio_utils::lock_guard _afl(mAfMelReporterCallback->mutex());
+ audio_utils::lock_guard _l(mutex());
ALOGV("%s add patch handle %d to active devices", __func__, handle);
startMelComputationForActivePatch_l(newPatch);
mActiveMelPatches[handle] = newPatch;
@@ -192,8 +192,8 @@
void MelReporter::startMelComputationForDeviceId(audio_port_handle_t deviceId) {
ALOGV("%s(%d)", __func__, deviceId);
- std::lock_guard _laf(mAfMelReporterCallback->mutex());
- std::lock_guard _l(mLock);
+ audio_utils::lock_guard _laf(mAfMelReporterCallback->mutex());
+ audio_utils::lock_guard _l(mutex());
for (auto& activeMelPatch : mActiveMelPatches) {
bool csdActive = false;
@@ -218,7 +218,7 @@
ActiveMelPatch melPatch;
{
- std::lock_guard _l(mLock);
+ audio_utils::lock_guard _l(mutex());
auto patchIt = mActiveMelPatches.find(handle);
if (patchIt == mActiveMelPatches.end()) {
@@ -231,8 +231,8 @@
mActiveMelPatches.erase(patchIt);
}
- std::lock_guard _afl(mAfMelReporterCallback->mutex());
- std::lock_guard _l(mLock);
+ audio_utils::lock_guard _afl(mAfMelReporterCallback->mutex());
+ audio_utils::lock_guard _l(mutex());
if (melPatch.csdActive) {
// only need to stop if patch was active
melPatch.csdActive = false;
@@ -254,7 +254,7 @@
void MelReporter::stopInternalMelComputation() {
ALOGV("%s", __func__);
- std::lock_guard _l(mLock);
+ audio_utils::lock_guard _l(mutex());
if (mUseHalSoundDoseInterface) {
return;
}
@@ -287,8 +287,8 @@
void MelReporter::stopMelComputationForDeviceId(audio_port_handle_t deviceId) {
ALOGV("%s(%d)", __func__, deviceId);
- std::lock_guard _laf(mAfMelReporterCallback->mutex());
- std::lock_guard _l(mLock);
+ audio_utils::lock_guard _laf(mAfMelReporterCallback->mutex());
+ audio_utils::lock_guard _l(mutex());
for (auto& activeMelPatch : mActiveMelPatches) {
bool csdActive = false;
@@ -322,7 +322,7 @@
}
std::string MelReporter::dump() {
- std::lock_guard _l(mLock);
+ audio_utils::lock_guard _l(mutex());
std::string output("\nSound Dose:\n");
output.append(mSoundDoseManager->dump());
return output;
diff --git a/services/audioflinger/MelReporter.h b/services/audioflinger/MelReporter.h
index d851e8e..90041df 100644
--- a/services/audioflinger/MelReporter.h
+++ b/services/audioflinger/MelReporter.h
@@ -23,7 +23,6 @@
#include <audio_utils/mutex.h>
#include <sounddose/SoundDoseManager.h>
-#include <mutex>
#include <unordered_map>
namespace android {
@@ -109,30 +108,32 @@
};
void stopInternalMelComputation();
+ audio_utils::mutex& mutex() const { return mMutex; }
- /** Should be called with the following order of locks: mAudioFlinger.mLock -> mLock. */
- void stopMelComputationForPatch_l(const ActiveMelPatch& patch) REQUIRES(mLock);
+ /** Should be called with the following order of locks: mAudioFlinger.mutex() -> mutex(). */
+ void stopMelComputationForPatch_l(const ActiveMelPatch& patch) REQUIRES(mutex());
- /** Should be called with the following order of locks: mAudioFlinger.mLock -> mLock. */
- void startMelComputationForActivePatch_l(const ActiveMelPatch& patch) REQUIRES(mLock);
+ /** Should be called with the following order of locks: mAudioFlinger.mutex() -> mutex(). */
+ void startMelComputationForActivePatch_l(const ActiveMelPatch& patch) REQUIRES(mutex());
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;
- sp<SoundDoseManager> mSoundDoseManager;
+ /* const */ sp<SoundDoseManager> mSoundDoseManager; // set onFirstRef
/**
* 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;
- std::unordered_map<audio_patch_handle_t, ActiveMelPatch> mActiveMelPatches GUARDED_BY(mLock);
- std::unordered_map<audio_port_handle_t, int> mActiveDevices GUARDED_BY(mLock);
- bool mUseHalSoundDoseInterface GUARDED_BY(mLock) = false;
+ mutable audio_utils::mutex mMutex;
+ std::unordered_map<audio_patch_handle_t, ActiveMelPatch> mActiveMelPatches
+ GUARDED_BY(mutex());
+ std::unordered_map<audio_port_handle_t, int> mActiveDevices GUARDED_BY(mutex());
+ bool mUseHalSoundDoseInterface GUARDED_BY(mutex()) = false;
};
} // namespace android
diff --git a/services/audioflinger/PatchCommandThread.cpp b/services/audioflinger/PatchCommandThread.cpp
index 8d5eb9f..2cfefa0 100644
--- a/services/audioflinger/PatchCommandThread.cpp
+++ b/services/audioflinger/PatchCommandThread.cpp
@@ -29,7 +29,7 @@
PatchCommandThread::~PatchCommandThread() {
exit();
- std::lock_guard _l(mLock);
+ audio_utils::lock_guard _l(mutex());
mCommands.clear();
}
@@ -39,7 +39,7 @@
void PatchCommandThread::addListener(const sp<PatchCommandListener>& listener) {
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);
}
@@ -69,9 +69,8 @@
}
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 (!mCommands.empty() && !exitPending()) {
@@ -81,7 +80,7 @@
std::vector<wp<PatchCommandListener>> listenersCopy;
{
- std::lock_guard _ll(mListenerLock);
+ audio_utils::lock_guard _ll(listenerMutex());
listenersCopy = mListeners;
}
@@ -147,7 +146,7 @@
}
void PatchCommandThread::sendCommand(const sp<Command>& command) {
- std::lock_guard _l(mLock);
+ audio_utils::lock_guard _l(mutex());
mCommands.emplace_back(command);
mWaitWorkCV.notify_one();
}
@@ -183,7 +182,7 @@
void PatchCommandThread::exit() {
ALOGV("%s", __func__);
{
- std::lock_guard _l(mLock);
+ audio_utils::lock_guard _l(mutex());
requestExit();
mWaitWorkCV.notify_one();
}
diff --git a/services/audioflinger/PatchCommandThread.h b/services/audioflinger/PatchCommandThread.h
index 66018d7..5ee0f4a 100644
--- a/services/audioflinger/PatchCommandThread.h
+++ b/services/audioflinger/PatchCommandThread.h
@@ -121,13 +121,16 @@
void sendCommand(const sp<Command>& command);
- std::string mThreadName;
- std::mutex mLock;
- std::condition_variable mWaitWorkCV;
- std::deque<sp<Command>> mCommands GUARDED_BY(mLock); // list of pending commands
+ audio_utils::mutex& mutex() const { return mMutex; }
+ audio_utils::mutex& listenerMutex() const { return mListenerMutex; }
- std::mutex mListenerLock;
- std::vector<wp<PatchCommandListener>> mListeners GUARDED_BY(mListenerLock);
+ std::string mThreadName;
+ mutable audio_utils::mutex mMutex;
+ audio_utils::condition_variable mWaitWorkCV;
+ std::deque<sp<Command>> mCommands GUARDED_BY(mutex()); // list of pending commands
+
+ mutable audio_utils::mutex mListenerMutex;
+ std::vector<wp<PatchCommandListener>> mListeners GUARDED_BY(listenerMutex());
};
} // namespace android
diff --git a/services/audioflinger/PlaybackTracks.h b/services/audioflinger/PlaybackTracks.h
index 3e5501f..6462a12 100644
--- a/services/audioflinger/PlaybackTracks.h
+++ b/services/audioflinger/PlaybackTracks.h
@@ -20,6 +20,7 @@
#include "TrackBase.h"
#include <android/os/BnExternalVibrationController.h>
+#include <audio_utils/mutex.h>
#include <audio_utils/LinearMap.h>
#include <binder/AppOpsManager.h>
@@ -468,7 +469,7 @@
*/
SourceMetadatas mTrackMetadatas;
/** Protects mTrackMetadatas against concurrent access. */
- mutable std::mutex mTrackMetadatasMutex;
+ mutable audio_utils::mutex mTrackMetadatasMutex;
}; // end of OutputTrack
// playback track, used by PatchPanel
diff --git a/services/audioflinger/RecordTracks.h b/services/audioflinger/RecordTracks.h
index 021add4..8d3de38 100644
--- a/services/audioflinger/RecordTracks.h
+++ b/services/audioflinger/RecordTracks.h
@@ -20,6 +20,7 @@
#include "TrackBase.h"
#include <android/content/AttributionSourceState.h>
+#include <audio_utils/mutex.h>
#include <datapath/AudioStreamIn.h> // struct Source
namespace android {
@@ -214,15 +215,16 @@
};
sp<StreamInHalInterface> obtainStream(sp<IAfThreadBase>* thread);
+ audio_utils::mutex& readMutex() const { return mReadMutex; }
PatchRecordAudioBufferProvider mPatchRecordAudioBufferProvider;
std::unique_ptr<void, decltype(free)*> mSinkBuffer; // frame size aligned continuous buffer
std::unique_ptr<void, decltype(free)*> mStubBuffer; // buffer used for AudioBufferProvider
size_t mUnconsumedFrames = 0;
- std::mutex mReadLock;
- std::condition_variable mReadCV;
- size_t mReadBytes = 0; // GUARDED_BY(mReadLock)
- status_t mReadError = NO_ERROR; // GUARDED_BY(mReadLock)
+ mutable audio_utils::mutex mReadMutex;
+ audio_utils::condition_variable mReadCV;
+ size_t mReadBytes = 0; // GUARDED_BY(readMutex())
+ status_t mReadError = NO_ERROR; // GUARDED_BY(readMutex())
int64_t mLastReadFrames = 0; // accessed on RecordThread only
};
diff --git a/services/audioflinger/Tracks.cpp b/services/audioflinger/Tracks.cpp
index d48bcef..8654f06 100644
--- a/services/audioflinger/Tracks.cpp
+++ b/services/audioflinger/Tracks.cpp
@@ -2349,13 +2349,13 @@
void OutputTrack::copyMetadataTo(MetadataInserter& backInserter) const
{
- std::lock_guard<std::mutex> lock(mTrackMetadatasMutex);
+ audio_utils::lock_guard lock(mTrackMetadatasMutex);
backInserter = std::copy(mTrackMetadatas.begin(), mTrackMetadatas.end(), backInserter);
}
void OutputTrack::setMetadatas(const SourceMetadatas& metadatas) {
{
- std::lock_guard<std::mutex> lock(mTrackMetadatasMutex);
+ audio_utils::lock_guard lock(mTrackMetadatasMutex);
mTrackMetadatas = metadatas;
}
// No need to adjust metadata track volumes as OutputTrack volumes are always 0dBFS.
@@ -3303,7 +3303,7 @@
}
{
- std::lock_guard<std::mutex> lock(mReadLock);
+ audio_utils::lock_guard lock(readMutex());
mReadBytes += bytesRead;
mReadError = NO_ERROR;
}
@@ -3330,7 +3330,7 @@
stream_error:
stream->standby();
{
- std::lock_guard<std::mutex> lock(mReadLock);
+ audio_utils::lock_guard lock(readMutex());
mReadError = result;
}
mReadCV.notify_one();
@@ -3359,7 +3359,7 @@
{
bytes = std::min(bytes, mFrameCount * mFrameSize);
{
- std::unique_lock<std::mutex> lock(mReadLock);
+ audio_utils::unique_lock lock(readMutex());
mReadCV.wait(lock, [&]{ return mReadError != NO_ERROR || mReadBytes != 0; });
if (mReadError != NO_ERROR) {
mLastReadFrames = 0;