Merge branch 'master' into windows
[ardour.git] / libs / ardour / graph.cc
index f0d3075b30334836a35402ffe9815bba8596314c..72017d1b36472efb883b85945f34d337257ea726 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "pbd/compose.h"
 #include "pbd/debug_rt_alloc.h"
+#include "pbd/pthread_utils.h"
 
 #include "ardour/debug.h"
 #include "ardour/graph.h"
@@ -77,59 +78,42 @@ Graph::Graph (Session & session)
 
         reset_thread_list ();
 
-        Config->ParameterChanged.connect_same_thread (processor_usage_connection, boost::bind (&Graph::parameter_changed, this, _1));
-
 #ifdef DEBUG_RT_ALLOC
        graph = this;
        pbd_alloc_allowed = &::alloc_allowed;
 #endif
 }
 
-void
-Graph::parameter_changed (std::string param)
-{
-        if (param == X_("processor-usage")) {
-                reset_thread_list ();
-        }
-}
-
+/** Set up threads for running the graph */
 void
 Graph::reset_thread_list ()
 {
         uint32_t num_threads = how_many_dsp_threads ();
 
+       /* For now, we shouldn't be using the graph code if we only have 1 DSP thread */
+       assert (num_threads > 1);
+
         /* don't bother doing anything here if we already have the right
            number of threads.
         */
 
-        if (_thread_list.size() == num_threads) {
+        if (AudioEngine::instance()->process_thread_count() == num_threads) {
                 return;
         }
 
-        Glib::Mutex::Lock lm (_session.engine().process_lock());
-       pthread_t a_thread;
+        Glib::Threads::Mutex::Lock lm (_session.engine().process_lock());
 
-        if (!_thread_list.empty()) {
+        if (AudioEngine::instance()->process_thread_count() != 0) {
                 drop_threads ();
         }
 
-#if 0
-        /* XXX this only makes sense when we can use just the AudioEngine thread
-           and still keep the graph current with the route list
-        */
-        if (num_threads <= 1) {
-                /* no point creating 1 thread - the AudioEngine already gives us one
-                 */
-                return;
-        }
-#endif
-       if (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::main_thread, this), &a_thread, 100000) == 0) {
-               _thread_list.push_back (a_thread);
+       if (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::main_thread, this)) != 0) {
+               throw failed_constructor ();
        }
 
         for (uint32_t i = 1; i < num_threads; ++i) {
-               if (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::helper_thread, this), &a_thread, 100000) == 0) {
-                       _thread_list.push_back (a_thread);
+               if (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::helper_thread, this))) {
+                       throw failed_constructor ();
                }
         }
 }
