2 Copyright (C) 2010 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "pbd/compose.h"
24 #include "pbd/debug_rt_alloc.h"
25 #include "pbd/pthread_utils.h"
27 #include "ardour/debug.h"
28 #include "ardour/graph.h"
29 #include "ardour/types.h"
30 #include "ardour/session.h"
31 #include "ardour/route.h"
32 #include "ardour/process_thread.h"
33 #include "ardour/audioengine.h"
37 using namespace ARDOUR;
42 static Graph* graph = 0;
48 return !graph->in_process_thread ();
54 Graph::Graph (Session & session)
55 : SessionHandleRef (session)
56 , _threads_active (false)
57 , _execution_sem ("graph_execution", 0)
58 , _callback_start_sem ("graph_start", 0)
59 , _callback_done_sem ("graph_done", 0)
60 , _cleanup_sem ("graph_cleanup", 0)
62 pthread_mutex_init( &_trigger_mutex, NULL);
64 /* XXX: rather hacky `fix' to stop _trigger_queue.push_back() allocating
65 memory in the RT thread.
67 _trigger_queue.reserve (8192);
69 _execution_tokens = 0;
77 ARDOUR::AudioEngine::instance()->Running.connect_same_thread (engine_connections, boost::bind (&Graph::reset_thread_list, this));
78 ARDOUR::AudioEngine::instance()->Stopped.connect_same_thread (engine_connections, boost::bind (&Graph::engine_stopped, this));
79 ARDOUR::AudioEngine::instance()->Halted.connect_same_thread (engine_connections, boost::bind (&Graph::engine_stopped, this));
85 pbd_alloc_allowed = &::alloc_allowed;
90 Graph::engine_stopped ()
92 if (AudioEngine::instance()->process_thread_count() != 0) {
97 /** Set up threads for running the graph */
99 Graph::reset_thread_list ()
101 uint32_t num_threads = how_many_dsp_threads ();
103 /* For now, we shouldn't be using the graph code if we only have 1 DSP thread */
104 assert (num_threads > 1);
106 /* don't bother doing anything here if we already have the right
110 if (AudioEngine::instance()->process_thread_count() == num_threads) {
114 Glib::Threads::Mutex::Lock lm (_session.engine().process_lock());
116 if (AudioEngine::instance()->process_thread_count() != 0) {
120 if (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::main_thread, this)) != 0) {
121 throw failed_constructor ();
124 for (uint32_t i = 1; i < num_threads; ++i) {
125 if (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::helper_thread, this))) {
126 throw failed_constructor ();
129 _threads_active = true;
133 Graph::session_going_away()
137 // now drop all references on the nodes.
138 _nodes_rt[0].clear();
139 _nodes_rt[1].clear();
140 _init_trigger_list[0].clear();
141 _init_trigger_list[1].clear();
142 _trigger_queue.clear();
146 Graph::drop_threads ()
148 _threads_active = false;
150 uint32_t thread_count = AudioEngine::instance()->process_thread_count ();
152 for (unsigned int i=0; i < thread_count; i++) {
153 _execution_sem.signal ();
156 _callback_start_sem.signal ();
158 AudioEngine::instance()->join_process_threads ();
160 _execution_tokens = 0;
164 Graph::clear_other_chain ()
166 Glib::Threads::Mutex::Lock ls (_swap_mutex);
169 if (_setup_chain != _pending_chain) {
171 for (node_list_t::iterator ni=_nodes_rt[_setup_chain].begin(); ni!=_nodes_rt[_setup_chain].end(); ni++) {
172 (*ni)->_activation_set[_setup_chain].clear();
175 _nodes_rt[_setup_chain].clear ();
176 _init_trigger_list[_setup_chain].clear ();
179 /* setup chain == pending chain - we have
180 to wait till this is no longer true.
182 _cleanup_cond.wait (_swap_mutex);
189 node_list_t::iterator i;
192 if (_swap_mutex.trylock()) {
193 // we got the swap mutex.
194 if (_current_chain != _pending_chain)
196 // printf ("chain swap ! %d -> %d\n", _current_chain, _pending_chain);
197 _setup_chain = _current_chain;
198 _current_chain = _pending_chain;
199 _cleanup_cond.signal ();
201 _swap_mutex.unlock ();
204 chain = _current_chain;
207 for (i=_nodes_rt[chain].begin(); i!=_nodes_rt[chain].end(); i++) {
209 _graph_empty = false;
211 _finished_refcount = _init_finished_refcount[chain];
213 /* Trigger the initial nodes for processing, which are the ones at the `input' end */
214 pthread_mutex_lock (&_trigger_mutex);
215 for (i=_init_trigger_list[chain].begin(); i!=_init_trigger_list[chain].end(); i++) {
216 /* don't use ::trigger here, as we have already locked the mutex */
217 _trigger_queue.push_back (i->get ());
219 pthread_mutex_unlock (&_trigger_mutex);
223 Graph::trigger (GraphNode* n)
225 pthread_mutex_lock (&_trigger_mutex);
226 _trigger_queue.push_back (n);
227 pthread_mutex_unlock (&_trigger_mutex);
230 /** Called when a node at the `output' end of the chain (ie one that has no-one to feed)
236 if (g_atomic_int_dec_and_test (const_cast<gint*> (&_finished_refcount))) {
238 /* We have run all the nodes that are at the `output' end of
239 the graph, so there is nothing more to do this time around.
247 Graph::restart_cycle()
249 // we are through. wakeup our caller.
252 _callback_done_sem.signal ();
254 /* Block until the a process callback triggers us */
255 _callback_start_sem.wait();
257 if (!_threads_active) {
267 // returning will restart the cycle.
268 // starting with waking up the others.
271 /** Rechain our stuff using a list of routes (which can be in any order) and
272 * a directed graph of their interconnections, which is guaranteed to be
277 Graph::rechain (boost::shared_ptr<RouteList> routelist, GraphEdges const & edges)
279 Glib::Threads::Mutex::Lock ls (_swap_mutex);
281 int chain = _setup_chain;
282 DEBUG_TRACE (DEBUG::Graph, string_compose ("============== setup %1\n", chain));
284 /* This will become the number of nodes that do not feed any other node;
285 once we have processed this number of those nodes, we have finished.
287 _init_finished_refcount[chain] = 0;
289 /* This will become a list of nodes that are not fed by another node, ie
290 those at the `input' end.
292 _init_trigger_list[chain].clear();
294 _nodes_rt[chain].clear();
296 /* Clear things out, and make _nodes_rt[chain] a copy of routelist */
297 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
298 (*ri)->_init_refcount[chain] = 0;
299 (*ri)->_activation_set[chain].clear();
300 _nodes_rt[chain].push_back (*ri);
303 // now add refs for the connections.
305 for (node_list_t::iterator ni = _nodes_rt[chain].begin(); ni != _nodes_rt[chain].end(); ni++) {
307 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (*ni);
309 /* The routes that are directly fed by r */
310 set<GraphVertex> fed_from_r = edges.from (r);
312 /* Hence whether r has an output */
313 bool const has_output = !fed_from_r.empty ();
315 /* Set up r's activation set */
316 for (set<GraphVertex>::iterator i = fed_from_r.begin(); i != fed_from_r.end(); ++i) {
317 r->_activation_set[chain].insert (*i);
320 /* r has an input if there are some incoming edges to r in the graph */
321 bool const has_input = !edges.has_none_to (r);
323 /* Increment the refcount of any route that we directly feed */
324 for (node_set_t::iterator ai = r->_activation_set[chain].begin(); ai != r->_activation_set[chain].end(); ai++) {
325 (*ai)->_init_refcount[chain] += 1;
329 /* no input, so this node needs to be triggered initially to get things going */
330 _init_trigger_list[chain].push_back (*ni);
334 /* no output, so this is one of the nodes that we can count off to decide
337 _init_finished_refcount[chain] += 1;
341 _pending_chain = chain;
345 /** Called by both the main thread and all helpers.
346 * @return true to quit, false to carry on.
353 pthread_mutex_lock (&_trigger_mutex);
354 if (_trigger_queue.size()) {
355 to_run = _trigger_queue.back();
356 _trigger_queue.pop_back();
361 /* the number of threads that are asleep */
362 int et = _execution_tokens;
363 /* the number of nodes that need to be run */
364 int ts = _trigger_queue.size();
366 /* hence how many threads to wake up */
367 int wakeup = min (et, ts);
368 /* update the number of threads that will still be sleeping */
369 _execution_tokens -= wakeup;
371 DEBUG_TRACE(DEBUG::ProcessThreads, string_compose ("%1 signals %2\n", pthread_name(), wakeup));
373 for (int i = 0; i < wakeup; i++) {
374 _execution_sem.signal ();
377 while (to_run == 0) {
378 _execution_tokens += 1;
379 pthread_mutex_unlock (&_trigger_mutex);
380 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 goes to sleep\n", pthread_name()));
381 _execution_sem.wait ();
382 if (!_threads_active) {
385 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 is awake\n", pthread_name()));
386 pthread_mutex_lock (&_trigger_mutex);
387 if (_trigger_queue.size()) {
388 to_run = _trigger_queue.back();
389 _trigger_queue.pop_back();
392 pthread_mutex_unlock (&_trigger_mutex);
395 to_run->finish (_current_chain);
397 DEBUG_TRACE(DEBUG::ProcessThreads, string_compose ("%1 has finished run_one()\n", pthread_name()));
403 Graph::helper_thread()
405 suspend_rt_malloc_checks ();
406 ProcessThread* pt = new ProcessThread ();
407 resume_rt_malloc_checks ();
420 /** Here's the main graph thread */
424 suspend_rt_malloc_checks ();
425 ProcessThread* pt = new ProcessThread ();
426 resume_rt_malloc_checks ();
431 _callback_start_sem.wait ();
433 DEBUG_TRACE(DEBUG::ProcessThreads, "main thread is awake\n");
435 if (!_threads_active) {
441 if (_graph_empty && _threads_active) {
442 _callback_done_sem.signal ();
443 DEBUG_TRACE(DEBUG::ProcessThreads, "main thread sees graph done, goes back to sleep\n");
447 /* This loop will run forever */
449 DEBUG_TRACE(DEBUG::ProcessThreads, "main thread runs one graph node\n");
459 Graph::dump (int chain)
462 node_list_t::iterator ni;
463 node_set_t::iterator ai;
465 chain = _pending_chain;
467 DEBUG_TRACE (DEBUG::Graph, "--------------------------------------------Graph dump:\n");
468 for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) {
469 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni);
470 DEBUG_TRACE (DEBUG::Graph, string_compose ("GraphNode: %1 refcount: %2\n", rp->name().c_str(), (*ni)->_init_refcount[chain]));
471 for (ai=(*ni)->_activation_set[chain].begin(); ai!=(*ni)->_activation_set[chain].end(); ai++) {
472 DEBUG_TRACE (DEBUG::Graph, string_compose (" triggers: %1\n", boost::dynamic_pointer_cast<Route>(*ai)->name().c_str()));
476 DEBUG_TRACE (DEBUG::Graph, "------------- trigger list:\n");
477 for (ni=_init_trigger_list[chain].begin(); ni!=_init_trigger_list[chain].end(); ni++) {
478 DEBUG_TRACE (DEBUG::Graph, string_compose ("GraphNode: %1 refcount: %2\n", boost::dynamic_pointer_cast<Route>(*ni)->name().c_str(), (*ni)->_init_refcount[chain]));
481 DEBUG_TRACE (DEBUG::Graph, string_compose ("final activation refcount: %1\n", _init_finished_refcount[chain]));
486 Graph::silent_process_routes (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, bool& need_butler)
488 if (!_threads_active) return 0;
490 _process_nframes = nframes;
491 _process_start_frame = start_frame;
492 _process_end_frame = end_frame;
494 _process_silent = true;
495 _process_noroll = false;
497 _process_need_butler = false;
500 DEBUG_TRACE(DEBUG::ProcessThreads, "wake graph for silent process\n");
501 _callback_start_sem.signal ();
502 _callback_done_sem.wait ();
505 need_butler = _process_need_butler;
507 return _process_retval;
511 Graph::process_routes (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler)
513 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("graph execution from %1 to %2 = %3\n", start_frame, end_frame, nframes));
515 if (!_threads_active) return 0;
517 _process_nframes = nframes;
518 _process_start_frame = start_frame;
519 _process_end_frame = end_frame;
520 _process_declick = declick;
522 _process_silent = false;
523 _process_noroll = false;
525 _process_need_butler = false;
527 DEBUG_TRACE(DEBUG::ProcessThreads, "wake graph for non-silent process\n");
528 _callback_start_sem.signal ();
529 _callback_done_sem.wait ();
531 DEBUG_TRACE (DEBUG::ProcessThreads, "graph execution complete\n");
533 need_butler = _process_need_butler;
535 return _process_retval;
539 Graph::routes_no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
540 bool non_rt_pending, int declick)
542 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("no-roll graph execution from %1 to %2 = %3\n", start_frame, end_frame, nframes));
544 if (!_threads_active) return 0;
546 _process_nframes = nframes;
547 _process_start_frame = start_frame;
548 _process_end_frame = end_frame;
549 _process_declick = declick;
550 _process_non_rt_pending = non_rt_pending;
552 _process_silent = false;
553 _process_noroll = true;
555 _process_need_butler = false;
557 DEBUG_TRACE(DEBUG::ProcessThreads, "wake graph for no-roll process\n");
558 _callback_start_sem.signal ();
559 _callback_done_sem.wait ();
561 return _process_retval;
564 Graph::process_one_route (Route* route)
566 bool need_butler = false;
571 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 runs route %2\n", pthread_name(), route->name()));
573 if (_process_silent) {
574 retval = route->silent_roll (_process_nframes, _process_start_frame, _process_end_frame, need_butler);
575 } else if (_process_noroll) {
576 route->set_pending_declick (_process_declick);
577 retval = route->no_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_non_rt_pending);
579 route->set_pending_declick (_process_declick);
580 retval = route->roll (_process_nframes, _process_start_frame, _process_end_frame, _process_declick, need_butler);
584 _process_retval = retval;
588 _process_need_butler = true;
593 Graph::in_process_thread () const
595 return AudioEngine::instance()->in_process_thread ();