tweak monitoring state logic
[ardour.git] / libs / ardour / graph.cc
index 6dc76673ff4ec25fc8d318270c5d9a81f3824200..cb5ac65f11075ee26c713979edc4067f37c1f6f7 100644 (file)
@@ -21,7 +21,7 @@
 #include <cmath>
 
 #include "pbd/compose.h"
-#include "pbd/cpus.h"
+#include "pbd/debug_rt_alloc.h"
 
 #include "ardour/debug.h"
 #include "ardour/graph.h"
@@ -39,16 +39,33 @@ using namespace ARDOUR;
 using namespace PBD;
 using namespace std;
 
+#ifdef DEBUG_RT_ALLOC
+static Graph* graph = 0;
 
-Graph::Graph (Session & session) 
-        : SessionHandleRef (session) 
+extern "C" {
+
+int alloc_allowed ()
+{
+       return !graph->in_process_thread ();
+}
+
+}
+#endif
+
+Graph::Graph (Session & session)
+        : SessionHandleRef (session)
+        , _quit_threads (false)
+       , _execution_sem ("graph_execution", 0)
+       , _callback_start_sem ("graph_start", 0)
+       , _callback_done_sem ("graph_done", 0)
+       , _cleanup_sem ("graph_cleanup", 0)
 {
         pthread_mutex_init( &_trigger_mutex, NULL);
-        sem_init( &_execution_sem, 0, 0 );
 
-        sem_init( &_callback_start_sem, 0, 0 );
-        sem_init( &_callback_done_sem,  0, 0 );
-        sem_init( &_cleanup_sem,  0, 0 );
+       /* XXX: rather hacky `fix' to stop _trigger_queue.push_back() allocating
+          memory in the RT thread.
+       */
+       _trigger_queue.reserve (8192);
 
         _execution_tokens = 0;
 
@@ -58,64 +75,99 @@ Graph::Graph (Session & session)
         _quit_threads = false;
         _graph_empty = true;
 
-        int num_cpu = hardware_concurrency();
-        int num_threads = num_cpu;
-        int pu = Config->get_processor_usage ();
+        reset_thread_list ();
 
-        if (pu < 0) {
-                /* pu is negative: use "pu" less cores for DSP than appear to be available
-                 */
+        Config->ParameterChanged.connect_same_thread (processor_usage_connection, boost::bind (&Graph::parameter_changed, this, _1));
 
-                if (-pu < num_threads) {
-                        num_threads += pu; 
-                } else {
-                        num_threads = 1;
-                }
-        } else {
-                /* use "pu" cores, if available
-                 */
+#ifdef DEBUG_RT_ALLOC
+       graph = this;
+       pbd_alloc_allowed = &::alloc_allowed;
+#endif
+}
 
-                if (pu <= num_threads) {
-                        num_threads = pu;
-                } 
+void
+Graph::parameter_changed (std::string param)
+{
+        if (param == X_("processor-usage")) {
+                reset_thread_list ();
         }
+}
+
+void
+Graph::reset_thread_list ()
+{
+        uint32_t num_threads = how_many_dsp_threads ();
 
-        info << string_compose (_("Using %2 threads on %1 CPUs"), num_cpu, num_threads) << endmsg;
+        /* don't bother doing anything here if we already have the right
+           number of threads.
+        */
 
+        if (_thread_list.size() == num_threads) {
+                return;
+        }
+
+        Glib::Mutex::Lock lm (_session.engine().process_lock());
        pthread_t a_thread;
 
+        if (!_thread_list.empty()) {
+                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);
        }
 
-        for (int i = 1; i < num_threads; ++i) {
-               if (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::helper_thread, this), &a_thread, 100000));
-                _thread_list.push_back (a_thread);
+        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);
+               }
         }
 }
 
 void
 Graph::session_going_away()
