2 Copyright (C) 2010 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "pbd/compose.h"
22 #include "ardour/debug.h"
23 #include "ardour/graph.h"
24 #include "ardour/types.h"
25 #include "ardour/session.h"
26 #include "ardour/route.h"
27 #include "ardour/process_thread.h"
28 #include "ardour/audioengine.h"
30 #include <jack/thread.h>
34 #elif defined(__APPLE__) || defined(__FreeBSD__)
35 #include <sys/types.h>
36 #include <sys/sysctl.h>
44 using namespace ARDOUR;
47 static unsigned int hardware_concurrency()
49 #if defined(PTW32_VERSION) || defined(__hpux)
50 return pthread_num_processors_np();
51 #elif defined(__APPLE__) || defined(__FreeBSD__)
53 size_t size=sizeof(count);
54 return sysctlbyname("hw.ncpu",&count,&size,NULL,0)?0:count;
55 #elif defined(HAVE_UNISTD) && defined(_SC_NPROCESSORS_ONLN)
56 int const count=sysconf(_SC_NPROCESSORS_ONLN);
57 return (count>0)?count:0;
62 // ========================================== Graph
64 Graph::Graph( Session & session )
65 : SessionHandleRef( session )
67 pthread_mutex_init( &_trigger_mutex, NULL );
68 sem_init( &_execution_sem, 0, 0 );
70 sem_init( &_callback_start_sem, 0, 0 );
71 sem_init( &_callback_done_sem, 0, 0 );
73 _execution_tokens = 0;
75 pthread_mutex_init( &_swap_mutex, NULL );
79 _quit_threads = false;
82 int num_cpu = hardware_concurrency();
83 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("Using %1 CPUs via %1 threads\n", num_cpu));
84 _thread_list.push_back( Glib::Thread::create( sigc::mem_fun( *this, &Graph::main_thread ), 100000, true, true, Glib::THREAD_PRIORITY_NORMAL ) );
85 for (int i=1; i<num_cpu; i++)
86 _thread_list.push_back( Glib::Thread::create( sigc::mem_fun( *this, &Graph::helper_thread ), 100000, true, true, Glib::THREAD_PRIORITY_NORMAL ) );
90 Graph::session_going_away()
94 for (unsigned int i=0; i<_thread_list.size(); i++)
95 sem_post( &_execution_sem );
97 sem_post( &_callback_start_sem );
99 for (std::list<Glib::Thread *>::iterator i=_thread_list.begin(); i!=_thread_list.end(); i++)
104 // now drop all references on the nodes.
106 _nodes_rt[0].clear();
107 _nodes_rt[1].clear();
108 _init_trigger_list[0].clear();
109 _init_trigger_list[1].clear();
110 _trigger_queue.clear();
116 node_list_t::iterator i;
119 if (pthread_mutex_trylock (&_swap_mutex) == 0)
121 // we got the swap mutex.
122 if (_current_chain != _pending_chain)
124 //printf ("chain swap ! %d -> %d\n", _current_chain, _pending_chain );
125 _setup_chain = _current_chain;
126 _current_chain = _pending_chain;
128 pthread_mutex_unlock (&_swap_mutex);
131 chain = _current_chain;
134 for (i=_nodes_rt[chain].begin(); i!=_nodes_rt[chain].end(); i++)
137 _graph_empty = false;
139 _finished_refcount = _init_finished_refcount[chain];
141 for (i=_init_trigger_list[chain].begin(); i!=_init_trigger_list[chain].end(); i++)
142 this->trigger( i->get() );
146 Graph::trigger( GraphNode * n )
148 pthread_mutex_lock( &_trigger_mutex );
149 _trigger_queue.push_back( n );
150 pthread_mutex_unlock( &_trigger_mutex );
156 if (g_atomic_int_dec_and_test( &_finished_refcount ))
158 // ok... this cycle is finished now.
159 // we are the only thread alive.
161 this->restart_cycle();
167 Graph::restart_cycle()
169 //printf( "cycle_done chain: %d\n", _current_chain );
171 // we are through. wakeup our caller.
173 sem_post( &_callback_done_sem );
175 // block until we are triggered.
176 sem_wait( &_callback_start_sem );
180 //printf( "cycle_start\n" );
185 //printf( "cycle_start chain: %d\n", _current_chain );
187 // returning will restart the cycle.
188 // starting with waking up the others.
192 is_feedback( boost::shared_ptr<RouteList> routelist, Route * from, boost::shared_ptr<Route> to )
194 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++)
210 if ((*ri).get() == to)
220 Graph::rechain( boost::shared_ptr<RouteList> routelist )
222 node_list_t::iterator ni;
224 pthread_mutex_lock (&_swap_mutex);
225 int chain = _setup_chain;
226 printf( "============== setup %d\n", chain );
227 // set all refcounts to 0;
229 _init_finished_refcount[chain] = 0;
230 _init_trigger_list[chain].clear();
232 _nodes_rt[chain].clear();
234 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++)
236 node_ptr_t n = boost::dynamic_pointer_cast<GraphNode> (*ri);
238 n->_init_refcount[chain] = 0;
239 n->_activation_set[chain].clear();
240 _nodes_rt[chain].push_back(n);
243 // now add refs for the connections.
245 for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++)
247 bool has_input = false;
248 bool has_output = false;
250 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni );
252 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++)
254 if (rp->direct_feeds( *ri ))
256 if (is_feedback( routelist, rp.get(), *ri ))
260 (*ni)->_activation_set[chain].insert( boost::dynamic_pointer_cast<GraphNode> (*ri) );
264 for (Route::FedBy::iterator fi=rp->fed_by().begin(); fi!=rp->fed_by().end(); fi++)
266 if (boost::shared_ptr<Route> r = fi->r.lock())
267 if (!is_feedback( routelist, r, rp.get() ))
271 for (node_set_t::iterator ai=(*ni)->_activation_set[chain].begin(); ai!=(*ni)->_activation_set[chain].end(); ai++)
273 (*ai)->_init_refcount[chain] += 1;
277 _init_trigger_list[chain].push_back( *ni );
280 _init_finished_refcount[chain] += 1;
283 _pending_chain = chain;
285 pthread_mutex_unlock (&_swap_mutex);
294 pthread_mutex_lock( &_trigger_mutex );
295 if (_trigger_queue.size()) {
296 to_run = _trigger_queue.back();
297 _trigger_queue.pop_back();
302 int wakeup = std::min( (int) _execution_tokens, (int) _trigger_queue.size() );
303 _execution_tokens -= wakeup;
305 for( int i=0; i<wakeup; i++ )
306 sem_post( &_execution_sem );
310 _execution_tokens += 1;
311 pthread_mutex_unlock( &_trigger_mutex );
312 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 goes to sleep\n", pthread_self()));
313 sem_wait( &_execution_sem );
316 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 is awake\n", pthread_self()));
317 pthread_mutex_lock( &_trigger_mutex );
318 if (_trigger_queue.size())
320 to_run = _trigger_queue.back();
321 _trigger_queue.pop_back();
324 pthread_mutex_unlock( &_trigger_mutex );
327 to_run->finish( _current_chain );
334 int priority = jack_client_real_time_priority( AudioEngine::instance()->jack() );
338 struct sched_param rtparam;
340 memset (&rtparam, 0, sizeof (rtparam));
341 rtparam.sched_priority = priority;
343 pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam);
348 Graph::helper_thread()
350 ProcessThread *pt = new ProcessThread;
367 ProcessThread *pt = new ProcessThread;
373 sem_wait( &_callback_start_sem );
378 sem_post( &_callback_done_sem );
391 Graph::dump( int chain )
394 node_list_t::iterator ni;
395 node_set_t::iterator ai;
397 chain = _pending_chain;
399 printf( "--------------------------------------------Graph dump:\n" );
400 for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++)
402 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni );
403 printf( "GraphNode: %s refcount: %d\n", rp->name().c_str(), (*ni)->_init_refcount[chain] );
404 for (ai=(*ni)->_activation_set[chain].begin(); ai!=(*ni)->_activation_set[chain].end(); ai++)
406 printf( " triggers: %s\n", boost::dynamic_pointer_cast<Route>(*ai)->name().c_str() );
410 printf( "------------- trigger list:\n" );
411 for (ni=_init_trigger_list[chain].begin(); ni!=_init_trigger_list[chain].end(); ni++)
413 printf( "GraphNode: %s refcount: %d\n", boost::dynamic_pointer_cast<Route>(*ni)->name().c_str(), (*ni)->_init_refcount[chain] );
416 printf( "final activation refcount: %d\n", _init_finished_refcount[chain] );
421 Graph::silent_process_routes (nframes_t nframes, sframes_t start_frame, sframes_t end_frame,
422 bool can_record, bool rec_monitors_input, bool& need_butler )
424 _process_nframes = nframes;
425 _process_start_frame = start_frame;
426 _process_end_frame = end_frame;
427 _process_can_record = can_record;
428 _process_rec_monitors_input = rec_monitors_input;
430 _process_silent = true;
431 _process_noroll = false;
433 _process_need_butler = false;
437 sem_post( &_callback_start_sem );
438 sem_wait( &_callback_done_sem );
441 need_butler = _process_need_butler;
443 return _process_retval;
447 Graph::process_routes (nframes_t nframes, sframes_t start_frame, sframes_t end_frame, int declick,
448 bool can_record, bool rec_monitors_input, bool& need_butler )
450 _process_nframes = nframes;
451 _process_start_frame = start_frame;
452 _process_end_frame = end_frame;
453 _process_can_record = can_record;
454 _process_rec_monitors_input = rec_monitors_input;
455 _process_declick = declick;
457 _process_silent = false;
458 _process_noroll = false;
460 _process_need_butler = false;
462 sem_post( &_callback_start_sem );
463 sem_wait( &_callback_done_sem );
465 need_butler = _process_need_butler;
467 return _process_retval;
471 Graph::routes_no_roll (nframes_t nframes, sframes_t start_frame, sframes_t end_frame,
472 bool non_rt_pending, bool can_record, int declick)
474 _process_nframes = nframes;
475 _process_start_frame = start_frame;
476 _process_end_frame = end_frame;
477 _process_can_record = can_record;
478 _process_declick = declick;
479 _process_non_rt_pending = non_rt_pending;
481 _process_silent = false;
482 _process_noroll = true;
484 _process_need_butler = false;
486 sem_post( &_callback_start_sem );
487 sem_wait( &_callback_done_sem );
489 return _process_retval;
492 Graph::process_one_route( Route * route )
494 bool need_butler = false;
499 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 runs route %2\n", pthread_self(), route->name()));
502 retval = route->silent_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_can_record, _process_rec_monitors_input, need_butler);
503 else if (_process_noroll)
505 route->set_pending_declick (_process_declick);
506 retval = route->no_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_non_rt_pending, _process_can_record, _process_declick);
510 route->set_pending_declick (_process_declick);
511 retval = route->roll (_process_nframes, _process_start_frame, _process_end_frame, _process_declick, _process_can_record, _process_rec_monitors_input, need_butler);
515 _process_retval = retval;
518 _process_need_butler = true;