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.
22 #include <xmmintrin.h>
24 #include "pbd/compose.h"
25 #include "pbd/debug_rt_alloc.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"
35 #include <jack/thread.h>
39 using namespace ARDOUR;
44 static Graph* graph = 0;
50 return !graph->in_process_thread ();
56 Graph::Graph (Session & session)
57 : SessionHandleRef (session)
58 , _quit_threads (false)
59 , _execution_sem ("graph_execution", 0)
60 , _callback_start_sem ("graph_start", 0)
61 , _callback_done_sem ("graph_done", 0)
62 , _cleanup_sem ("graph_cleanup", 0)
64 pthread_mutex_init( &_trigger_mutex, NULL);
66 /* XXX: rather hacky `fix' to stop _trigger_queue.push_back() allocating
67 memory in the RT thread.
69 _trigger_queue.reserve (8192);
71 _execution_tokens = 0;
76 _quit_threads = false;
83 pbd_alloc_allowed = &::alloc_allowed;
87 /** Set up threads for running the graph */
89 Graph::reset_thread_list ()
91 uint32_t num_threads = how_many_dsp_threads ();
93 /* For now, we shouldn't be using the graph code if we only have 1 DSP thread */
94 assert (num_threads > 1);
96 /* don't bother doing anything here if we already have the right
100 if (_thread_list.size() == num_threads) {
104 Glib::Threads::Mutex::Lock lm (_session.engine().process_lock());
107 if (!_thread_list.empty()) {
111 if (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::main_thread, this), &a_thread, 100000) != 0) {
112 throw failed_constructor ();
115 _thread_list.push_back (a_thread);
117 for (uint32_t i = 1; i < num_threads; ++i) {
118 if (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::helper_thread, this), &a_thread, 100000) != 0) {
119 throw failed_constructor ();
122 _thread_list.push_back (a_thread);
127 Graph::session_going_away()
131 // now drop all references on the nodes.
132 _nodes_rt[0].clear();
133 _nodes_rt[1].clear();
134 _init_trigger_list[0].clear();
135 _init_trigger_list[1].clear();
136 _trigger_queue.clear();
140 Graph::drop_threads ()
142 _quit_threads = true;
144 for (unsigned int i=0; i< _thread_list.size(); i++) {
145 _execution_sem.signal ();
148 _callback_start_sem.signal ();
150 for (list<pthread_t>::iterator i = _thread_list.begin(); i != _thread_list.end(); ++i) {
152 pthread_join (*i, &status);
155 _thread_list.clear ();
157 _execution_tokens = 0;
159 _quit_threads = false;
163 Graph::clear_other_chain ()
165 Glib::Threads::Mutex::Lock ls (_swap_mutex);
168 if (_setup_chain != _pending_chain) {
170 for (node_list_t::iterator ni=_nodes_rt[_setup_chain].begin(); ni!=_nodes_rt[_setup_chain].end(); ni++) {
171 (*ni)->_activation_set[_setup_chain].clear();
174 _nodes_rt[_setup_chain].clear ();
175 _init_trigger_list[_setup_chain].clear ();
178 /* setup chain == pending chain - we have
179 to wait till this is no longer true.
181 _cleanup_cond.wait (_swap_mutex);
188 node_list_t::iterator i;
191 if (_swap_mutex.trylock()) {
192 // we got the swap mutex.
193 if (_current_chain != _pending_chain)
195 // printf ("chain swap ! %d -> %d\n", _current_chain, _pending_chain);
196 _setup_chain = _current_chain;
197 _current_chain = _pending_chain;
198 _cleanup_cond.signal ();
200 _swap_mutex.unlock ();
203 chain = _current_chain;
206 for (i=_nodes_rt[chain].begin(); i!=_nodes_rt[chain].end(); i++) {
208 _graph_empty = false;
210 _finished_refcount = _init_finished_refcount[chain];
212 /* Trigger the initial nodes for processing, which are the ones at the `input' end */
213 pthread_mutex_lock (&_trigger_mutex);
214 for (i=_init_trigger_list[chain].begin(); i!=_init_trigger_list[chain].end(); i++) {
215 /* don't use ::trigger here, as we have already locked the mutex */
216 _trigger_queue.push_back (i->get ());
218 pthread_mutex_unlock (&_trigger_mutex);
222 Graph::trigger (GraphNode* n)
224 pthread_mutex_lock (&_trigger_mutex);
225 _trigger_queue.push_back (n);
226 pthread_mutex_unlock (&_trigger_mutex);
229 /** Called when a node at the `output' end of the chain (ie one that has no-one to feed)
235 if (g_atomic_int_dec_and_test (const_cast<gint*> (&_finished_refcount))) {
237 /* We have run all the nodes that are at the `output' end of
238 the graph, so there is nothing more to do this time around.
246 Graph::restart_cycle()
248 // we are through. wakeup our caller.
251 _callback_done_sem.signal ();
253 /* Block until the a process callback triggers us */
254 _callback_start_sem.wait();
266 // returning will restart the cycle.
267 // starting with waking up the others.
270 /** Rechain our stuff using a list of routes (which can be in any order) and
271 * a directed graph of their interconnections, which is guaranteed to be
276 Graph::rechain (boost::shared_ptr<RouteList> routelist, GraphEdges const & edges)
278 Glib::Threads::Mutex::Lock ls (_swap_mutex);
280 int chain = _setup_chain;
281 DEBUG_TRACE (DEBUG::Graph, string_compose ("============== setup %1\n", chain));
283 /* This will become the number of nodes that do not feed any other node;
284 once we have processed this number of those nodes, we have finished.
286 _init_finished_refcount[chain] = 0;
288 /* This will become a list of nodes that are not fed by another node, ie
289 those at the `input' end.
291 _init_trigger_list[chain].clear();
293 _nodes_rt[chain].clear();
295 /* Clear things out, and make _nodes_rt[chain] a copy of routelist */
296 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
297 (*ri)->_init_refcount[chain] = 0;
298 (*ri)->_activation_set[chain].clear();
299 _nodes_rt[chain].push_back (*ri);
302 // now add refs for the connections.
304 for (node_list_t::iterator ni = _nodes_rt[chain].begin(); ni != _nodes_rt[chain].end(); ni++) {
306 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (*ni);
308 /* The routes that are directly fed by r */
309 set<GraphVertex> fed_from_r = edges.from (r);
311 /* Hence whether r has an output */
312 bool const has_output = !fed_from_r.empty ();
314 /* Set up r's activation set */
315 for (set<GraphVertex>::iterator i = fed_from_r.begin(); i != fed_from_r.end(); ++i) {
316 r->_activation_set[chain].insert (*i);
319 /* r has an input if there are some incoming edges to r in the graph */
320 bool const has_input = !edges.has_none_to (r);
322 /* Increment the refcount of any route that we directly feed */
323 for (node_set_t::iterator ai = r->_activation_set[chain].begin(); ai != r->_activation_set[chain].end(); ai++) {
324 (*ai)->_init_refcount[chain] += 1;
328 /* no input, so this node needs to be triggered initially to get things going */
329 _init_trigger_list[chain].push_back (*ni);
333 /* no output, so this is one of the nodes that we can count off to decide
336 _init_finished_refcount[chain] += 1;
340 _pending_chain = chain;
344 /** Called by both the main thread and all helpers.
345 * @return true to quit, false to carry on.
352 pthread_mutex_lock (&_trigger_mutex);
353 if (_trigger_queue.size()) {
354 to_run = _trigger_queue.back();
355 _trigger_queue.pop_back();
360 /* the number of threads that are asleep */
361 int et = _execution_tokens;
362 /* the number of nodes that need to be run */
363 int ts = _trigger_queue.size();
365 /* hence how many threads to wake up */
366 int wakeup = min (et, ts);
367 /* update the number of threads that will still be sleeping */
368 _execution_tokens -= wakeup;
370 DEBUG_TRACE(DEBUG::ProcessThreads, string_compose ("%1 signals %2\n", pthread_self(), wakeup));
372 for (int i = 0; i < wakeup; i++) {
373 _execution_sem.signal ();
376 while (to_run == 0) {
377 _execution_tokens += 1;
378 pthread_mutex_unlock (&_trigger_mutex);
379 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 goes to sleep\n", pthread_self()));
380 _execution_sem.wait ();
384 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 is awake\n", pthread_self()));
385 pthread_mutex_lock (&_trigger_mutex);
386 if (_trigger_queue.size()) {
387 to_run = _trigger_queue.back();
388 _trigger_queue.pop_back();
391 pthread_mutex_unlock (&_trigger_mutex);
394 to_run->finish (_current_chain);
396 DEBUG_TRACE(DEBUG::ProcessThreads, string_compose ("%1 has finished run_one()\n", pthread_self()));
402 Graph::helper_thread()
404 suspend_rt_malloc_checks ();
405 ProcessThread* pt = new ProcessThread ();
406 resume_rt_malloc_checks ();
419 /** Here's the main graph thread */
423 suspend_rt_malloc_checks ();
424 ProcessThread* pt = new ProcessThread ();
425 resume_rt_malloc_checks ();
430 _callback_start_sem.wait ();
432 DEBUG_TRACE(DEBUG::ProcessThreads, "main thread is awake\n");
440 if (_graph_empty && !_quit_threads) {
441 _callback_done_sem.signal ();
442 DEBUG_TRACE(DEBUG::ProcessThreads, "main thread sees graph done, goes back to sleep\n");
446 /* This loop will run forever */
448 DEBUG_TRACE(DEBUG::ProcessThreads, "main thread runs one graph node\n");
458 Graph::dump (int chain)
461 node_list_t::iterator ni;
462 node_set_t::iterator ai;
464 chain = _pending_chain;
466 DEBUG_TRACE (DEBUG::Graph, "--------------------------------------------Graph dump:\n");
467 for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) {
468 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni);
469 DEBUG_TRACE (DEBUG::Graph, string_compose ("GraphNode: %1 refcount: %2\n", rp->name().c_str(), (*ni)->_init_refcount[chain]));
470 for (ai=(*ni)->_activation_set[chain].begin(); ai!=(*ni)->_activation_set[chain].end(); ai++) {
471 DEBUG_TRACE (DEBUG::Graph, string_compose (" triggers: %1\n", boost::dynamic_pointer_cast<Route>(*ai)->name().c_str()));
475 DEBUG_TRACE (DEBUG::Graph, "------------- trigger list:\n");
476 for (ni=_init_trigger_list[chain].begin(); ni!=_init_trigger_list[chain].end(); ni++) {
477 DEBUG_TRACE (DEBUG::Graph, string_compose ("GraphNode: %1 refcount: %2\n", boost::dynamic_pointer_cast<Route>(*ni)->name().c_str(), (*ni)->_init_refcount[chain]));
480 DEBUG_TRACE (DEBUG::Graph, string_compose ("final activation refcount: %1\n", _init_finished_refcount[chain]));
485 Graph::silent_process_routes (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, bool& need_butler)
487 _process_nframes = nframes;
488 _process_start_frame = start_frame;
489 _process_end_frame = end_frame;
491 _process_silent = true;
492 _process_noroll = false;
494 _process_need_butler = false;
497 DEBUG_TRACE(DEBUG::ProcessThreads, "wake graph for silent process\n");
498 _callback_start_sem.signal ();
499 _callback_done_sem.wait ();
502 need_butler = _process_need_butler;
504 return _process_retval;
508 Graph::process_routes (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler)
510 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("graph execution from %1 to %2 = %3\n", start_frame, end_frame, nframes));
512 _process_nframes = nframes;
513 _process_start_frame = start_frame;
514 _process_end_frame = end_frame;
515 _process_declick = declick;
517 _process_silent = false;
518 _process_noroll = false;
520 _process_need_butler = false;
522 DEBUG_TRACE(DEBUG::ProcessThreads, "wake graph for non-silent process\n");
523 _callback_start_sem.signal ();
524 _callback_done_sem.wait ();
526 DEBUG_TRACE (DEBUG::ProcessThreads, "graph execution complete\n");
528 need_butler = _process_need_butler;
530 return _process_retval;
534 Graph::routes_no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
535 bool non_rt_pending, int declick)
537 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("no-roll graph execution from %1 to %2 = %3\n", start_frame, end_frame, nframes));
539 _process_nframes = nframes;
540 _process_start_frame = start_frame;
541 _process_end_frame = end_frame;
542 _process_declick = declick;
543 _process_non_rt_pending = non_rt_pending;
545 _process_silent = false;
546 _process_noroll = true;
548 _process_need_butler = false;
550 DEBUG_TRACE(DEBUG::ProcessThreads, "wake graph for no-roll process\n");
551 _callback_start_sem.signal ();
552 _callback_done_sem.wait ();
554 return _process_retval;
557 Graph::process_one_route (Route* route)
559 bool need_butler = false;
564 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 runs route %2\n", pthread_self(), route->name()));
566 if (_process_silent) {
567 retval = route->silent_roll (_process_nframes, _process_start_frame, _process_end_frame, need_butler);
568 } else if (_process_noroll) {
569 route->set_pending_declick (_process_declick);
570 retval = route->no_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_non_rt_pending);
572 route->set_pending_declick (_process_declick);
573 retval = route->roll (_process_nframes, _process_start_frame, _process_end_frame, _process_declick, need_butler);
577 _process_retval = retval;
581 _process_need_butler = true;
586 Graph::in_process_thread () const
588 for (list<pthread_t>::const_iterator i = _thread_list.begin (); i != _thread_list.end(); ++i) {
589 if (*i == pthread_self()) {