use a unique key to store per-thread request buffers
[ardour.git] / libs / pbd / event_loop.cc
index 6506c18be99a1261569453196e4c918540652617..09099f7eb5b260c09da11728b56cc5918c521350 100644 (file)
 
 */
 
+#include <cstring>
+
+#include <pthread.h>
+
+#include "pbd/compose.h"
+#include "pbd/debug.h"
 #include "pbd/event_loop.h"
+#include "pbd/error.h"
 #include "pbd/stacktrace.h"
 
+#include "i18n.h"
+
 using namespace PBD;
 using namespace std;
 
@@ -27,8 +36,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 ();
 }
 
@@ -66,7 +85,7 @@ EventLoop::invalidate_request (void* data)
         * want, and many of the objects we want to do this with already
         * inherit (indirectly) from sigc::trackable.
         */
-       
+
         if (ir->event_loop) {
                Glib::Threads::Mutex::Lock lm (ir->event_loop->slot_invalidation_mutex());
                for (list<BaseRequestObject*>::iterator i = ir->requests.begin(); i != ir->requests.end(); ++i) {
@@ -79,3 +98,107 @@ 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);
+               }
+       }
+
+       DEBUG_TRACE (PBD::DEBUG::EventLoop, string_compose ("for thread \"%1\", found %2 request buffers\n", target_thread, ret.size()));
+
+       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
+               */
+
+               const string key = string_compose ("%1/%2", mapping.emitting_thread, mapping.target_thread_name);
+
+               /* note that there is no cleanup mechanism to remove
+                * dead/out-of-date entries from this map.
+                *
+                * the request buffers themselves will be cleaned up
+                * when the requesting thread exits (by the
+                * thread-local-storage (TLS) cleanup mechanism). 
+                *
+                * but an entry will remain in the map.
+                *
+                * really need a way to register some end-of-thread callback
+                * that will remove the entry from the thread_buffer_requests
+                * container. but there is no such thing in the pthreads API
+                *
+                * the target thread only searches the map once, when the event
+                * loop object is constructed. if it finds invalid buffers
+                * it will (a) never get any requests for them anyway (b) will
+                * find them marked "dead" and delete them.
+                */
+
+               thread_buffer_requests[key] = mapping;
+               DEBUG_TRACE (PBD::DEBUG::EventLoop, string_compose ("pre-registered request buffer for \"%1\" to send to \"%2\", buffer @ %3 (key was %4)\n",
+                                                                   emitting_thread_name, trs->name, mapping.request_buffer, key));
+       }
+}
+