tweak transport bar spacing
[ardour.git] / libs / pbd / pbd / abstract_ui.cc
index eca53916afc1462b2ba9ed6cc5d424b5f5994957..4c13ec1b09f6a05b5c62e6251e1b05e8e22681ef 100644 (file)
 #include <unistd.h>
+#include <iostream>
 
-#include <pbd/abstract_ui.h>
-#include <pbd/pthread_utils.h>
-#include <pbd/failed_constructor.h>
+#include "pbd/stacktrace.h"
+#include "pbd/abstract_ui.h"
+#include "pbd/pthread_utils.h"
+#include "pbd/failed_constructor.h"
 
 #include "i18n.h"
 
 using namespace std;
 
-template <typename RequestObject>
-AbstractUI<RequestObject>::AbstractUI (string name, bool with_signal_pipes)
-       : BaseUI (name, with_signal_pipes)
+template<typename R>
+Glib::StaticPrivate<typename AbstractUI<R>::RequestBuffer> AbstractUI<R>::per_thread_request_buffer;
+
+template<typename RequestBuffer> void 
+cleanup_request_buffer (void* ptr)
 {
-       if (pthread_key_create (&thread_request_buffer_key, 0)) {
-               cerr << _("cannot create thread request buffer key") << endl;
-               throw failed_constructor();
-       }
+        RequestBuffer* rb = (RequestBuffer*) ptr;
 
-       PBD::ThreadCreated.connect (mem_fun (*this, &AbstractUI<RequestObject>::register_thread));
-       PBD::ThreadCreatedWithRequestSize.connect (mem_fun (*this, &AbstractUI<RequestObject>::register_thread_with_request_count));
+        {
+                Glib::Mutex::Lock lm (rb->ui.request_buffer_map_lock);
+                rb->dead = true;
+        }
 }
 
-template <typename RequestObject> void
-AbstractUI<RequestObject>::register_thread (pthread_t thread_id, string name)
+template <typename RequestObject>
+AbstractUI<RequestObject>::AbstractUI (const string& name)
+       : BaseUI (name)
 {
-       register_thread_with_request_count (thread_id, name, 256);
+       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
-AbstractUI<RequestObject>::register_thread_with_request_count (pthread_t thread_id, string thread_name, uint32_t num_requests)
+AbstractUI<RequestObject>::register_thread (string target_gui, pthread_t thread_id, string /*thread name*/, uint32_t num_requests)
 {
-       RequestBuffer* b = new RequestBuffer (num_requests);
+       if (target_gui != name()) {
+               return;
+       }
+
+       RequestBuffer* b = per_thread_request_buffer.get();
+
+        if (b) {
+                /* thread already registered with this UI
+                 */
+                return;
+        }
+
+        b = new RequestBuffer (num_requests, *this);
 
        {
-        Glib::Mutex::Lock lm (request_buffer_map_lock);
+               Glib::Mutex::Lock lm (request_buffer_map_lock);
                request_buffers[thread_id] = b;
        }
 
-       pthread_setspecific (thread_request_buffer_key, b);
+       per_thread_request_buffer.set (b, cleanup_request_buffer<RequestBuffer>);
 }
 
 template <typename RequestObject> RequestObject*
 AbstractUI<RequestObject>::get_request (RequestType rt)
 {
-       RequestBuffer* rbuf = static_cast<RequestBuffer*>(pthread_getspecific (thread_request_buffer_key));
-       
-       if (rbuf == 0) {
-               /* Cannot happen, but if it does we can't use the error reporting mechanism */
-               cerr << _("programming error: ")
-                    << string_compose ("no %1-UI request buffer found for thread %2", name(), pthread_name())
-                    << endl;
-               abort ();
-       }
-       
+       RequestBuffer* rbuf = per_thread_request_buffer.get ();
        RequestBufferVector vec;
-       
-       rbuf->get_write_vector (&vec);
 
-       if (vec.len[0] == 0) {
-               if (vec.len[1] == 0) {
-                       cerr << string_compose ("no space in %1-UI request buffer for thread %2", name(), pthread_name())
-                            << endl;
+       if (rbuf != 0) {
+               /* we have a per-thread FIFO, use it */
+
+               rbuf->get_write_vector (&vec);
+
+               if (vec.len[0] == 0) {
                        return 0;
-               } else {
-                       vec.buf[1]->type = rt;
-                       return vec.buf[1];
                }
-       } else {
+
                vec.buf[0]->type = rt;
+                vec.buf[0]->valid = true;
                return vec.buf[0];
        }
+
+       RequestObject* req = new RequestObject;
+       req->type = rt;
+
+       return req;
 }
 
 template <typename RequestObject> void
 AbstractUI<RequestObject>::handle_ui_requests ()
 {
        RequestBufferMapIterator i;
+       RequestBufferVector vec;
+
+       /* per-thread buffers first */
 
        request_buffer_map_lock.lock ();
 
        for (i = request_buffers.begin(); i != request_buffers.end(); ++i) {
 
-               RequestBufferVector vec;
+                while (true) {
+                        
+                        /* we must process requests 1 by 1 because
+                           the request may run a recursive main
+                           event loop that will itself call
+                           handle_ui_requests. when we return
+                           from the request handler, we cannot
+                           expect that the state of queued requests
+                           is even remotely consistent with
+                           the condition before we called it.
+                        */
+                        
+                        i->second->get_read_vector (&vec);
+                        
+                        if (vec.len[0] == 0) {
+                                break;
+                        } else {
+                                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);
+                                }
+                        } 
+                }
+        }
+
+        /* clean up any dead request buffers (their thread has exited) */
+
+       for (i = request_buffers.begin(); i != request_buffers.end(); ) {
+             if ((*i).second->dead) {
+                     delete (*i).second;
+                     RequestBufferMapIterator tmp = i;
+                     ++tmp;
+                     request_buffers.erase (i);
+                     i = tmp;
+             } else {          
+                     ++i;
+             }
+        }
 
-               while (true) {
+       request_buffer_map_lock.unlock ();
 
-                       /* we must process requests 1 by 1 because
-                          the request may run a recursive main
-                          event loop that will itself call
-                          handle_ui_requests. when we return
-                          from the request handler, we cannot
-                          expect that the state of queued requests
-                          is even remotely consistent with
-                          the condition before we called it.
-                       */
+       /* and now, the generic request buffer. same rules as above apply */
 
-                       i->second->get_read_vector (&vec);
-
-                       if (vec.len[0] == 0) {
-                               break;
-                       } else {
-                               /* request_factory/copy constructor does a deep
-                                  copy of the Request object,
-                                  unlike Ringbuffer::read()
-                               */
-
-                               RequestObject req (*vec.buf[0]);
-                               i->second->increment_read_ptr (1);
-                               request_buffer_map_lock.unlock ();
-                               do_request (&req);
-                               request_buffer_map_lock.lock ();
-                       } 
-               }
-       }
+       Glib::Mutex::Lock lm (request_list_lock);
 
-       request_buffer_map_lock.unlock ();
+       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);
+
+               delete req;
+
+               lm.acquire();
+       }
 }
 
 template <typename RequestObject> void
