From 5b452a9d75b24db2dc2f5b2f5b4f31d710384d2a Mon Sep 17 00:00:00 2001 From: Davide Beatrici Date: Sun, 24 Mar 2024 00:15:12 +0100 Subject: [PATCH] OSS: Use std::atomic_flag instead of PauseFlag We couldn't do it before because the functions we need were introduced in C++20. --- src/backends/OSS/CMakeLists.txt | 1 - src/backends/OSS/OSS.cpp | 19 +++++++++++------ src/backends/OSS/OSS.hpp | 3 +-- src/backends/OSS/PauseFlag.hpp | 36 --------------------------------- 4 files changed, 14 insertions(+), 45 deletions(-) delete mode 100644 src/backends/OSS/PauseFlag.hpp diff --git a/src/backends/OSS/CMakeLists.txt b/src/backends/OSS/CMakeLists.txt index ad53981..34b2df0 100644 --- a/src/backends/OSS/CMakeLists.txt +++ b/src/backends/OSS/CMakeLists.txt @@ -41,7 +41,6 @@ target_sources(be_oss "FileDescriptor.hpp" "OSS.cpp" "OSS.hpp" - "PauseFlag.hpp" ) target_link_libraries(be_oss diff --git a/src/backends/OSS/OSS.cpp b/src/backends/OSS/OSS.cpp index 6d84bbf..94657df 100644 --- a/src/backends/OSS/OSS.cpp +++ b/src/backends/OSS/OSS.cpp @@ -367,7 +367,8 @@ ErrorCode Flux::start(FluxConfig &config, const FluxFeedback &feedback) { ErrorCode Flux::stop() { m_halt = true; - m_pause.set(false); + m_pause.clear(); + m_pause.notify_all(); if (m_thread) { m_thread->join(); @@ -380,7 +381,13 @@ ErrorCode Flux::stop() { } ErrorCode Flux::pause(const bool on) { - m_pause.set(on); + if (on) { + m_pause.test_and_set(); + } else { + m_pause.clear(); + } + + m_pause.notify_all(); return CROSSAUDIO_EC_OK; } @@ -409,8 +416,8 @@ void Flux::processInput() { FluxData fluxData = { buffer.data(), static_cast< uint32_t >(bytes / frameSize) }; m_feedback.process(m_feedback.userData, &fluxData); - if (m_pause) { - m_pause.wait(false); + if (m_pause.test()) { + m_pause.wait(true); } } @@ -431,9 +438,9 @@ void Flux::processOutput() { break; } - if (m_pause) { + if (m_pause.test()) { ioctl(m_fd.get(), SNDCTL_DSP_SILENCE, 0); - m_pause.wait(false); + m_pause.wait(true); ioctl(m_fd.get(), SNDCTL_DSP_SKIP, 0); } } diff --git a/src/backends/OSS/OSS.hpp b/src/backends/OSS/OSS.hpp index 578c99d..21dabce 100644 --- a/src/backends/OSS/OSS.hpp +++ b/src/backends/OSS/OSS.hpp @@ -7,7 +7,6 @@ #define CROSSAUDIO_SRC_BACKENDS_OSS_OSS_HPP #include "FileDescriptor.hpp" -#include "PauseFlag.hpp" #include "crossaudio/ErrorCode.h" #include "crossaudio/Flux.h" @@ -86,8 +85,8 @@ class Flux { FileDescriptor m_fd; - PauseFlag m_pause; std::atomic_bool m_halt; + std::atomic_flag m_pause; std::unique_ptr< std::thread > m_thread; }; } // namespace oss diff --git a/src/backends/OSS/PauseFlag.hpp b/src/backends/OSS/PauseFlag.hpp deleted file mode 100644 index 000aff5..0000000 --- a/src/backends/OSS/PauseFlag.hpp +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2024 The Mumble Developers. All rights reserved. -// Use of this source code is governed by a BSD-style license -// that can be found in the LICENSE file at the root of the -// Mumble source tree or at . - -#ifndef CROSSAUDIO_SRC_BACKENDS_OSS_PAUSEFLAG_HPP -#define CROSSAUDIO_SRC_BACKENDS_OSS_PAUSEFLAG_HPP - -#include -#include -#include - -class PauseFlag { -public: - PauseFlag() : m_paused(false) {} - ~PauseFlag() {} - - explicit operator bool() const { return m_paused; } - - void set(const bool value) { - m_paused = value; - m_change.notify_all(); - } - - void wait(const bool value) { - std::mutex mutex; - std::unique_lock lock(mutex); - m_change.wait(lock, [this, value] { return m_paused == value; }); - } - -private: - std::atomic_bool m_paused; - std::condition_variable m_change; -}; - -#endif