diff options
author | Yuchen Liu <yucliu@google.com> | 2022-03-25 18:54:43 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2022-03-25 18:54:43 +0000 |
commit | 2f85bfbb449739d7861121765a34eb0c1b7cb10c (patch) | |
tree | 85092db897dc541a6ad6923da238462362899121 /audio_proxy | |
parent | 5dd30deaae4457f42a60252d4ca6f9da78bc5833 (diff) | |
parent | 22e809b8a0fa9d0cda4fa8dbf2135f2a39594492 (diff) | |
download | atv-2f85bfbb449739d7861121765a34eb0c1b7cb10c.tar.gz |
Merge "[AudioProxy] Do not use readBlocking in WriteThread" into tm-dev
Diffstat (limited to 'audio_proxy')
-rw-r--r-- | audio_proxy/service/RemoteBusOutputStream.cpp | 3 | ||||
-rw-r--r-- | audio_proxy/service/WriteThread.cpp | 23 |
2 files changed, 15 insertions, 11 deletions
diff --git a/audio_proxy/service/RemoteBusOutputStream.cpp b/audio_proxy/service/RemoteBusOutputStream.cpp index 3b27b79..a5edc76 100644 --- a/audio_proxy/service/RemoteBusOutputStream.cpp +++ b/audio_proxy/service/RemoteBusOutputStream.cpp @@ -103,6 +103,9 @@ AidlWriteStatus RemoteBusOutputStream::writeRingBuffer(const uint8_t* firstMem, // readNotification is used to "wake" after successful read, hence we don't // need it. writeNotification is used to "wait" for the other end to write // enough data. + // It's fine to use readBlocking here because: + // 1. We don't wake without writing mStatusMQ. + // 2. The other end will always write mStatusMQ before wake mEventFlag. if (!mStatusMQ->readBlocking( &status, 1 /* count */, 0 /* readNotification */, static_cast<uint32_t>( diff --git a/audio_proxy/service/WriteThread.cpp b/audio_proxy/service/WriteThread.cpp index 24a6b66..0e715cb 100644 --- a/audio_proxy/service/WriteThread.cpp +++ b/audio_proxy/service/WriteThread.cpp @@ -23,10 +23,6 @@ #include "BusOutputStream.h" namespace audio_proxy::service { -namespace { -// Time out for FMQ read in ns -- 1s. -constexpr int64_t kFmqReadTimeoutNs = 1'000'000'000; -} // namespace WriteThread::WriteThread(std::shared_ptr<BusOutputStream> stream, CommandMQ* commandMQ, DataMQ* dataMQ, @@ -140,14 +136,19 @@ bool WriteThread::threadLoop() { stream = mStream; } + // Read command. Don't use readBlocking, because readBlocking will block + // when there's no data. When stopping the thread, there's a chance that we + // only wake the mEventFlag without writing any data to FMQ. In this case, + // readBlocking will block until timeout. IStreamOut::WriteCommand replyTo; - if (!mCommandMQ->readBlocking( - &replyTo, 1 /* count */, 0 /* readNoticication */, - static_cast<uint32_t>( - MessageQueueFlagBits::NOT_EMPTY) /* writeNotification */, - kFmqReadTimeoutNs, mEventFlag)) { - LOG(ERROR) << "read command timeout"; - continue; + uint32_t efState = 0; + mEventFlag->wait(static_cast<uint32_t>(MessageQueueFlagBits::NOT_EMPTY), + &efState); + if (!(efState & static_cast<uint32_t>(MessageQueueFlagBits::NOT_EMPTY))) { + continue; // Nothing to do. + } + if (!mCommandMQ->read(&replyTo)) { + continue; // Nothing to do. } if (replyTo == IStreamOut::WriteCommand::WRITE) { |