diff options
author | Paul Davis <paul@linuxaudiosystems.com> | 2015-12-28 10:14:17 -0500 |
---|---|---|
committer | Paul Davis <paul@linuxaudiosystems.com> | 2015-12-28 10:14:17 -0500 |
commit | 0d9efc11484c901795ff4e9549a1a39715d0474d (patch) | |
tree | 956ab3cd570670bcb1ff68856553f5aec4a8e470 /libs/pbd/event_loop.cc | |
parent | db4834027858b10f313c822c7fb3fad1617f11aa (diff) |
redesign cross-thread registration/signalling system
This new design will work even when threads that need to receive
messages from RT threads are created *after* the RT threads. The
existing design would fail because the RT thread(s) would never
be known the later created threads, and so signals emitted by the
RT thread and causing call_slot() in the receiver would end up
being enqueued using a lock-protected list. The new design ensures
that communication always uses a lock-free FIFO instead
Diffstat (limited to 'libs/pbd/event_loop.cc')
-rw-r--r-- | libs/pbd/event_loop.cc | 106 |
1 files changed, 105 insertions, 1 deletions
diff --git a/libs/pbd/event_loop.cc b/libs/pbd/event_loop.cc index 95b43d9038..671e26bedc 100644 --- a/libs/pbd/event_loop.cc +++ b/libs/pbd/event_loop.cc @@ -17,9 +17,13 @@ */ +#include "pbd/compose.h" #include "pbd/event_loop.h" +#include "pbd/error.h" #include "pbd/stacktrace.h" +#include "i18n.h" + using namespace PBD; using namespace std; @@ -27,13 +31,18 @@ static void do_not_delete_the_loop_pointer (void*) { } Glib::Threads::Private<EventLoop> EventLoop::thread_event_loop (do_not_delete_the_loop_pointer); +Glib::Threads::RWLock EventLoop::thread_buffer_requests_lock; +EventLoop::ThreadRequestBufferList EventLoop::thread_buffer_requests; +EventLoop::RequestBufferSuppliers EventLoop::request_buffer_suppliers; + EventLoop::EventLoop (string const& name) : _name (name) { } EventLoop* -EventLoop::get_event_loop_for_thread() { +EventLoop::get_event_loop_for_thread() +{ return thread_event_loop.get (); } @@ -84,3 +93,98 @@ EventLoop::invalidate_request (void* data) return 0; } +vector<EventLoop::ThreadBufferMapping> +EventLoop::get_request_buffers_for_target_thread (const std::string& target_thread) +{ + vector<ThreadBufferMapping> ret; + Glib::Threads::RWLock::WriterLock lm (thread_buffer_requests_lock); + + for (ThreadRequestBufferList::const_iterator x = thread_buffer_requests.begin(); + x != thread_buffer_requests.end(); ++x) { + + if (x->second.target_thread_name == target_thread) { + ret.push_back (x->second); + } + } + + return ret; +} + +void +EventLoop::register_request_buffer_factory (const string& target_thread_name, + void* (*factory)(uint32_t)) +{ + + RequestBufferSupplier trs; + trs.name = target_thread_name; + trs.factory = factory; + + { + Glib::Threads::RWLock::WriterLock lm (thread_buffer_requests_lock); + request_buffer_suppliers.push_back (trs); + } +} + +void +EventLoop::pre_register (const string& emitting_thread_name, uint32_t num_requests) +{ + /* Threads that need to emit signals "towards" other threads, but with + RT safe behavior may be created before the receiving threads + exist. This makes it impossible for them to use the + ThreadCreatedWithRequestSize signal to notify receiving threads of + their existence. + + This function creates a request buffer for them to use with + the (not yet) created threads, and stores it where the receiving + thread can find it later. + */ + + ThreadBufferMapping mapping; + Glib::Threads::RWLock::ReaderLock lm (thread_buffer_requests_lock); + + for (RequestBufferSuppliers::iterator trs = request_buffer_suppliers.begin(); trs != request_buffer_suppliers.end(); ++trs) { + + if (!trs->factory) { + /* no factory - no request buffer required or expected */ + continue; + } + + if (emitting_thread_name == trs->name) { + /* no need to register an emitter with itself */ + continue; + } + + mapping.emitting_thread = pthread_self(); + mapping.target_thread_name = trs->name; + + /* Allocate a suitably sized request buffer. This will set the + * thread-local variable that holds a pointer to this request + * buffer. + */ + mapping.request_buffer = trs->factory (num_requests); + + /* now store it where the receiving thread (trs->name) can find + it if and when it is created. (Discovery happens in the + AbstractUI constructor. Note that if + */ + + /* make a key composed of the emitter and receiver thread names */ + + string key = emitting_thread_name; + key += '/'; + key += mapping.target_thread_name; + + /* if the emitting thread was killed and recreated (with the + * same name), this will replace the entry in + * thread_buffer_requests. The old entry will be lazily deleted + * when the target thread finds the request buffer and realizes + * that it is dead. + * + * If the request buffer is replaced before the target thread + * ever finds the dead version, we will leak the old request + * buffer. + */ + + thread_buffer_requests[key] = mapping; + } +} |