@@ -152,18 +136,15 @@ Graph::drop_threads ()
 {
         _quit_threads = true;
 
-        for (unsigned int i=0; i< _thread_list.size(); i++) {
+        uint32_t thread_count = AudioEngine::instance()->process_thread_count ();
+
+        for (unsigned int i=0; i < thread_count; i++) {
                _execution_sem.signal ();
         }
 
         _callback_start_sem.signal ();
 
-        for (list<pthread_t>::iterator i = _thread_list.begin(); i != _thread_list.end(); ++i) {
-                void* status;
-                pthread_join (*i, &status);
-        }
-
-        _thread_list.clear ();
+       AudioEngine::instance()->join_process_threads ();
 
        _execution_tokens = 0;
 
@@ -173,7 +154,7 @@ Graph::drop_threads ()
 void
 Graph::clear_other_chain ()
 {
-        Glib::Mutex::Lock ls (_swap_mutex);
+        Glib::Threads::Mutex::Lock ls (_swap_mutex);
 
         while (1) {
                 if (_setup_chain != _pending_chain) {
@@ -220,28 +201,36 @@ Graph::prep()
         }
         _finished_refcount = _init_finished_refcount[chain];
 
+       /* Trigger the initial nodes for processing, which are the ones at the `input' end */
+       pthread_mutex_lock (&_trigger_mutex);
         for (i=_init_trigger_list[chain].begin(); i!=_init_trigger_list[chain].end(); i++) {
-                this->trigger( i->get() );
+               /* don't use ::trigger here, as we have already locked the mutex */
+                _trigger_queue.push_back (i->get ());
         }
+       pthread_mutex_unlock (&_trigger_mutex);
 }
 
 void
 Graph::trigger (GraphNode* n)
 {
-        pthread_mutex_lock (&_trigger_mutex);
+       pthread_mutex_lock (&_trigger_mutex);
         _trigger_queue.push_back (n);
-        pthread_mutex_unlock (&_trigger_mutex);
+       pthread_mutex_unlock (&_trigger_mutex);
 }
 
+/** Called when a node at the `output' end of the chain (ie one that has no-one to feed)
+ *  is finished.
+ */
 void
 Graph::dec_ref()
 {
         if (g_atomic_int_dec_and_test (&_finished_refcount)) {
 
-                // ok... this cycle is finished now.
-                // we are the only thread alive.
+               /* We have run all the nodes that are at the `output' end of
+                  the graph, so there is nothing more to do this time around.
+               */
 
-                this->restart_cycle();
+               restart_cycle ();
         }
 }
 
@@ -253,14 +242,14 @@ Graph::restart_cycle()
   again:
         _callback_done_sem.signal ();
 
-        // block until we are triggered.
+        /* Block until the a process callback triggers us */
         _callback_start_sem.wait();
 
         if (_quit_threads) {
                 return;
         }
 
-        this->prep();
+       prep ();
 
         if (_graph_empty) {
                 goto again;
@@ -270,97 +259,83 @@ Graph::restart_cycle()
         // starting with waking up the others.
 }
 
-static bool
-is_feedback (boost::shared_ptr<RouteList> routelist, Route* from, boost::shared_ptr<Route> to)
-{
-        for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
-                if ((*ri).get() == from)
-                        return false;
-                if ((*ri) == to)
-                        return true;
-        }
-        assert(0);
-        return false;
-}
-
-static bool
-is_feedback (boost::shared_ptr<RouteList> routelist, boost::shared_ptr<Route> from, Route* to)
-{
-        for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
-                if ((*ri).get() == to)
-                        return true;
-                if ((*ri) == from)
-                        return false;
-        }
-        assert(0);
-        return false;
-}
+/** Rechain our stuff using a list of routes (which can be in any order) and
+ *  a directed graph of their interconnections, which is guaranteed to be
+ *  acyclic.
+ */
 
 void
-Graph::rechain (boost::shared_ptr<RouteList> routelist)
+Graph::rechain (boost::shared_ptr<RouteList> routelist, GraphEdges const & edges)
 {
-        node_list_t::iterator ni;
-        Glib::Mutex::Lock ls (_swap_mutex);
+        Glib::Threads::Mutex::Lock ls (_swap_mutex);
 
         int chain = _setup_chain;
         DEBUG_TRACE (DEBUG::Graph, string_compose ("============== setup %1\n", chain));
-        // set all refcounts to 0;
 
+       /* This will become the number of nodes that do not feed any other node;
+          once we have processed this number of those nodes, we have finished.
+       */
         _init_finished_refcount[chain] = 0;
+
+       /* This will become a list of nodes that are not fed by another node, ie
+          those at the `input' end.
+       */
         _init_trigger_list[chain].clear();
 
         _nodes_rt[chain].clear();
 
+       /* Clear things out, and make _nodes_rt[chain] a copy of routelist */
         for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
-                node_ptr_t n = boost::dynamic_pointer_cast<GraphNode> (*ri);
-
-                n->_init_refcount[chain] = 0;
-                n->_activation_set[chain].clear();
-                _nodes_rt[chain].push_back(n);
+                (*ri)->_init_refcount[chain] = 0;
+                (*ri)->_activation_set[chain].clear();
+                _nodes_rt[chain].push_back (*ri);
         }
 
         // now add refs for the connections.
 
-        for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) {
-                bool has_input  = false;
-                bool has_output = false;
+        for (node_list_t::iterator ni = _nodes_rt[chain].begin(); ni != _nodes_rt[chain].end(); ni++) {
 
-                boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni);
+                boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (*ni);
 
-                for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
-                        if (rp->direct_feeds (*ri)) {
-                                if (is_feedback (routelist, rp.get(), *ri)) {
-                                        continue;
-                                }
+               /* The routes that are directly fed by r */
+               set<GraphVertex> fed_from_r = edges.from (r);
 
-                                has_output = true;
-                                (*ni)->_activation_set[chain].insert (boost::dynamic_pointer_cast<GraphNode> (*ri) );
-                        }
-                }
+               /* Hence whether r has an output */
+               bool const has_output = !fed_from_r.empty ();
 
