Merge branch 'master' into windows
[ardour.git] / libs / ardour / graph.cc
index 7d75738e0b4d98e5477e39125f36b4f55ac0ca4b..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,60 +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 ();
                }
         }
 }
@@ -153,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;
 
@@ -174,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) {
@@ -222,17 +202,20 @@ 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++) {
-                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)
@@ -284,7 +267,7 @@ Graph::restart_cycle()
 void
 Graph::rechain (boost::shared_ptr<RouteList> routelist, GraphEdges const & edges)
 {
-        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));
@@ -376,7 +359,7 @@ Graph::run_one()
        /* 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 ();
@@ -385,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();
@@ -402,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()
 {
@@ -433,7 +398,6 @@ Graph::helper_thread()
        resume_rt_malloc_checks ();
 
         pt->get_buffers();
-        get_rt();
 
         while(1) {
                 if (run_one()) {
@@ -453,7 +417,6 @@ Graph::main_thread()
        resume_rt_malloc_checks ();
 
         pt->get_buffers();
-        get_rt();
 
   again:
         _callback_start_sem.wait ();
@@ -590,7 +553,7 @@ 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, need_butler);
@@ -614,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 ();
 }