Allow cross-thread request invalidators to cope with multiple requests
[ardour.git] / libs / pbd / pbd / abstract_ui.cc
index 98ef094a0088ed2b2da9a5d3813f610e0841b162..a769246f3874a895247e42aaaee52ea98676f689 100644 (file)
@@ -1,4 +1,5 @@
 #include <unistd.h>
+#include <iostream>
 
 #include "pbd/stacktrace.h"
 #include "pbd/abstract_ui.h"
 
 using namespace std;
 
+static void do_not_delete_the_request_buffer (void*) { }
+
+template<typename R>
+Glib::StaticPrivate<typename AbstractUI<R>::RequestBuffer> AbstractUI<R>::per_thread_request_buffer;
+
 template <typename RequestObject>
 AbstractUI<RequestObject>::AbstractUI (const string& name)
        : BaseUI (name)
 {
-       PBD::ThreadCreatedWithRequestSize.connect (mem_fun (*this, &AbstractUI<RequestObject>::register_thread));
+       void (AbstractUI<RequestObject>::*pmf)(string,pthread_t,string,uint32_t) = &AbstractUI<RequestObject>::register_thread;
+
+       /* better to make this connect a handler that runs in the UI event loop but the syntax seems hard, and 
+          register_thread() is thread safe anyway.
+       */
+
+       PBD::ThreadCreatedWithRequestSize.connect_same_thread (new_thread_connection, boost::bind (pmf, this, _1, _2, _3, _4));
 }
 
 template <typename RequestObject> void
@@ -30,7 +42,7 @@ AbstractUI<RequestObject>::register_thread (string target_gui, pthread_t thread_
                request_buffers[thread_id] = b;
        }
 
-       per_thread_request_buffer.set (b);
+       per_thread_request_buffer.set (b, do_not_delete_the_request_buffer);
 }
 
 template <typename RequestObject> RequestObject*
@@ -49,11 +61,13 @@ AbstractUI<RequestObject>::get_request (RequestType rt)
                }
 
                vec.buf[0]->type = rt;
+                vec.buf[0]->valid = true;
                return vec.buf[0];
        }
 
        RequestObject* req = new RequestObject;
        req->type = rt;
+
        return req;
 }
 
@@ -86,10 +100,15 @@ AbstractUI<RequestObject>::handle_ui_requests ()
                        if (vec.len[0] == 0) {
                                break;
                        } else {
-                               request_buffer_map_lock.unlock ();
-                               do_request (vec.buf[0]);
-                               request_buffer_map_lock.lock ();
-                               i->second->increment_read_ptr (1);
+                                if (vec.buf[0]->valid) {
+                                        request_buffer_map_lock.unlock ();
+                                        do_request (vec.buf[0]);
+                                        request_buffer_map_lock.lock ();
+                                        if (vec.buf[0]->invalidation) {
+                                                vec.buf[0]->invalidation->requests.remove (vec.buf[0]);
+                                        }
+                                        i->second->increment_read_ptr (1);
+                                }
                        } 
                }
        }
@@ -103,6 +122,30 @@ AbstractUI<RequestObject>::handle_ui_requests ()
        while (!request_list.empty()) {
                RequestObject* req = request_list.front ();
                request_list.pop_front ();
+
+                /* We need to use this lock, because its the one
+                   returned by slot_invalidation_mutex() and protects
+                   against request invalidation.
+                */
+
+                request_buffer_map_lock.lock ();
+                if (!req->valid) {
+                        delete req;
+                        request_buffer_map_lock.unlock ();
+                        continue;
+                }
+
+                /* we're about to execute this request, so its
+                   too late for any invalidation. mark
+                   the request as "done" before we start.
+                */
+
+                if (req->invalidation) {
+                        req->invalidation->requests.remove (req);
+                }
+
+                request_buffer_map_lock.unlock ();
+
                lm.release ();
 
                do_request (req);
@@ -140,7 +183,7 @@ AbstractUI<RequestObject>::send_request (RequestObject *req)
 }
 
 template<typename RequestObject> void
-AbstractUI<RequestObject>::call_slot (const boost::function<void()>& f)
+AbstractUI<RequestObject>::call_slot (InvalidationRecord* invalidation, const boost::function<void()>& f)
 {
        if (caller_is_self()) {
                f ();
@@ -154,6 +197,13 @@ AbstractUI<RequestObject>::call_slot (const boost::function<void()>& f)
        }
 
        req->the_slot = f;
+        req->invalidation = invalidation;
+
+        if (invalidation) {
+                invalidation->requests.push_back (req);
+                invalidation->event_loop = this;
+        }
+
        send_request (req);
 }