+{
+        drop_threads ();
+
+        // now drop all references on the nodes.
+        _nodes_rt[0].clear();
+        _nodes_rt[1].clear();
+        _init_trigger_list[0].clear();
+        _init_trigger_list[1].clear();
+        _trigger_queue.clear();
+}
+
+void
+Graph::drop_threads ()
 {
         _quit_threads = true;
 
-        for (unsigned int i=0; i<_thread_list.size(); i++) {
-                sem_post( &_execution_sem);
+        for (unsigned int i=0; i< _thread_list.size(); i++) {
+               _execution_sem.signal ();
         }
 
-        sem_post( &_callback_start_sem);
+        _callback_start_sem.signal ();
 
-        for (list<pthread_t>::iterator i = _thread_list.begin(); i != _thread_list.end(); i++) {
+        for (list<pthread_t>::iterator i = _thread_list.begin(); i != _thread_list.end(); ++i) {
                 void* status;
                 pthread_join (*i, &status);
         }
 
-        // now drop all references on the nodes.
-        _nodes_rt[0].clear();
-        _nodes_rt[1].clear();
-        _init_trigger_list[0].clear();
-        _init_trigger_list[1].clear();
-        _trigger_queue.clear();
+        _thread_list.clear ();
+
+       _execution_tokens = 0;
+
+        _quit_threads = false;
 }
 
 void
@@ -137,7 +189,7 @@ Graph::clear_other_chain ()
                 /* setup chain == pending chain - we have
                    to wait till this is no longer true.
                 */
-                _cleanup_cond.wait (_swap_mutex);                
+                _cleanup_cond.wait (_swap_mutex);
         }
 }
 
@@ -177,7 +229,7 @@ void
 Graph::trigger (GraphNode* n)
 {
         pthread_mutex_lock (&_trigger_mutex);
-        _trigger_queue.push_backn);
+        _trigger_queue.push_back (n);
         pthread_mutex_unlock (&_trigger_mutex);
 }
 
@@ -188,7 +240,7 @@ Graph::dec_ref()
 
                 // ok... this cycle is finished now.
                 // we are the only thread alive.
-       
+
                 this->restart_cycle();
         }
 }
@@ -196,26 +248,26 @@ Graph::dec_ref()
 void
 Graph::restart_cycle()
 {
-        //printf( "cycle_done chain: %d\n", _current_chain);
-
         // we are through. wakeup our caller.
+
   again:
-        sem_post( &_callback_done_sem);
+        _callback_done_sem.signal ();
 
         // block until we are triggered.
-        sem_wait( &_callback_start_sem);
-        if (_quit_threads)
-                return;
+        _callback_start_sem.wait();
 
-        //printf( "cycle_start\n" );
+        if (_quit_threads) {
+                return;
+        }
 
         this->prep();
-        if (_graph_empty)
+
+        if (_graph_empty) {
                 goto again;
-        //printf( "cycle_start chain: %d\n", _current_chain);
+        }
 
         // returning will restart the cycle.
-        //  starting with waking up the others.
+        // starting with waking up the others.
 }
 
 static bool
@@ -278,7 +330,7 @@ Graph::rechain (boost::shared_ptr<RouteList> routelist)
                 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
                         if (rp->direct_feeds (*ri)) {
                                 if (is_feedback (routelist, rp.get(), *ri)) {
-                                        continue; 
+                                        continue;
                                 }
 
                                 has_output = true;
@@ -303,7 +355,7 @@ Graph::rechain (boost::shared_ptr<RouteList> routelist)
 
                 if (!has_output)
                         _init_finished_refcount[chain] += 1;
-        } 
+        }
 
         _pending_chain = chain;
         dump(chain);
@@ -328,17 +380,20 @@ Graph::run_one()
         int wakeup = min (et, ts);
         _execution_tokens -= wakeup;
 
-        for (int i=0; i<wakeup; i++ ) {
-                sem_post (&_execution_sem);
+        DEBUG_TRACE(DEBUG::ProcessThreads, string_compose ("%1 signals %2\n", pthread_self(), wakeup));
+
+        for (int i = 0; i < wakeup; i++) {
+                _execution_sem.signal ();
         }
 
         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()));
-                sem_wait (&_execution_sem);
-                if (_quit_threads)
+                _execution_sem.wait ();
+                if (_quit_threads) {
                         return true;
+                }
                 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 is awake\n", pthread_self()));
                 pthread_mutex_lock (&_trigger_mutex);
                 if (_trigger_queue.size()) {
@@ -351,6 +406,8 @@ 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()));
+
         return false;
 }
 
@@ -364,10 +421,10 @@ static void get_rt()
 
         if (priority) {
                 struct sched_param rtparam;
-       
+
                 memset (&rtparam, 0, sizeof (rtparam));
                 rtparam.sched_priority = priority;
-       
+
                 pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam);
         }
 }
