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"
26 #include "ardour/debug.h"
27 #include "ardour/graph.h"
28 #include "ardour/types.h"
29 #include "ardour/session.h"
30 #include "ardour/route.h"
31 #include "ardour/process_thread.h"
32 #include "ardour/audioengine.h"
34 #include <jack/thread.h>
38 using namespace ARDOUR;
43 static Graph* graph = 0;
49 return !graph->in_process_thread ();
55 Graph::Graph (Session & session)
56 : SessionHandleRef (session)
57 , _quit_threads (false)
58 , _execution_sem ("graph_execution", 0)
59 , _callback_start_sem ("graph_start", 0)
60 , _callback_done_sem ("graph_done", 0)
61 , _cleanup_sem ("graph_cleanup", 0)
63 pthread_mutex_init( &_trigger_mutex, NULL);
65 /* XXX: rather hacky `fix' to stop _trigger_queue.push_back() allocating
66 memory in the RT thread.
68 _trigger_queue.reserve (8192);
70 _execution_tokens = 0;
75 _quit_threads = false;
80 Config->ParameterChanged.connect_same_thread (processor_usage_connection, boost::bind (&Graph::parameter_changed, this, _1));
84 pbd_alloc_allowed = &::alloc_allowed;
89 Graph::parameter_changed (std::string param)
91 if (param == X_("processor-usage")) {
96 /** Set up threads for running the graph */
98 Graph::reset_thread_list ()
100 uint32_t num_threads = how_many_dsp_threads ();
102 /* don't bother doing anything here if we already have the right
106 if (_thread_list.size() == num_threads) {
110 Glib::Mutex::Lock lm (_session.engine().process_lock());
113 if (!_thread_list.empty()) {
118 /* XXX this only makes sense when we can use just the AudioEngine thread
119 and still keep the graph current with the route list
121 if (num_threads <= 1) {
122 /* no point creating 1 thread - the AudioEngine already gives us one
127 if (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::main_thread, this), &a_thread, 100000) == 0) {
128 _thread_list.push_back (a_thread);
131 for (uint32_t i = 1; i < num_threads; ++i) {
132 if (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::helper_thread, this), &a_thread, 100000) == 0) {
133 _thread_list.push_back (a_thread);
139 Graph::session_going_away()
143 // now drop all references on the nodes.
144 _nodes_rt[0].clear();
145 _nodes_rt[1].clear();
146 _init_trigger_list[0].clear();
147 _init_trigger_list[1].clear();
148 _trigger_queue.clear();
152 Graph::drop_threads ()
154 _quit_threads = true;
156 for (unsigned int i=0; i< _thread_list.size(); i++) {
157 _execution_sem.signal ();
160 _callback_start_sem.signal ();
162 for (list<pthread_t>::iterator i = _thread_list.begin(); i != _thread_list.end(); ++i) {
164 pthread_join (*i, &status);
167 _thread_list.clear ();
169 _execution_tokens = 0;
171 _quit_threads = false;
175 Graph::clear_other_chain ()
177 Glib::Mutex::Lock ls (_swap_mutex);
180 if (_setup_chain != _pending_chain) {
182 for (node_list_t::iterator ni=_nodes_rt[_setup_chain].begin(); ni!=_nodes_rt[_setup_chain].end(); ni++) {
183 (*ni)->_activation_set[_setup_chain].clear();
186 _nodes_rt[_setup_chain].clear ();
187 _init_trigger_list[_setup_chain].clear ();
190 /* setup chain == pending chain - we have
191 to wait till this is no longer true.
193 _cleanup_cond.wait (_swap_mutex);
200 node_list_t::iterator i;
203 if (_swap_mutex.trylock()) {
204 // we got the swap mutex.
205 if (_current_chain != _pending_chain)
207 // printf ("chain swap ! %d -> %d\n", _current_chain, _pending_chain);
208 _setup_chain = _current_chain;
209 _current_chain = _pending_chain;
210 _cleanup_cond.signal ();
212 _swap_mutex.unlock ();
215 chain = _current_chain;
218 for (i=_nodes_rt[chain].begin(); i!=_nodes_rt[chain].end(); i++) {
220 _graph_empty = false;
222 _finished_refcount = _init_finished_refcount[chain];
224 /* Trigger the initial nodes for processing, which are the ones at the `input' end */
225 pthread_mutex_lock (&_trigger_mutex);
226 for (i=_init_trigger_list[chain].begin(); i!=_init_trigger_list[chain].end(); i++) {
227 /* don't use ::trigger here, as we have already locked the mutex */
228 _trigger_queue.push_back (i->get ());
230 pthread_mutex_unlock (&_trigger_mutex);
234 Graph::trigger (GraphNode* n)
236 pthread_mutex_lock (&_trigger_mutex);
237 _trigger_queue.push_back (n);
238 pthread_mutex_unlock (&_trigger_mutex);
241 /** Called when a node at the `output' end of the chain (ie one that has no-one to feed)
247 if (g_atomic_int_dec_and_test (&_finished_refcount)) {
249 /* We have run all the nodes that are at the `output' end of
250 the graph, so there is nothing more to do this time around.
258 Graph::restart_cycle()
260 // we are through. wakeup our caller.
263 _callback_done_sem.signal ();
265 /* Block until the a process callback triggers us */
266 _callback_start_sem.wait();
278 // returning will restart the cycle.
279 // starting with waking up the others.
282 /** Rechain our stuff using a list of routes (which can be in any order) and
283 * a directed graph of their interconnections, which is guaranteed to be
288 Graph::rechain (boost::shared_ptr<RouteList> routelist, GraphEdges const & edges)
290 Glib::Mutex::Lock ls (_swap_mutex);
292 int chain = _setup_chain;
293 DEBUG_TRACE (DEBUG::Graph, string_compose ("============== setup %1\n", chain));
295 /* This will become the number of nodes that do not feed any other node;
296 once we have processed this number of those nodes, we have finished.
298 _init_finished_refcount[chain] = 0;
300 /* This will become a list of nodes that are not fed by another node, ie
301 those at the `input' end.
303 _init_trigger_list[chain].clear();
305 _nodes_rt[chain].clear();
307 /* Clear things out, and make _nodes_rt[chain] a copy of routelist */
308 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
309 (*ri)->_init_refcount[chain] = 0;
310 (*ri)->_activation_set[chain].clear();
311 _nodes_rt[chain].push_back (*ri);
314 // now add refs for the connections.
316 for (node_list_t::iterator ni = _nodes_rt[chain].begin(); ni != _nodes_rt[chain].end(); ni++) {
318 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (*ni);
320 /* The routes that are directly fed by r */
321 set<GraphVertex> fed_from_r = edges.from (r);
323 /* Hence whether r has an output */
324 bool const has_output = !fed_from_r.empty ();
326 /* Set up r's activation set */
327 for (set<GraphVertex>::iterator i = fed_from_r.begin(); i != fed_from_r.end(); ++i) {
328 r->_activation_set[chain].insert (*i);
331 /* r has an input if there are some incoming edges to r in the graph */
332 bool const has_input = !edges.has_none_to (r);
334 /* Increment the refcount of any route that we directly feed */
335 for (node_set_t::iterator ai = r->_activation_set[chain].begin(); ai != r->_activation_set[chain].end(); ai++) {
336 (*ai)->_init_refcount[chain] += 1;
340 /* no input, so this node needs to be triggered initially to get things going */
341 _init_trigger_list[chain].push_back (*ni);
345 /* no output, so this is one of the nodes that we can count off to decide
348 _init_finished_refcount[chain] += 1;
352 _pending_chain = chain;
356 /** Called by both the main thread and all helpers.
357 * @return true to quit, false to carry on.
364 pthread_mutex_lock (&_trigger_mutex);
365 if (_trigger_queue.size()) {
366 to_run = _trigger_queue.back();
367 _trigger_queue.pop_back();
372 /* the number of threads that are asleep */
373 int et = _execution_tokens;
374 /* the number of nodes that need to be run */
375 int ts = _trigger_queue.size();
377 /* hence how many threads to wake up */
378 int wakeup = min (et, ts);
379 /* update the number of threads that will still be sleeping */
380 _execution_tokens -= wakeup;
382 DEBUG_TRACE(DEBUG::ProcessThreads, string_compose ("%1 signals %2\n", pthread_self(), wakeup));
384 for (int i = 0; i < wakeup; i++) {
385 _execution_sem.signal ();
388 while (to_run == 0) {
389 _execution_tokens += 1;
390 pthread_mutex_unlock (&_trigger_mutex);
391 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 goes to sleep\n", pthread_self()));
392 _execution_sem.wait ();
396 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 is awake\n", pthread_self()));
397 pthread_mutex_lock (&_trigger_mutex);
398 if (_trigger_queue.size()) {
399 to_run = _trigger_queue.back();
400 _trigger_queue.pop_back();
403 pthread_mutex_unlock (&_trigger_mutex);
406 to_run->finish (_current_chain);
408 DEBUG_TRACE(DEBUG::ProcessThreads, string_compose ("%1 has finished run_one()\n", pthread_self()));
414 Graph::helper_thread()
416 suspend_rt_malloc_checks ();
417 ProcessThread* pt = new ProcessThread ();
418 resume_rt_malloc_checks ();
431 /** Here's the main graph thread */
435 suspend_rt_malloc_checks ();
436 ProcessThread* pt = new ProcessThread ();
437 resume_rt_malloc_checks ();
442 _callback_start_sem.wait ();
444 DEBUG_TRACE(DEBUG::ProcessThreads, "main thread is awake\n");
452 if (_graph_empty && !_quit_threads) {
453 _callback_done_sem.signal ();
454 DEBUG_TRACE(DEBUG::ProcessThreads, "main thread sees graph done, goes back to sleep\n");
458 /* This loop will run forever */
460 DEBUG_TRACE(DEBUG::ProcessThreads, "main thread runs one graph node\n");
470 Graph::dump (int chain)
473 node_list_t::iterator ni;
474 node_set_t::iterator ai;
476 chain = _pending_chain;
478 DEBUG_TRACE (DEBUG::Graph, "--------------------------------------------Graph dump:\n");
479 for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) {
480 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni);
481 DEBUG_TRACE (DEBUG::Graph, string_compose ("GraphNode: %1 refcount: %2\n", rp->name().c_str(), (*ni)->_init_refcount[chain]));
482 for (ai=(*ni)->_activation_set[chain].begin(); ai!=(*ni)->_activation_set[chain].end(); ai++) {
483 DEBUG_TRACE (DEBUG::Graph, string_compose (" triggers: %1\n", boost::dynamic_pointer_cast<Route>(*ai)->name().c_str()));
487 DEBUG_TRACE (DEBUG::Graph, "------------- trigger list:\n");
488 for (ni=_init_trigger_list[chain].begin(); ni!=_init_trigger_list[chain].end(); ni++) {
489 DEBUG_TRACE (DEBUG::Graph, string_compose ("GraphNode: %1 refcount: %2\n", boost::dynamic_pointer_cast<Route>(*ni)->name().c_str(), (*ni)->_init_refcount[chain]));
492 DEBUG_TRACE (DEBUG::Graph, string_compose ("final activation refcount: %1\n", _init_finished_refcount[chain]));
497 Graph::silent_process_routes (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, bool& need_butler)
499 _process_nframes = nframes;
500 _process_start_frame = start_frame;
501 _process_end_frame = end_frame;
503 _process_silent = true;
504 _process_noroll = false;
506 _process_need_butler = false;
509 DEBUG_TRACE(DEBUG::ProcessThreads, "wake graph for silent process\n");
510 _callback_start_sem.signal ();
511 _callback_done_sem.wait ();
514 need_butler = _process_need_butler;
516 return _process_retval;
520 Graph::process_routes (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler)
522 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("graph execution from %1 to %2 = %3\n", start_frame, end_frame, nframes));
524 _process_nframes = nframes;
525 _process_start_frame = start_frame;
526 _process_end_frame = end_frame;
527 _process_declick = declick;
529 _process_silent = false;
530 _process_noroll = false;
532 _process_need_butler = false;
534 DEBUG_TRACE(DEBUG::ProcessThreads, "wake graph for non-silent process\n");
535 _callback_start_sem.signal ();
536 _callback_done_sem.wait ();
538 DEBUG_TRACE (DEBUG::ProcessThreads, "graph execution complete\n");
540 need_butler = _process_need_butler;
542 return _process_retval;
546 Graph::routes_no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
547 bool non_rt_pending, int declick)
549 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("no-roll graph execution from %1 to %2 = %3\n", start_frame, end_frame, nframes));
551 _process_nframes = nframes;
552 _process_start_frame = start_frame;
553 _process_end_frame = end_frame;
554 _process_declick = declick;
555 _process_non_rt_pending = non_rt_pending;
557 _process_silent = false;
558 _process_noroll = true;
560 _process_need_butler = false;
562 DEBUG_TRACE(DEBUG::ProcessThreads, "wake graph for no-roll process\n");
563 _callback_start_sem.signal ();
564 _callback_done_sem.wait ();
566 return _process_retval;
569 Graph::process_one_route (Route* route)
571 bool need_butler = false;
576 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 runs route %2\n", pthread_self(), route->name()));
578 if (_process_silent) {
579 retval = route->silent_roll (_process_nframes, _process_start_frame, _process_end_frame, need_butler);
580 } else if (_process_noroll) {
581 route->set_pending_declick (_process_declick);
582 retval = route->no_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_non_rt_pending);
584 route->set_pending_declick (_process_declick);
585 retval = route->roll (_process_nframes, _process_start_frame, _process_end_frame, _process_declick, need_butler);
589 _process_retval = retval;
593 _process_need_butler = true;
598 Graph::in_process_thread () const
600 list<pthread_t>::const_iterator i = _thread_list.begin ();
601 while (i != _thread_list.end() && *i != pthread_self ()) {
605 return i != _thread_list.end ();