Skip to content

Commit

Permalink
Tried to fix hang on Panic GrandOrgue#1726
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg68 committed Dec 1, 2023
1 parent 9b99958 commit b540d23
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 34 deletions.
86 changes: 52 additions & 34 deletions src/grandorgue/sound/GOSound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@

GOSound::GOSound(GOConfig &settings)
: m_open(false),
m_IsRunning(false),
logSoundErrors(true),
m_AudioOutputs(),
m_WaitCount(),
m_CalcCount(),
m_NCallbacksEntered(0),
m_CallbackCondition(m_CallbackMutex),
m_SamplesPerBuffer(0),
meter_counter(0),
m_defaultAudioDevice(),
Expand Down Expand Up @@ -68,6 +71,7 @@ void GOSound::OpenSound() {
m_LastErrorMessage = wxEmptyString;
assert(!m_open);
assert(m_AudioOutputs.size() == 0);
m_NCallbacksEntered.store(0);

unsigned audio_group_count = m_config.GetAudioGroups().size();
std::vector<GOAudioDeviceConfig> audio_config
Expand Down Expand Up @@ -151,6 +155,7 @@ void GOSound::OpenSound() {
StartStreams();
StartThreads();
m_open = true;
m_IsRunning.store(true);

if (m_OrganController)
m_OrganController->PreparePlayback(
Expand Down Expand Up @@ -189,6 +194,14 @@ void GOSound::StartStreams() {
}

void GOSound::CloseSound() {
// wait for all started callbacks to finish
m_IsRunning.store(false);
{
GOMutexLocker lock(m_CallbackMutex);

while (m_NCallbacksEntered.load() > 0)
m_CallbackCondition.Wait();
}
StopThreads();

for (unsigned i = 0; i < m_AudioOutputs.size(); i++) {
Expand Down Expand Up @@ -318,44 +331,49 @@ void GOSound::UpdateMeter() {

bool GOSound::AudioCallback(
unsigned dev_index, float *output_buffer, unsigned int n_frames) {
if (n_frames != m_SamplesPerBuffer) {
wxLogError(
_("No sound output will happen. Samples per buffer has been "
"changed by the sound driver to %d"),
n_frames);
return 1;
}
GOSoundOutput *device = &m_AudioOutputs[dev_index];
GOMutexLocker locker(device->mutex);

while (device->wait && device->waiting)
device->condition.Wait();

unsigned cnt = m_CalcCount.fetch_add(1);
m_SoundEngine.GetAudioOutput(
output_buffer, n_frames, dev_index, cnt + 1 >= m_AudioOutputs.size());
device->wait = true;
unsigned count = m_WaitCount.fetch_add(1);

if (count + 1 == m_AudioOutputs.size()) {
m_SoundEngine.NextPeriod();
UpdateMeter();

{
GOMutexLocker thread_locker(m_thread_lock);
for (unsigned i = 0; i < m_Threads.size(); i++)
m_Threads[i]->Wakeup();
if (m_IsRunning.load()) {
if (n_frames != m_SamplesPerBuffer) {
wxLogError(
_("No sound output will happen. Samples per buffer has been "
"changed by the sound driver to %d"),
n_frames);
return 1;
}
m_CalcCount.exchange(0);
m_WaitCount.exchange(0);
m_NCallbacksEntered.fetch_add(1);

for (unsigned i = 0; i < m_AudioOutputs.size(); i++) {
GOMutexLocker(m_AudioOutputs[i].mutex, i == dev_index);
m_AudioOutputs[i].wait = false;
m_AudioOutputs[i].condition.Signal();
GOSoundOutput *device = &m_AudioOutputs[dev_index];
GOMutexLocker locker(device->mutex);

while (device->wait && device->waiting)
device->condition.Wait();

unsigned cnt = m_CalcCount.fetch_add(1);
m_SoundEngine.GetAudioOutput(
output_buffer, n_frames, dev_index, cnt + 1 >= m_AudioOutputs.size());
device->wait = true;
unsigned count = m_WaitCount.fetch_add(1);

if (count + 1 == m_AudioOutputs.size()) {
m_SoundEngine.NextPeriod();
UpdateMeter();

{
GOMutexLocker thread_locker(m_thread_lock);
for (unsigned i = 0; i < m_Threads.size(); i++)
m_Threads[i]->Wakeup();
}
m_CalcCount.exchange(0);
m_WaitCount.exchange(0);

for (unsigned i = 0; i < m_AudioOutputs.size(); i++) {
GOMutexLocker(m_AudioOutputs[i].mutex, i == dev_index);
m_AudioOutputs[i].wait = false;
m_AudioOutputs[i].condition.Signal();
}
}
m_NCallbacksEntered.fetch_sub(1);
m_CallbackCondition.Signal();
}

return true;
}

Expand Down
4 changes: 4 additions & 0 deletions src/grandorgue/sound/GOSound.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class GOSound {

private:
bool m_open;
std::atomic_bool m_IsRunning;

GOMutex m_lock;
GOMutex m_thread_lock;
Expand All @@ -73,6 +74,9 @@ class GOSound {
std::vector<GOSoundOutput> m_AudioOutputs;
std::atomic_uint m_WaitCount;
std::atomic_uint m_CalcCount;
GOMutex m_CallbackMutex;
std::atomic_int m_NCallbacksEntered;
GOCondition m_CallbackCondition;

unsigned m_SamplesPerBuffer;

Expand Down

0 comments on commit b540d23

Please sign in to comment.