@@ -375,7 +432,9 @@ static void get_rt()
 void
 Graph::helper_thread()
 {
-        ProcessThread *pt = new ProcessThread;
+       suspend_rt_malloc_checks ();
+       ProcessThread* pt = new ProcessThread ();
+       resume_rt_malloc_checks ();
 
         pt->get_buffers();
         get_rt();
@@ -392,22 +451,31 @@ Graph::helper_thread()
 void
 Graph::main_thread()
 {
-        ProcessThread *pt = new ProcessThread;
+       suspend_rt_malloc_checks ();
+       ProcessThread* pt = new ProcessThread ();
+       resume_rt_malloc_checks ();
 
         pt->get_buffers();
         get_rt();
 
   again:
-        sem_wait (&_callback_start_sem);
+        _callback_start_sem.wait ();
+       DEBUG_TRACE(DEBUG::ProcessThreads, "main thread is awake\n");
+
+        if (_quit_threads) {
+                return;
+        }
 
         this->prep();
 
         if (_graph_empty && !_quit_threads) {
-                sem_post (&_callback_done_sem);
+                _callback_done_sem.signal ();
+                DEBUG_TRACE(DEBUG::ProcessThreads, "main thread sees graph done, goes back to slee\n");
                 goto again;
         }
 
         while (1) {
+               DEBUG_TRACE(DEBUG::ProcessThreads, "main thread runs one graph node\n");
                 if (run_one()) {
                         break;
                 }
@@ -444,14 +512,11 @@ Graph::dump (int chain)
 }
 
 int
-Graph::silent_process_routes (nframes_t nframes, framepos_t start_frame, framepos_t end_frame,
-                              bool can_record, bool rec_monitors_input, 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_rec_monitors_input = rec_monitors_input;
 
         _process_silent = true;
         _process_noroll = false;
@@ -459,8 +524,9 @@ Graph::silent_process_routes (nframes_t nframes, framepos_t start_frame, framepo
         _process_need_butler = false;
 
         if (!_graph_empty) {
-                sem_post (&_callback_start_sem);
-                sem_wait (&_callback_done_sem);
+               DEBUG_TRACE(DEBUG::ProcessThreads, "wake graph for silent process\n");
+                _callback_start_sem.signal ();
+                _callback_done_sem.wait ();
         }
 
         need_butler = _process_need_butler;
@@ -469,14 +535,13 @@ Graph::silent_process_routes (nframes_t nframes, framepos_t start_frame, framepo
 }
 
 int
-Graph::process_routes (nframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick,
-                       bool can_record, bool rec_monitors_input, 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_rec_monitors_input = rec_monitors_input;
         _process_declick = declick;
 
         _process_silent = false;
@@ -484,8 +549,11 @@ Graph::process_routes (nframes_t nframes, framepos_t start_frame, framepos_t end
         _process_retval = 0;
         _process_need_butler = false;
 
-        sem_post (&_callback_start_sem);
-        sem_wait (&_callback_done_sem);
+       DEBUG_TRACE(DEBUG::ProcessThreads, "wake graph for non-silent process\n");
+        _callback_start_sem.signal ();
+       _callback_done_sem.wait ();
+
+       DEBUG_TRACE (DEBUG::ProcessThreads, "graph execution complete\n");
 
         need_butler = _process_need_butler;
 
@@ -493,13 +561,14 @@ Graph::process_routes (nframes_t nframes, framepos_t start_frame, framepos_t end
 }
 
 int
-Graph::routes_no_roll (nframes_t nframes, framepos_t start_frame, framepos_t end_frame, 
-                       bool non_rt_pending, bool can_record, int declick)
+Graph::routes_no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
+                       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;
 
@@ -508,8 +577,9 @@ Graph::routes_no_roll (nframes_t nframes, framepos_t start_frame, framepos_t end
         _process_retval = 0;
         _process_need_butler = false;
 
-        sem_post (&_callback_start_sem);
-        sem_wait (&_callback_done_sem);
+       DEBUG_TRACE(DEBUG::ProcessThreads, "wake graph for no-roll process\n");
+        _callback_start_sem.signal ();
+        _callback_done_sem.wait ();
 
         return _process_retval;
 }
@@ -524,23 +594,31 @@ Graph::process_one_route (Route* route)
         DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 runs route %2\n", pthread_self(), route->name()));
 
         if (_process_silent) {
-                retval = route->silent_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_can_record, _process_rec_monitors_input, 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, _process_declick);
+                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, _process_rec_monitors_input, need_butler);
+                retval = route->roll (_process_nframes, _process_start_frame, _process_end_frame, _process_declick, need_butler);
         }
 
         if (retval) {
                 _process_retval = retval;
         }
-    
+
         if (need_butler) {
                 _process_need_butler = true;
         }
 }
 
+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 ();
+}