const correctness.
[ardour.git] / libs / ardour / graph.cc
index 54e619aef89d73adfed11e49fab87392df529f43..0987b979a2e5162609296c783bb42b533dee62a9 100644 (file)
@@ -17,7 +17,6 @@
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
 */
-
 #include <stdio.h>
 #include <cmath>
 
@@ -38,6 +37,7 @@
 
 using namespace ARDOUR;
 using namespace PBD;
+using namespace std;
 
 
 Graph::Graph (Session & session) 
@@ -48,10 +48,10 @@ Graph::Graph (Session & session)
 
         sem_init( &_callback_start_sem, 0, 0 );
         sem_init( &_callback_done_sem,  0, 0 );
+        sem_init( &_cleanup_sem,  0, 0 );
 
         _execution_tokens = 0;
 
-        pthread_mutex_init (&_swap_mutex, NULL);
         _current_chain = 0;
         _pending_chain = 0;
         _setup_chain   = 1;
@@ -63,11 +63,11 @@ Graph::Graph (Session & session)
         int pu = Config->get_processor_usage ();
 
         if (pu < 0) {
-                /* use "pu" less cores for DSP than appear to be available
+                /* pu is negative: use "pu" less cores for DSP than appear to be available
                  */
 
-                if (pu < num_threads) {
-                        num_threads += pu; // pu is negative
+                if (-pu < num_threads) {
+                        num_threads += pu; 
                 } else {
                         num_threads = 1;
                 }
@@ -100,7 +100,7 @@ Graph::session_going_away()
 
         sem_post( &_callback_start_sem);
 
-        for (std::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);
         }
@@ -113,21 +113,45 @@ Graph::session_going_away()
         _trigger_queue.clear();
 }
 
+void
+Graph::clear_other_chain ()
+{
+        Glib::Mutex::Lock ls (_swap_mutex);
+
+        while (1) {
+                if (_setup_chain != _pending_chain) {
+
+                        for (node_list_t::iterator ni=_nodes_rt[_setup_chain].begin(); ni!=_nodes_rt[_setup_chain].end(); ni++) {
+                                (*ni)->_activation_set[_setup_chain].clear();
+                        }
+
+                        _nodes_rt[_setup_chain].clear ();
+                        _init_trigger_list[_setup_chain].clear ();
+                        break;
+                }
+                /* setup chain == pending chain - we have
+                   to wait till this is no longer true.
+                */
+                _cleanup_cond.wait (_swap_mutex);                
+        }
+}
+
 void
 Graph::prep()
 {
         node_list_t::iterator i;
         int chain;
 
-        if (pthread_mutex_trylock (&_swap_mutex) == 0) {
+        if (_swap_mutex.trylock()) {
                 // we got the swap mutex.
                 if (_current_chain != _pending_chain)
                 {
-                        //printf ("chain swap ! %d -> %d\n", _current_chain, _pending_chain);
+                        // printf ("chain swap ! %d -> %d\n", _current_chain, _pending_chain);
                         _setup_chain = _current_chain;
                         _current_chain = _pending_chain;
+                        _cleanup_cond.signal ();
                 }
-                pthread_mutex_unlock (&_swap_mutex);
+                _swap_mutex.unlock ();
         }
 
         chain = _current_chain;
@@ -219,8 +243,8 @@ void
 Graph::rechain (boost::shared_ptr<RouteList> routelist)
 {
         node_list_t::iterator ni;
+        Glib::Mutex::Lock ls (_swap_mutex);
 
-        pthread_mutex_lock (&_swap_mutex);
         int chain = _setup_chain;
         DEBUG_TRACE (DEBUG::Graph, string_compose ("============== setup %1\n", chain));
         // set all refcounts to 0;
@@ -278,10 +302,8 @@ Graph::rechain (boost::shared_ptr<RouteList> routelist)
 
         _pending_chain = chain;
         dump(chain);
-        pthread_mutex_unlock (&_swap_mutex);
 }
 
-
 bool
 Graph::run_one()
 {
@@ -295,7 +317,7 @@ Graph::run_one()
                 to_run = 0;
         }
 
-        int wakeup = std::min ((int) _execution_tokens, (int) _trigger_queue.size());
+        int wakeup = min ((int) _execution_tokens, (int) _trigger_queue.size());
         _execution_tokens -= wakeup;
 
         for (int i=0; i<wakeup; i++ ) {
@@ -372,7 +394,7 @@ Graph::main_thread()
 
         this->prep();
 
-        if (_graph_empty) {
+        if (_graph_empty && !_quit_threads) {
                 sem_post (&_callback_done_sem);
                 goto again;
         }