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.
24 #include "pbd/compose.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;
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 );
52 _execution_tokens = 0;
54 pthread_mutex_init (&_swap_mutex, NULL);
58 _quit_threads = false;
61 int num_cpu = hardware_concurrency();
62 int num_threads = num_cpu;
63 int pu = Config->get_processor_usage ();
66 /* use "pu" less cores for DSP than appear to be available
69 if (pu < num_threads) {
70 num_threads += pu; // pu is negative
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 (std::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();
119 node_list_t::iterator i;
122 if (pthread_mutex_trylock (&_swap_mutex) == 0) {
123 // we got the swap mutex.
124 if (_current_chain != _pending_chain)
126 //printf ("chain swap ! %d -> %d\n", _current_chain, _pending_chain);
127 _setup_chain = _current_chain;
128 _current_chain = _pending_chain;
130 pthread_mutex_unlock (&_swap_mutex);
133 chain = _current_chain;
136 for (i=_nodes_rt[chain].begin(); i!=_nodes_rt[chain].end(); i++) {
138 _graph_empty = false;
140 _finished_refcount = _init_finished_refcount[chain];
142 for (i=_init_trigger_list[chain].begin(); i!=_init_trigger_list[chain].end(); i++) {
143 this->trigger( i->get() );
148 Graph::trigger (GraphNode* n)
150 pthread_mutex_lock (&_trigger_mutex);
151 _trigger_queue.push_back( n);
152 pthread_mutex_unlock (&_trigger_mutex);
158 if (g_atomic_int_dec_and_test (&_finished_refcount)) {
160 // ok... this cycle is finished now.
161 // we are the only thread alive.
163 this->restart_cycle();
168 Graph::restart_cycle()
170 //printf( "cycle_done chain: %d\n", _current_chain);
172 // we are through. wakeup our caller.
174 sem_post( &_callback_done_sem);
176 // block until we are triggered.
177 sem_wait( &_callback_start_sem);
181 //printf( "cycle_start\n" );
186 //printf( "cycle_start chain: %d\n", _current_chain);
188 // returning will restart the cycle.
189 // starting with waking up the others.
193 is_feedback (boost::shared_ptr<RouteList> routelist, Route* from, boost::shared_ptr<Route> to)
195 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
196 if ((*ri).get() == from)
206 is_feedback (boost::shared_ptr<RouteList> routelist, boost::shared_ptr<Route> from, Route* to)
208 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
209 if ((*ri).get() == to)
219 Graph::rechain (boost::shared_ptr<RouteList> routelist)
221 node_list_t::iterator ni;
223 pthread_mutex_lock (&_swap_mutex);
224 int chain = _setup_chain;
225 DEBUG_TRACE (DEBUG::Graph, string_compose ("============== setup %1\n", chain));
226 // set all refcounts to 0;
228 _init_finished_refcount[chain] = 0;
229 _init_trigger_list[chain].clear();
231 _nodes_rt[chain].clear();
233 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
234 node_ptr_t n = boost::dynamic_pointer_cast<GraphNode> (*ri);
236 n->_init_refcount[chain] = 0;
237 n->_activation_set[chain].clear();
238 _nodes_rt[chain].push_back(n);
241 // now add refs for the connections.
243 for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) {
244 bool has_input = false;
245 bool has_output = false;
247 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni);
249 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
250 if (rp->direct_feeds (*ri)) {
251 if (is_feedback (routelist, rp.get(), *ri)) {
256 (*ni)->_activation_set[chain].insert (boost::dynamic_pointer_cast<GraphNode> (*ri) );
260 for (Route::FedBy::iterator fi=rp->fed_by().begin(); fi!=rp->fed_by().end(); fi++) {
261 if (boost::shared_ptr<Route> r = fi->r.lock()) {
262 if (!is_feedback (routelist, r, rp.get())) {
268 for (node_set_t::iterator ai=(*ni)->_activation_set[chain].begin(); ai!=(*ni)->_activation_set[chain].end(); ai++) {
269 (*ai)->_init_refcount[chain] += 1;
273 _init_trigger_list[chain].push_back (*ni);
276 _init_finished_refcount[chain] += 1;
279 _pending_chain = chain;
281 pthread_mutex_unlock (&_swap_mutex);
290 pthread_mutex_lock (&_trigger_mutex);
291 if (_trigger_queue.size()) {
292 to_run = _trigger_queue.back();
293 _trigger_queue.pop_back();
298 int wakeup = std::min ((int) _execution_tokens, (int) _trigger_queue.size());
299 _execution_tokens -= wakeup;
301 for (int i=0; i<wakeup; i++ ) {
302 sem_post (&_execution_sem);
305 while (to_run == 0) {
306 _execution_tokens += 1;
307 pthread_mutex_unlock (&_trigger_mutex);
308 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 goes to sleep\n", pthread_self()));
309 sem_wait (&_execution_sem);
312 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 is awake\n", pthread_self()));
313 pthread_mutex_lock (&_trigger_mutex);
314 if (_trigger_queue.size()) {
315 to_run = _trigger_queue.back();
316 _trigger_queue.pop_back();
319 pthread_mutex_unlock (&_trigger_mutex);
322 to_run->finish (_current_chain);
329 if (!jack_is_realtime (AudioEngine::instance()->jack())) {
333 int priority = jack_client_real_time_priority (AudioEngine::instance()->jack());
336 struct sched_param rtparam;
338 memset (&rtparam, 0, sizeof (rtparam));
339 rtparam.sched_priority = priority;
341 pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam);
346 Graph::helper_thread()
348 ProcessThread *pt = new ProcessThread;
365 ProcessThread *pt = new ProcessThread;
371 sem_wait (&_callback_start_sem);
376 sem_post (&_callback_done_sem);
390 Graph::dump (int chain)
393 node_list_t::iterator ni;
394 node_set_t::iterator ai;
396 chain = _pending_chain;
398 DEBUG_TRACE (DEBUG::Graph, "--------------------------------------------Graph dump:\n");
399 for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) {
400 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni);
401 DEBUG_TRACE (DEBUG::Graph, string_compose ("GraphNode: %1 refcount: %2\n", rp->name().c_str(), (*ni)->_init_refcount[chain]));
402 for (ai=(*ni)->_activation_set[chain].begin(); ai!=(*ni)->_activation_set[chain].end(); ai++) {
403 DEBUG_TRACE (DEBUG::Graph, string_compose (" triggers: %1\n", boost::dynamic_pointer_cast<Route>(*ai)->name().c_str()));
407 DEBUG_TRACE (DEBUG::Graph, "------------- trigger list:\n");
408 for (ni=_init_trigger_list[chain].begin(); ni!=_init_trigger_list[chain].end(); ni++) {
409 DEBUG_TRACE (DEBUG::Graph, string_compose ("GraphNode: %1 refcount: %2\n", boost::dynamic_pointer_cast<Route>(*ni)->name().c_str(), (*ni)->_init_refcount[chain]));
412 DEBUG_TRACE (DEBUG::Graph, string_compose ("final activation refcount: %1\n", _init_finished_refcount[chain]));
417 Graph::silent_process_routes (nframes_t nframes, framepos_t start_frame, framepos_t end_frame,
418 bool can_record, bool rec_monitors_input, bool& need_butler)
420 _process_nframes = nframes;
421 _process_start_frame = start_frame;
422 _process_end_frame = end_frame;
423 _process_can_record = can_record;
424 _process_rec_monitors_input = rec_monitors_input;
426 _process_silent = true;
427 _process_noroll = false;
429 _process_need_butler = false;
432 sem_post (&_callback_start_sem);
433 sem_wait (&_callback_done_sem);
436 need_butler = _process_need_butler;
438 return _process_retval;
442 Graph::process_routes (nframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick,
443 bool can_record, bool rec_monitors_input, bool& need_butler)
445 _process_nframes = nframes;
446 _process_start_frame = start_frame;
447 _process_end_frame = end_frame;
448 _process_can_record = can_record;
449 _process_rec_monitors_input = rec_monitors_input;
450 _process_declick = declick;
452 _process_silent = false;
453 _process_noroll = false;
455 _process_need_butler = false;
457 sem_post (&_callback_start_sem);
458 sem_wait (&_callback_done_sem);
460 need_butler = _process_need_butler;
462 return _process_retval;
466 Graph::routes_no_roll (nframes_t nframes, framepos_t start_frame, framepos_t end_frame,
467 bool non_rt_pending, bool can_record, int declick)
469 _process_nframes = nframes;
470 _process_start_frame = start_frame;
471 _process_end_frame = end_frame;
472 _process_can_record = can_record;
473 _process_declick = declick;
474 _process_non_rt_pending = non_rt_pending;
476 _process_silent = false;
477 _process_noroll = true;
479 _process_need_butler = false;
481 sem_post (&_callback_start_sem);
482 sem_wait (&_callback_done_sem);
484 return _process_retval;
487 Graph::process_one_route (Route* route)
489 bool need_butler = false;
494 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 runs route %2\n", pthread_self(), route->name()));
496 if (_process_silent) {
497 retval = route->silent_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_can_record, _process_rec_monitors_input, need_butler);
498 } else if (_process_noroll) {
499 route->set_pending_declick (_process_declick);
500 retval = route->no_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_non_rt_pending, _process_can_record, _process_declick);
502 route->set_pending_declick (_process_declick);
503 retval = route->roll (_process_nframes, _process_start_frame, _process_end_frame, _process_declick, _process_can_record, _process_rec_monitors_input, need_butler);
507 _process_retval = retval;
511 _process_need_butler = true;