Fix race condition resulting in unbalanced wakelock calls
Because we release the lock during threadLoop(), we might actually
make the queue empty more than once during a cycle, but only release
the wakelock once in the end.
This change makes sure we release once for every time we make the
queue empty, just like we acquire once for every time we make it non-
empty.
Test: No good way to repro. Will submit and wait for the power monitor
results.
Bug: 228012293
Change-Id: I740b438dbddb6aace41263427bde95f902ed25a0
diff --git a/services/audiopolicy/service/AudioPolicyService.cpp b/services/audiopolicy/service/AudioPolicyService.cpp
index da0a4a9..18339b0 100644
--- a/services/audiopolicy/service/AudioPolicyService.cpp
+++ b/services/audiopolicy/service/AudioPolicyService.cpp
@@ -1938,12 +1938,16 @@
while (!exitPending())
{
sp<AudioPolicyService> svc;
+ int numTimesBecameEmpty = 0;
while (!mAudioCommands.isEmpty() && !exitPending()) {
nsecs_t curTime = systemTime();
// commands are sorted by increasing time stamp: execute them from index 0 and up
if (mAudioCommands[0]->mTime <= curTime) {
sp<AudioCommand> command = mAudioCommands[0];
mAudioCommands.removeAt(0);
+ if (mAudioCommands.isEmpty()) {
+ ++numTimesBecameEmpty;
+ }
mLastCommand = command;
switch (command->mCommand) {
@@ -2180,8 +2184,9 @@
}
}
- // release delayed commands wake lock if the queue is empty
- if (mAudioCommands.isEmpty()) {
+ // release delayed commands wake lock as many times as we made the queue is
+ // empty during popping.
+ while (numTimesBecameEmpty--) {
release_wake_lock(mName.string());
}