@@ -123,31 +193,48 @@ AbstractUI<RequestObject>::send_request (RequestObject *req)
        if (base_instance() == 0) {
                return; /* XXX is this the right thing to do ? */
        }
-       
-       if (caller_is_ui_thread()) {
-               // cerr << "GUI thread sent request " << req << " type = " << req->type << endl;
+
+       if (caller_is_self ()) {
                do_request (req);
        } else {        
-               RequestBuffer* rbuf = static_cast<RequestBuffer*> (pthread_getspecific (thread_request_buffer_key));
+               RequestBuffer* rbuf = per_thread_request_buffer.get ();
 
-               if (rbuf == 0) {
-                       /* can't use the error system to report this, because this
-                          thread isn't registered!
+               if (rbuf != 0) {
+                       rbuf->increment_write_ptr (1);
+               } else {
+                       /* no per-thread buffer, so just use a list with a lock so that it remains
+                          single-reader/single-writer semantics
                        */
-                       cerr << _("programming error: ")
-                            << string_compose ("AbstractUI::send_request() called from %1 (%2), but no request buffer exists for that thread", name(), pthread_name())
-                            << endl;
-                       abort ();
+                       Glib::Mutex::Lock lm (request_list_lock);
+                       request_list.push_back (req);
                }
-               
-               // cerr << "thread " << pthread_self() << " sent request " << req << " type = " << req->type << endl;
 
-               rbuf->increment_write_ptr (1);
-
-               if (signal_pipe[1] >= 0) {
-                       const char c = 0;
-                       write (signal_pipe[1], &c, 1);
-               }
+               request_channel.wakeup ();
        }
 }
 
+template<typename RequestObject> void
+AbstractUI<RequestObject>::call_slot (InvalidationRecord* invalidation, const boost::function<void()>& f)
+{
+       if (caller_is_self()) {
+               f ();
+               return;
+       }
+
+       RequestObject *req = get_request (BaseUI::CallSlot);
+       
+       if (req == 0) {
+               return;
+       }
+
+       req->the_slot = f;
+        req->invalidation = invalidation;
+
+        if (invalidation) {
+                invalidation->requests.push_back (req);
+                invalidation->event_loop = this;
+        }
+
+       send_request (req);
+}      
+