-                for (Route::FedBy::iterator fi=rp->fed_by().begin(); fi!=rp->fed_by().end(); fi++) {
-                        if (boost::shared_ptr<Route> r = fi->r.lock()) {
-                                if (!is_feedback (routelist, r, rp.get())) {
-                                        has_input = true;
-                                }
-                        }
-                }
+               /* Set up r's activation set */
+               for (set<GraphVertex>::iterator i = fed_from_r.begin(); i != fed_from_r.end(); ++i) {
+                       r->_activation_set[chain].insert (*i);
+               }
+
+               /* r has an input if there are some incoming edges to r in the graph */
+               bool const has_input = !edges.has_none_to (r);
 
-                for (node_set_t::iterator ai=(*ni)->_activation_set[chain].begin(); ai!=(*ni)->_activation_set[chain].end(); ai++) {
+               /* Increment the refcount of any route that we directly feed */
+                for (node_set_t::iterator ai = r->_activation_set[chain].begin(); ai != r->_activation_set[chain].end(); ai++) {
                         (*ai)->_init_refcount[chain] += 1;
                 }
 
-                if (!has_input)
+                if (!has_input) {
+                       /* no input, so this node needs to be triggered initially to get things going */
                         _init_trigger_list[chain].push_back (*ni);
+               }
 
-                if (!has_output)
+                if (!has_output) {
+                       /* no output, so this is one of the nodes that we can count off to decide
+                          if we've finished
+                       */
                         _init_finished_refcount[chain] += 1;
+               }
         }
 
         _pending_chain = chain;
         dump(chain);
 }
 
+/** Called by both the main thread and all helpers.
+ *  @return true to quit, false to carry on.
+ */
 bool
 Graph::run_one()
 {
@@ -374,13 +349,17 @@ Graph::run_one()
                 to_run = 0;
         }
 
+       /* the number of threads that are asleep */
        int et = _execution_tokens;
+       /* the number of nodes that need to be run */
        int ts = _trigger_queue.size();
 
+       /* hence how many threads to wake up */
         int wakeup = min (et, ts);
+       /* update the number of threads that will still be sleeping */
         _execution_tokens -= wakeup;
 
