Skip to content
This repository has been archived by the owner on Mar 27, 2020. It is now read-only.

Commit

Permalink
mutex protection for auto module vector
Browse files Browse the repository at this point in the history
prevents crashes during module dup, but not delete
  • Loading branch information
Skrylar committed Jan 14, 2019
1 parent 7ecfd25 commit f01f0fe
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/jack-audio-module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ void JackAudioModule::wipe_buffers() {
}

void JackAudioModule::globally_register() {
std::unique_lock<std::mutex> lock(g_audio_modules_mutex);

g_audio_modules.push_back(this);

/* ensure modules are not filling up their buffers out of sync */
Expand All @@ -99,6 +101,8 @@ void JackAudioModule::globally_register() {
}

void JackAudioModule::globally_unregister() {
std::unique_lock<std::mutex> lock(g_audio_modules_mutex);

/* drop ourselves from active module list */
auto x = std::find(g_audio_modules.begin(), g_audio_modules.end(), this);
if (x != g_audio_modules.end())
Expand Down
5 changes: 5 additions & 0 deletions src/skjack.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ std::condition_variable g_jack_cv;
// this would be optimal for correctness, but in practice this gets modified
// very infrequently and nobody cares if we miss one or two audio frames right
// after creating or destroying a module...
std::mutex g_audio_modules_mutex;
std::vector<JackAudioModule*> g_audio_modules;
std::atomic<unsigned int> g_audio_blocked(0);

int on_jack_process(jack_nframes_t nframes, void *) {
if (!g_jack_client.alive()) return 1;
/* audio modules vector is blocked; we have to skip this cycle */
if (!g_audio_modules_mutex.try_lock()) return 0;

for (auto itr = g_audio_modules.begin();
itr != g_audio_modules.end();
Expand Down Expand Up @@ -46,6 +49,8 @@ int on_jack_process(jack_nframes_t nframes, void *) {
}

g_audio_blocked = 0;
g_audio_modules_mutex.unlock();

g_jack_cv.notify_all();
return 0;
}
Expand Down
1 change: 1 addition & 0 deletions src/skjack.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern std::condition_variable g_jack_cv;
// We'll be using this from here on out.
extern jaq::client g_jack_client;

extern std::mutex g_audio_modules_mutex;
extern std::vector<JackAudioModule*> g_audio_modules;
extern std::atomic<unsigned int> g_audio_blocked;

Expand Down

0 comments on commit f01f0fe

Please sign in to comment.