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"
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 Graph::Graph (Session & session)
44 : SessionHandleRef (session)
46 pthread_mutex_init( &_trigger_mutex, NULL);
47 sem_init( &_execution_sem, 0, 0 );
49 sem_init( &_callback_start_sem, 0, 0 );
50 sem_init( &_callback_done_sem, 0, 0 );
51 sem_init( &_cleanup_sem, 0, 0 );
53 _execution_tokens = 0;
58 _quit_threads = false;
61 int num_cpu = hardware_concurrency();
62 int num_threads = num_cpu;
63 int pu = Config->get_processor_usage ();
66 /* pu is negative: use "pu" less cores for DSP than appear to be available
69 if (-pu < num_threads) {
75 /* use "pu" cores, if available
78 if (pu <= num_threads) {
83 info << string_compose (_("Using %2 threads on %1 CPUs"), num_cpu, num_threads) << endmsg;
85 _thread_list.push_back (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::main_thread, this), 100000));
87 for (int i = 1; i < num_threads; ++i) {
88 _thread_list.push_back (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::helper_thread, this), 100000));
93 Graph::session_going_away()
97 for (unsigned int i=0; i<_thread_list.size(); i++) {
98 sem_post( &_execution_sem);
101 sem_post( &_callback_start_sem);
103 for (list<pthread_t>::iterator i = _thread_list.begin(); i != _thread_list.end(); i++) {
105 pthread_join (*i, &status);
108 // now drop all references on the nodes.
109 _nodes_rt[0].clear();
110 _nodes_rt[1].clear();
111 _init_trigger_list[0].clear();
112 _init_trigger_list[1].clear();
113 _trigger_queue.clear();
117 Graph::clear_other_chain ()
119 Glib::Mutex::Lock ls (_swap_mutex);
122 if (_setup_chain != _pending_chain) {
124 for (node_list_t::iterator ni=_nodes_rt[_setup_chain].begin(); ni!=_nodes_rt[_setup_chain].end(); ni++) {
125 (*ni)->_activation_set[_setup_chain].clear();
128 _nodes_rt[_setup_chain].clear ();
129 _init_trigger_list[_setup_chain].clear ();
132 /* setup chain == pending chain - we have
133 to wait till this is no longer true.
135 _cleanup_cond.wait (_swap_mutex);
142 node_list_t::iterator i;
145 if (_swap_mutex.trylock()) {
146 // we got the swap mutex.
147 if (_current_chain != _pending_chain)
149 // printf ("chain swap ! %d -> %d\n", _current_chain, _pending_chain);
150 _setup_chain = _current_chain;
151 _current_chain = _pending_chain;
152 _cleanup_cond.signal ();
154 _swap_mutex.unlock ();
157 chain = _current_chain;
160 for (i=_nodes_rt[chain].begin(); i!=_nodes_rt[chain].end(); i++) {
162 _graph_empty = false;
164 _finished_refcount = _init_finished_refcount[chain];
166 for (i=_init_trigger_list[chain].begin(); i!=_init_trigger_list[chain].end(); i++) {
167 this->trigger( i->get() );
172 Graph::trigger (GraphNode* n)
174 pthread_mutex_lock (&_trigger_mutex);
175 _trigger_queue.push_back( n);
176 pthread_mutex_unlock (&_trigger_mutex);
182 if (g_atomic_int_dec_and_test (&_finished_refcount)) {
184 // ok... this cycle is finished now.
185 // we are the only thread alive.
187 this->restart_cycle();
192 Graph::restart_cycle()
194 //printf( "cycle_done chain: %d\n", _current_chain);
196 // we are through. wakeup our caller.
198 sem_post( &_callback_done_sem);
200 // block until we are triggered.
201 sem_wait( &_callback_start_sem);
205 //printf( "cycle_start\n" );
210 //printf( "cycle_start chain: %d\n", _current_chain);
212 // returning will restart the cycle.
213 // starting with waking up the others.
217 is_feedback (boost::shared_ptr<RouteList> routelist, Route* from, boost::shared_ptr<Route> to)
219 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
220 if ((*ri).get() == from)
230 is_feedback (boost::shared_ptr<RouteList> routelist, boost::shared_ptr<Route> from, Route* to)
232 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
233 if ((*ri).get() == to)
243 Graph::rechain (boost::shared_ptr<RouteList> routelist)
245 node_list_t::iterator ni;
246 Glib::Mutex::Lock ls (_swap_mutex);
248 int chain = _setup_chain;
249 DEBUG_TRACE (DEBUG::Graph, string_compose ("============== setup %1\n", chain));
250 // set all refcounts to 0;
252 _init_finished_refcount[chain] = 0;
253 _init_trigger_list[chain].clear();
255 _nodes_rt[chain].clear();
257 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
258 node_ptr_t n = boost::dynamic_pointer_cast<GraphNode> (*ri);
260 n->_init_refcount[chain] = 0;
261 n->_activation_set[chain].clear();
262 _nodes_rt[chain].push_back(n);
265 // now add refs for the connections.
267 for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) {
268 bool has_input = false;
269 bool has_output = false;
271 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni);
273 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
274 if (rp->direct_feeds (*ri)) {
275 if (is_feedback (routelist, rp.get(), *ri)) {
280 (*ni)->_activation_set[chain].insert (boost::dynamic_pointer_cast<GraphNode> (*ri) );
284 for (Route::FedBy::iterator fi=rp->fed_by().begin(); fi!=rp->fed_by().end(); fi++) {
285 if (boost::shared_ptr<Route> r = fi->r.lock()) {
286 if (!is_feedback (routelist, r, rp.get())) {
292 for (node_set_t::iterator ai=(*ni)->_activation_set[chain].begin(); ai!=(*ni)->_activation_set[chain].end(); ai++) {
293 (*ai)->_init_refcount[chain] += 1;
297 _init_trigger_list[chain].push_back (*ni);
300 _init_finished_refcount[chain] += 1;
303 _pending_chain = chain;
312 pthread_mutex_lock (&_trigger_mutex);
313 if (_trigger_queue.size()) {
314 to_run = _trigger_queue.back();
315 _trigger_queue.pop_back();
320 int wakeup = min ((int) _execution_tokens, (int) _trigger_queue.size());
321 _execution_tokens -= wakeup;
323 for (int i=0; i<wakeup; i++ ) {
324 sem_post (&_execution_sem);
327 while (to_run == 0) {
328 _execution_tokens += 1;
329 pthread_mutex_unlock (&_trigger_mutex);
330 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 goes to sleep\n", pthread_self()));
331 sem_wait (&_execution_sem);
334 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 is awake\n", pthread_self()));
335 pthread_mutex_lock (&_trigger_mutex);
336 if (_trigger_queue.size()) {
337 to_run = _trigger_queue.back();
338 _trigger_queue.pop_back();
341 pthread_mutex_unlock (&_trigger_mutex);
344 to_run->finish (_current_chain);
351 if (!jack_is_realtime (AudioEngine::instance()->jack())) {
355 int priority = jack_client_real_time_priority (AudioEngine::instance()->jack());
358 struct sched_param rtparam;
360 memset (&rtparam, 0, sizeof (rtparam));
361 rtparam.sched_priority = priority;
363 pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam);
368 Graph::helper_thread()
370 ProcessThread *pt = new ProcessThread;
387 ProcessThread *pt = new ProcessThread;
393 sem_wait (&_callback_start_sem);
397 if (_graph_empty && !_quit_threads) {
398 sem_post (&_callback_done_sem);
412 Graph::dump (int chain)
415 node_list_t::iterator ni;
416 node_set_t::iterator ai;
418 chain = _pending_chain;
420 DEBUG_TRACE (DEBUG::Graph, "--------------------------------------------Graph dump:\n");
421 for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) {
422 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni);
423 DEBUG_TRACE (DEBUG::Graph, string_compose ("GraphNode: %1 refcount: %2\n", rp->name().c_str(), (*ni)->_init_refcount[chain]));
424 for (ai=(*ni)->_activation_set[chain].begin(); ai!=(*ni)->_activation_set[chain].end(); ai++) {
425 DEBUG_TRACE (DEBUG::Graph, string_compose (" triggers: %1\n", boost::dynamic_pointer_cast<Route>(*ai)->name().c_str()));
429 DEBUG_TRACE (DEBUG::Graph, "------------- trigger list:\n");
430 for (ni=_init_trigger_list[chain].begin(); ni!=_init_trigger_list[chain].end(); ni++) {
431 DEBUG_TRACE (DEBUG::Graph, string_compose ("GraphNode: %1 refcount: %2\n", boost::dynamic_pointer_cast<Route>(*ni)->name().c_str(), (*ni)->_init_refcount[chain]));
434 DEBUG_TRACE (DEBUG::Graph, string_compose ("final activation refcount: %1\n", _init_finished_refcount[chain]));
439 Graph::silent_process_routes (nframes_t nframes, framepos_t start_frame, framepos_t end_frame,
440 bool can_record, bool rec_monitors_input, bool& need_butler)
442 _process_nframes = nframes;
443 _process_start_frame = start_frame;
444 _process_end_frame = end_frame;
445 _process_can_record = can_record;
446 _process_rec_monitors_input = rec_monitors_input;
448 _process_silent = true;
449 _process_noroll = false;
451 _process_need_butler = false;
454 sem_post (&_callback_start_sem);
455 sem_wait (&_callback_done_sem);
458 need_butler = _process_need_butler;
460 return _process_retval;
464 Graph::process_routes (nframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick,
465 bool can_record, bool rec_monitors_input, bool& need_butler)
467 _process_nframes = nframes;
468 _process_start_frame = start_frame;
469 _process_end_frame = end_frame;
470 _process_can_record = can_record;
471 _process_rec_monitors_input = rec_monitors_input;
472 _process_declick = declick;
474 _process_silent = false;
475 _process_noroll = false;
477 _process_need_butler = false;
479 sem_post (&_callback_start_sem);
480 sem_wait (&_callback_done_sem);
482 need_butler = _process_need_butler;
484 return _process_retval;
488 Graph::routes_no_roll (nframes_t nframes, framepos_t start_frame, framepos_t end_frame,
489 bool non_rt_pending, bool can_record, int declick)
491 _process_nframes = nframes;
492 _process_start_frame = start_frame;
493 _process_end_frame = end_frame;
494 _process_can_record = can_record;
495 _process_declick = declick;
496 _process_non_rt_pending = non_rt_pending;
498 _process_silent = false;
499 _process_noroll = true;
501 _process_need_butler = false;
503 sem_post (&_callback_start_sem);
504 sem_wait (&_callback_done_sem);
506 return _process_retval;
509 Graph::process_one_route (Route* route)
511 bool need_butler = false;
516 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 runs route %2\n", pthread_self(), route->name()));
518 if (_process_silent) {
519 retval = route->silent_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_can_record, _process_rec_monitors_input, need_butler);
520 } else if (_process_noroll) {
521 route->set_pending_declick (_process_declick);
522 retval = route->no_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_non_rt_pending, _process_can_record, _process_declick);
524 route->set_pending_declick (_process_declick);
525 retval = route->roll (_process_nframes, _process_start_frame, _process_end_frame, _process_declick, _process_can_record, _process_rec_monitors_input, need_butler);
529 _process_retval = retval;
533 _process_need_butler = true;