-        DEBUG_TRACE(DEBUG::ProcessThreads, string_compose ("%1 signals %2\n", pthread_self(), wakeup));
+        DEBUG_TRACE(DEBUG::ProcessThreads, string_compose ("%1 signals %2\n", pthread_name(), wakeup));
 
         for (int i = 0; i < wakeup; i++) {
                 _execution_sem.signal ();
@@ -389,12 +368,12 @@ Graph::run_one()
         while (to_run == 0) {
                 _execution_tokens += 1;
                 pthread_mutex_unlock (&_trigger_mutex);
-                DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 goes to sleep\n", pthread_self()));
+                DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 goes to sleep\n", pthread_name()));
                 _execution_sem.wait ();
                 if (_quit_threads) {
                         return true;
                 }
-                DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 is awake\n", pthread_self()));
+                DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 is awake\n", pthread_name()));
                 pthread_mutex_lock (&_trigger_mutex);
                 if (_trigger_queue.size()) {
                         to_run = _trigger_queue.back();
@@ -406,29 +385,11 @@ Graph::run_one()
         to_run->process();
         to_run->finish (_current_chain);
 
-        DEBUG_TRACE(DEBUG::ProcessThreads, string_compose ("%1 has finished run_one()\n", pthread_self()));
+        DEBUG_TRACE(DEBUG::ProcessThreads, string_compose ("%1 has finished run_one()\n", pthread_name()));
 
         return false;
 }
 
-static void get_rt()
-{
-        if (!jack_is_realtime (AudioEngine::instance()->jack())) {
-                return;
-        }
-
-        int priority = jack_client_real_time_priority (AudioEngine::instance()->jack());
-
-        if (priority) {
-                struct sched_param rtparam;
-
-                memset (&rtparam, 0, sizeof (rtparam));
-                rtparam.sched_priority = priority;
-
-                pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam);
-        }
-}
-
 void
 Graph::helper_thread()
 {
@@ -437,7 +398,6 @@ Graph::helper_thread()
        resume_rt_malloc_checks ();
 
         pt->get_buffers();
-        get_rt();
 
         while(1) {
                 if (run_one()) {
@@ -448,6 +408,7 @@ Graph::helper_thread()
         pt->drop_buffers();
 }
 
+/** Here's the main graph thread */
 void
 Graph::main_thread()
 {
@@ -456,24 +417,25 @@ Graph::main_thread()
        resume_rt_malloc_checks ();
 
         pt->get_buffers();
-        get_rt();
 
   again:
         _callback_start_sem.wait ();
+       
        DEBUG_TRACE(DEBUG::ProcessThreads, "main thread is awake\n");
 
         if (_quit_threads) {
                 return;
         }
 
-        this->prep();
+       prep ();
 
         if (_graph_empty && !_quit_threads) {
                 _callback_done_sem.signal ();
-                DEBUG_TRACE(DEBUG::ProcessThreads, "main thread sees graph done, goes back to slee\n");
+                DEBUG_TRACE(DEBUG::ProcessThreads, "main thread sees graph done, goes back to sleep\n");
                 goto again;
         }
 
+       /* This loop will run forever */
         while (1) {
                DEBUG_TRACE(DEBUG::ProcessThreads, "main thread runs one graph node\n");
                 if (run_one()) {
@@ -512,13 +474,11 @@ Graph::dump (int chain)
 }
 
 int
-Graph::silent_process_routes (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
-                              bool can_record, bool& need_butler)
+Graph::silent_process_routes (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, bool& need_butler)
 {
         _process_nframes = nframes;
         _process_start_frame = start_frame;
         _process_end_frame = end_frame;
-        _process_can_record = can_record;
 
         _process_silent = true;
         _process_noroll = false;
@@ -537,15 +497,13 @@ Graph::silent_process_routes (pframes_t nframes, framepos_t start_frame, framepo
 }
 
 int
-Graph::process_routes (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick,
-                       bool can_record, bool& need_butler)
+Graph::process_routes (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler)
 {
        DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("graph execution from %1 to %2 = %3\n", start_frame, end_frame, nframes));
 
         _process_nframes = nframes;
         _process_start_frame = start_frame;
         _process_end_frame = end_frame;
-        _process_can_record = can_record;
         _process_declick = declick;
 
         _process_silent = false;
@@ -566,14 +524,13 @@ Graph::process_routes (pframes_t nframes, framepos_t start_frame, framepos_t end
 
 int
 Graph::routes_no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
-                       bool non_rt_pending, bool can_record, int declick)
+                       bool non_rt_pending, int declick)
 {
        DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("no-roll graph execution from %1 to %2 = %3\n", start_frame, end_frame, nframes));
 
         _process_nframes = nframes;
         _process_start_frame = start_frame;
         _process_end_frame = end_frame;
-        _process_can_record = can_record;
         _process_declick = declick;
         _process_non_rt_pending = non_rt_pending;
 
@@ -596,16 +553,16 @@ Graph::process_one_route (Route* route)
 
         assert (route);
 
-        DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 runs route %2\n", pthread_self(), route->name()));
+        DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 runs route %2\n", pthread_name(), route->name()));
 
         if (_process_silent) {
-                retval = route->silent_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_can_record, need_butler);
+                retval = route->silent_roll (_process_nframes, _process_start_frame, _process_end_frame, need_butler);
         } else if (_process_noroll) {
                 route->set_pending_declick (_process_declick);
-                retval = route->no_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_non_rt_pending, _process_can_record);
+                retval = route->no_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_non_rt_pending);
         } else {
                 route->set_pending_declick (_process_declick);
-                retval = route->roll (_process_nframes, _process_start_frame, _process_end_frame, _process_declick, _process_can_record, need_butler);
+                retval = route->roll (_process_nframes, _process_start_frame, _process_end_frame, _process_declick, need_butler);
         }
 
         if (retval) {
@@ -620,10 +577,5 @@ Graph::process_one_route (Route* route)
 bool
 Graph::in_process_thread () const
 {
-       list<pthread_t>::const_iterator i = _thread_list.begin ();
-       while (i != _thread_list.end() && *i != pthread_self ()) {
-               ++i;
-       }
-
-       return i != _thread_list.end ();
+       return AudioEngine::instance()->in_process_thread ();
 }