add a mechanism to use existing MIDNAM info and connect to PatchesChanged in future, atomically

The atomically is with respect to the initial thread-based MIDNAM loading
This commit is contained in:
Paul Davis 2019-12-23 10:26:05 -07:00
parent 63ba8da3e1
commit 61aeb05f2e
2 changed files with 36 additions and 3 deletions

View file

@ -23,8 +23,11 @@
#ifndef MIDI_PATCH_MANAGER_H_
#define MIDI_PATCH_MANAGER_H_
#include <glibmm/threads.h>
#include "midi++/midnam_patch.h"
#include "pbd/event_loop.h"
#include "pbd/signals.h"
#include "pbd/search_path.h"
@ -147,7 +150,10 @@ public:
const DeviceNamesByMaker& devices_by_manufacturer() const { return _devices_by_manufacturer; }
void load_midnams_in_thread ();
void maybe_use (PBD::ScopedConnectionList& clist,
PBD::EventLoop::InvalidationRecord* ir,
const boost::function<void()>& slot,
PBD::EventLoop* event_loop);
private:
bool load_midi_name_document(const std::string& file_path);
bool add_midi_name_document(boost::shared_ptr<MIDINameDocument>);
@ -164,6 +170,7 @@ private:
DeviceNamesByMaker _devices_by_manufacturer;
MasterDeviceNames::Models _all_models;
Glib::Threads::Mutex _lock;
bool no_patch_changed_messages;
pthread_t _midnam_load_thread;
static void* _midnam_load (void *);
@ -175,4 +182,3 @@ private:
} // namespace MIDI
#endif /* MIDI_PATCH_MANAGER_H_ */

View file

@ -265,13 +265,20 @@ MidiPatchManager::_midnam_load (void* arg)
void
MidiPatchManager::load_midnams ()
{
/* really there's only going to be one x-thread request/signal before
this thread exits but we'll say 8 just to be sure.
*/
PBD::notify_event_loops_about_thread_creation (pthread_self(), "midi-patch-manager", 8);
{
Glib::Threads::Mutex::Lock lm (_lock);
PBD::Unwinder<bool> npc (no_patch_changed_messages, true);
for (Searchpath::const_iterator i = _search_path.begin(); i != _search_path.end(); ++i) {
add_midnam_files_from_directory (*i);
}
}
PatchesChanged (); /* EMIT SIGNAL */
}
@ -280,3 +287,23 @@ MidiPatchManager::load_midnams_in_thread ()
{
pthread_create_and_store (X_("midnam"), &_midnam_load_thread, _midnam_load, this);
}
void
MidiPatchManager::maybe_use (PBD::ScopedConnectionList& cl,
PBD::EventLoop::InvalidationRecord* ir,
const boost::function<void()> & midnam_info_method,
PBD::EventLoop* event_loop)
{
{
Glib::Threads::Mutex::Lock lm (_lock);
if (!_documents.empty()) {
/* already have documents loaded, so call closure to use them */
midnam_info_method ();
}
/* if/when they ever change, call the closure (maybe multiple times) */
PatchesChanged.connect (cl, ir, midnam_info_method, event_loop);
}
}