Skip to content

Commit

Permalink
Switch to fair mutexes to avoid UI deadlocks (#245)
Browse files Browse the repository at this point in the history
Taken from https://github.com/yohhoy/yamc/blob/master/include/fair_mutex.hpp

Co-authored-by: nyanpasu64 <nyanpasu64@tuta.io>
  • Loading branch information
Gumball2415 and nyanpasu64 authored Dec 27, 2023
1 parent ba0d902 commit 6613efc
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 6 deletions.
17 changes: 11 additions & 6 deletions Source/SoundGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
#include "rigtorp/SPSCQueue.h"
#include "libsamplerate/include/samplerate.h"
#include "utils/handle_ptr.h"
#include "yamc/fair_mutex.hpp"
#include <queue> // // //
#include "Common.h"
#include "FamiTrackerTypes.h"

#include <atomic>
#include <cstdint>
#include <memory>
#include <mutex>
#include <thread>

const int VIBRATO_LENGTH = 256;
Expand Down Expand Up @@ -107,6 +107,8 @@ class CRegisterState; // // //

// CSoundGen

using FairMutex = yamc::fair::mutex;

class CSoundGen : IAudioCallback
{
public:
Expand Down Expand Up @@ -369,7 +371,10 @@ class CSoundGen : IAudioCallback

// Thread synchronization
private:
mutable std::mutex m_csAPULock; // // //
/// A fair mutex ensures that if the audio thread holds m_csAPULock,
/// and the UI thread is waiting for m_csAPULock,
/// the audio thread cannot release and reacquire m_csAPULock (starving the UI thread of access).
mutable FairMutex m_csAPULock; // // //
mutable std::mutex m_csVisualizerWndLock;

// Handles
Expand Down Expand Up @@ -513,11 +518,11 @@ class CSoundGen : IAudioCallback
afx_msg void OnRemoveDocument(WPARAM wParam, LPARAM lParam);

public:
std::unique_lock<std::mutex> Lock() {
return std::unique_lock<std::mutex>(m_csAPULock);
std::unique_lock<FairMutex> Lock() {
return std::unique_lock<FairMutex>(m_csAPULock);
}

std::unique_lock<std::mutex> DeferLock() {
return std::unique_lock<std::mutex>(m_csAPULock, std::defer_lock);
std::unique_lock<FairMutex> DeferLock() {
return std::unique_lock<FairMutex>(m_csAPULock, std::defer_lock);
}
};
87 changes: 87 additions & 0 deletions Source/yamc/fair_mutex.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* fair_mutex.hpp
*
* MIT License
*
* Copyright (c) 2017 yohhoy
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef YAMC_FAIR_MUTEX_HPP_
#define YAMC_FAIR_MUTEX_HPP_

#include <condition_variable>
#include <mutex>


namespace yamc {

/*
* fairness (FIFO locking) mutex
*
* - yamc::fair::mutex
* - yamc::fair::recursive_mutex
* - yamc::fair::timed_mutex
* - yamc::fair::recursive_timed_mutex
*/
namespace fair {

class mutex {
std::size_t next_ = 0;
std::size_t curr_ = 0;
std::condition_variable cv_;
std::mutex mtx_;

public:
mutex() = default;
~mutex() = default;

mutex(const mutex&) = delete;
mutex& operator=(const mutex&) = delete;

void lock()
{
std::unique_lock<decltype(mtx_)> lk(mtx_);
const std::size_t request = next_++;
while (request != curr_) {
cv_.wait(lk);
}
}

bool try_lock()
{
std::lock_guard<decltype(mtx_)> lk(mtx_);
if (next_ != curr_)
return false;
++next_;
return true;
}

void unlock()
{
std::lock_guard<decltype(mtx_)> lk(mtx_);
++curr_;
cv_.notify_all();
}
};

} // namespace fair
} // namespace yamc

#endif

0 comments on commit 6613efc

Please sign in to comment.