summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-11-07 08:30:26 +1100
committerjacqueline <me@jacqueline.id.au>2023-11-07 08:30:26 +1100
commitd36fe9be6b522a3dade389213a0bb7e26a169627 (patch)
tree45f143981af1d6ae2717785ed316cd54a94a3f29 /src
parent05ce360c9928df9387a65f5e1840bf3f67231cea (diff)
downloadtangara-fw-d36fe9be6b522a3dade389213a0bb7e26a169627.tar.gz
Use atomics for new file notification instead of a semaphore
Diffstat (limited to 'src')
-rw-r--r--src/audio/fatfs_audio_input.cpp28
-rw-r--r--src/audio/include/fatfs_audio_input.hpp2
2 files changed, 16 insertions, 14 deletions
diff --git a/src/audio/fatfs_audio_input.cpp b/src/audio/fatfs_audio_input.cpp
index 4c5e981b..b919a3a8 100644
--- a/src/audio/fatfs_audio_input.cpp
+++ b/src/audio/fatfs_audio_input.cpp
@@ -47,12 +47,10 @@ FatfsAudioInput::FatfsAudioInput(database::ITagParser& tag_parser)
tag_parser_(tag_parser),
new_stream_mutex_(),
new_stream_(),
- has_new_stream_(xSemaphoreCreateBinary()),
+ has_new_stream_(false),
pending_path_() {}
-FatfsAudioInput::~FatfsAudioInput() {
- vSemaphoreDelete(has_new_stream_);
-}
+FatfsAudioInput::~FatfsAudioInput() {}
auto FatfsAudioInput::SetPath(std::future<std::optional<std::pmr::string>> fut)
-> void {
@@ -61,36 +59,40 @@ auto FatfsAudioInput::SetPath(std::future<std::optional<std::pmr::string>> fut)
new database::FutureFetcher<std::optional<std::pmr::string>>(
std::move(fut)));
- xSemaphoreGive(has_new_stream_);
+ has_new_stream_ = true;
+ has_new_stream_.notify_one();
}
auto FatfsAudioInput::SetPath(const std::pmr::string& path) -> void {
std::lock_guard<std::mutex> guard{new_stream_mutex_};
if (OpenFile(path)) {
- xSemaphoreGive(has_new_stream_);
+ has_new_stream_ = true;
+ has_new_stream_.notify_one();
}
}
auto FatfsAudioInput::SetPath() -> void {
std::lock_guard<std::mutex> guard{new_stream_mutex_};
new_stream_.reset();
- xSemaphoreGive(has_new_stream_);
+ has_new_stream_ = true;
+ has_new_stream_.notify_one();
}
auto FatfsAudioInput::HasNewStream() -> bool {
- bool res = xSemaphoreTake(has_new_stream_, 0);
- if (res) {
- xSemaphoreGive(has_new_stream_);
- }
- return res;
+ return has_new_stream_;
}
auto FatfsAudioInput::NextStream() -> std::shared_ptr<codecs::IStream> {
while (true) {
- xSemaphoreTake(has_new_stream_, portMAX_DELAY);
+ has_new_stream_.wait(false);
{
std::lock_guard<std::mutex> guard{new_stream_mutex_};
+ if (!has_new_stream_.exchange(false)) {
+ // If the new stream went away, then we need to go back to waiting.
+ continue;
+ }
+
// If the path is a future, then wait for it to complete.
if (pending_path_) {
auto res = pending_path_->Result();
diff --git a/src/audio/include/fatfs_audio_input.hpp b/src/audio/include/fatfs_audio_input.hpp
index b7b1d18e..08527350 100644
--- a/src/audio/include/fatfs_audio_input.hpp
+++ b/src/audio/include/fatfs_audio_input.hpp
@@ -58,7 +58,7 @@ class FatfsAudioInput : public IAudioSource {
std::mutex new_stream_mutex_;
std::shared_ptr<codecs::IStream> new_stream_;
- SemaphoreHandle_t has_new_stream_;
+ std::atomic<bool> has_new_stream_;
std::unique_ptr<database::FutureFetcher<std::optional<std::pmr::string>>>
pending_path_;