fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / graph.cc
index cc82fb6c5349a3ff3cde24adc64ec8aaff3b114b..563c882efec76706c6b6c2032a10e343ecc56af9 100644 (file)
@@ -32,7 +32,7 @@
 #include "ardour/process_thread.h"
 #include "ardour/audioengine.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace ARDOUR;
 using namespace PBD;
@@ -117,6 +117,8 @@ Graph::reset_thread_list ()
                 drop_threads ();
         }
 
+        _threads_active = true;
+
        if (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::main_thread, this)) != 0) {
                throw failed_constructor ();
        }
@@ -126,7 +128,6 @@ Graph::reset_thread_list ()
                        throw failed_constructor ();
                }
         }
-        _threads_active = true;
 }
 
 void
@@ -145,15 +146,20 @@ Graph::session_going_away()
 void
 Graph::drop_threads ()
 {
+        Glib::Threads::Mutex::Lock ls (_swap_mutex);
         _threads_active = false;
 
         uint32_t thread_count = AudioEngine::instance()->process_thread_count ();
 
         for (unsigned int i=0; i < thread_count; i++) {
+               pthread_mutex_lock (&_trigger_mutex);
                _execution_sem.signal ();
+               pthread_mutex_unlock (&_trigger_mutex);
         }
 
+       pthread_mutex_lock (&_trigger_mutex);
         _callback_start_sem.signal ();
+       pthread_mutex_unlock (&_trigger_mutex);
 
        AudioEngine::instance()->join_process_threads ();
 
@@ -260,7 +266,7 @@ Graph::restart_cycle()
 
        prep ();
 
-        if (_graph_empty) {
+        if (_graph_empty && _threads_active) {
                 goto again;
         }
 
@@ -396,7 +402,7 @@ Graph::run_one()
 
         DEBUG_TRACE(DEBUG::ProcessThreads, string_compose ("%1 has finished run_one()\n", pthread_name()));
 
-        return false;
+        return !_threads_active;
 }
 
 void
@@ -406,15 +412,16 @@ Graph::helper_thread()
        ProcessThread* pt = new ProcessThread ();
        resume_rt_malloc_checks ();
 
-        pt->get_buffers();
+       pt->get_buffers();
 
-        while(1) {
-                if (run_one()) {
-                        break;
-                }
-        }
+       while(1) {
+               if (run_one()) {
+                       break;
+               }
+       }
 
-        pt->drop_buffers();
+       pt->drop_buffers();
+       delete pt;
 }
 
 /** Here's the main graph thread */
@@ -425,34 +432,35 @@ Graph::main_thread()
        ProcessThread* pt = new ProcessThread ();
        resume_rt_malloc_checks ();
 
-        pt->get_buffers();
+       pt->get_buffers();
+
+again:
+       _callback_start_sem.wait ();
 
-  again:
-        _callback_start_sem.wait ();
-       
        DEBUG_TRACE(DEBUG::ProcessThreads, "main thread is awake\n");
 
-        if (!_threads_active) {
-                return;
-        }
+       if (!_threads_active) {
+               return;
+       }
 
        prep ();
 
-        if (_graph_empty && _threads_active) {
-                _callback_done_sem.signal ();
-                DEBUG_TRACE(DEBUG::ProcessThreads, "main thread sees graph done, goes back to sleep\n");
-                goto again;
-        }
+       if (_graph_empty && _threads_active) {
+               _callback_done_sem.signal ();
+               DEBUG_TRACE(DEBUG::ProcessThreads, "main thread sees graph done, goes back to sleep\n");
+               goto again;
+       }
 
        /* This loop will run forever */
-        while (1) {
+       while (1) {
                DEBUG_TRACE(DEBUG::ProcessThreads, "main thread runs one graph node\n");
-                if (run_one()) {
-                        break;
-                }
-        }
+               if (run_one()) {
+                       break;
+               }
+       }
 
-        pt->drop_buffers();
+       pt->drop_buffers();
+       delete (pt);
 }
 
 void
@@ -485,6 +493,8 @@ Graph::dump (int chain)
 int
 Graph::silent_process_routes (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, bool& need_butler)
 {
+       if (!_threads_active) return 0;
+
         _process_nframes = nframes;
         _process_start_frame = start_frame;
         _process_end_frame = end_frame;
@@ -510,6 +520,8 @@ Graph::process_routes (pframes_t nframes, framepos_t start_frame, framepos_t end
 {
        DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("graph execution from %1 to %2 = %3\n", start_frame, end_frame, nframes));
 
+       if (!_threads_active) return 0;
+
         _process_nframes = nframes;
         _process_start_frame = start_frame;
         _process_end_frame = end_frame;