pal: Add sleep during second stage processing.

Add wake/notify during second stage processing when
there is not enough data to avoid unnecessary loop
and log flooding.

Change-Id: I8132ce0df07092eb753d42bc2269b992c14a40df
diff --git a/session/src/SoundTriggerEngineCapi.cpp b/session/src/SoundTriggerEngineCapi.cpp
index a58de5a..48de4b1 100644
--- a/session/src/SoundTriggerEngineCapi.cpp
+++ b/session/src/SoundTriggerEngineCapi.cpp
@@ -28,7 +28,7 @@
  *
  * Changes from Qualcomm Innovation Center are provided under the following license:
  *
- * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
+ * Copyright (c) 2022-2024, Qualcomm Innovation Center, Inc. All rights reserved.
  * SPDX-License-Identifier: BSD-3-Clause-Clear
  */
 
@@ -250,7 +250,7 @@
             }
         }
 
-        if (reader_->getUnreadSize() < buffer_size_)
+        if (!reader_->waitForBuffers(buffer_size_))
             continue;
 
         read_size = reader_->read((void*)process_input_buff, buffer_size_);
@@ -527,7 +527,7 @@
             }
         }
 
-        if (reader_->getUnreadSize() < buffer_size_)
+        if (!reader_->waitForBuffers(buffer_size_))
             continue;
 
         read_size = reader_->read((void*)process_input_buff, buffer_size_);
diff --git a/utils/inc/PalRingBuffer.h b/utils/inc/PalRingBuffer.h
index 79a4710..a166d03 100644
--- a/utils/inc/PalRingBuffer.h
+++ b/utils/inc/PalRingBuffer.h
@@ -28,7 +28,7 @@
  *
  * Changes from Qualcomm Innovation Center are provided under the following license:
  *
- * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
+ * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
  * SPDX-License-Identifier: BSD-3-Clause-Clear
  */
 
@@ -59,6 +59,7 @@
          : ringBuffer_(buffer),
            unreadSize_(0),
            readOffset_(0),
+           requestedSize_(0),
            state_(READER_DISABLED) {}
 
     ~PalRingBufferReader() {};
@@ -70,6 +71,7 @@
     size_t getUnreadSize();
     void reset();
     bool isEnabled() { return state_ == READER_ENABLED; }
+    bool waitForBuffers(uint32_t buffer_size);
 
     friend class PalRingBuffer;
     friend class StreamSoundTrigger;
@@ -79,6 +81,9 @@
     size_t unreadSize_;
     size_t readOffset_;
     pal_ring_buffer_reader_state state_;
+    std::mutex mutex_;
+    std::condition_variable cv_;
+    uint32_t requestedSize_;
 };
 
 class PalRingBuffer {
diff --git a/utils/src/PalRingBuffer.cpp b/utils/src/PalRingBuffer.cpp
index cfe7432..6bd6937 100644
--- a/utils/src/PalRingBuffer.cpp
+++ b/utils/src/PalRingBuffer.cpp
@@ -25,6 +25,11 @@
  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Changes from Qualcomm Innovation Center are provided under the following license:
+ *
+ * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved.
+ * SPDX-License-Identifier: BSD-3-Clause-Clear
  */
 
 
@@ -71,6 +76,11 @@
     for (it = readOffsets_.begin(); it != readOffsets_.end(); it++, i++) {
         (*(it))->unreadSize_ += writtenSize;
         PAL_VERBOSE(LOG_TAG, "Reader (%d), unreadSize(%zu)", i, (*(it))->unreadSize_);
+
+        if ((*(it))->requestedSize_ > 0 &&
+            (*(it))->unreadSize_ >= (*(it))->requestedSize_) {
+             (*(it))->cv_.notify_one();
+        }
     }
 }
 
@@ -148,6 +158,21 @@
     bufferEnd_ = bufferSize;
 }
 
+bool PalRingBufferReader::waitForBuffers(uint32_t buffer_size)
+{
+    std::unique_lock<std::mutex> lck(mutex_);
+    if (state_ == READER_ENABLED) {
+        if (unreadSize_ >= buffer_size)
+            goto exit;
+        requestedSize_ = buffer_size;
+        cv_.wait_for(lck, std::chrono::milliseconds(3000));
+    }
+
+exit:
+    requestedSize_ = 0;
+    return unreadSize_ >= buffer_size;
+}
+
 int32_t PalRingBufferReader::read(void* readBuffer, size_t bufferSize)
 {
     int32_t readSize = 0;
@@ -283,6 +308,8 @@
     unreadSize_ = 0;
     state_ = READER_DISABLED;
     ringBuffer_->mutex_.unlock();
+    requestedSize_ = 0;
+    cv_.notify_all();
 }
 
 PalRingBufferReader* PalRingBuffer::newReader()