#include "ardour/process_thread.h"
#include "ardour/audioengine.h"
-#include <jack/thread.h>
-
#include "i18n.h"
using namespace ARDOUR;
Graph::Graph (Session & session)
: SessionHandleRef (session)
- , _quit_threads (false)
+ , _threads_active (false)
, _execution_sem ("graph_execution", 0)
, _callback_start_sem ("graph_start", 0)
, _callback_done_sem ("graph_done", 0)
_current_chain = 0;
_pending_chain = 0;
_setup_chain = 1;
- _quit_threads = false;
_graph_empty = true;
+
+ ARDOUR::AudioEngine::instance()->Running.connect_same_thread (engine_connections, boost::bind (&Graph::reset_thread_list, this));
+ ARDOUR::AudioEngine::instance()->Stopped.connect_same_thread (engine_connections, boost::bind (&Graph::engine_stopped, this));
+ ARDOUR::AudioEngine::instance()->Halted.connect_same_thread (engine_connections, boost::bind (&Graph::engine_stopped, this));
+
reset_thread_list ();
#ifdef DEBUG_RT_ALLOC
#endif
}
+void
+Graph::engine_stopped ()
+{
+ if (AudioEngine::instance()->process_thread_count() != 0) {
+ drop_threads ();
+ }
+}
+
/** Set up threads for running the graph */
void
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 ();
}
void
Graph::drop_threads ()
{
- _quit_threads = true;
+ 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 ();
_execution_tokens = 0;
-
- _quit_threads = false;
}
void
/* Block until the a process callback triggers us */
_callback_start_sem.wait();
- if (_quit_threads) {
+ if (!_threads_active) {
return;
}
prep ();
- if (_graph_empty) {
+ if (_graph_empty && _threads_active) {
goto again;
}
pthread_mutex_unlock (&_trigger_mutex);
DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 goes to sleep\n", pthread_name()));
_execution_sem.wait ();
- if (_quit_threads) {
+ if (!_threads_active) {
return true;
}
DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 is awake\n", pthread_name()));
DEBUG_TRACE(DEBUG::ProcessThreads, string_compose ("%1 has finished run_one()\n", pthread_name()));
- return false;
+ return !_threads_active;
}
void
DEBUG_TRACE(DEBUG::ProcessThreads, "main thread is awake\n");
- if (_quit_threads) {
+ if (!_threads_active) {
return;
}
prep ();
- if (_graph_empty && !_quit_threads) {
+ 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;
}
pt->drop_buffers();
+ delete (pt);
}
void
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;
{
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;
{
DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("no-roll 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;