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 #elif defined(__APPLE__) || defined(__FreeBSD__)
24 #include <sys/types.h>
25 #include <sys/sysctl.h>
31 #include "pbd/compose.h"
33 #include "ardour/debug.h"
34 #include "ardour/graph.h"
35 #include "ardour/types.h"
36 #include "ardour/session.h"
37 #include "ardour/route.h"
38 #include "ardour/process_thread.h"
39 #include "ardour/audioengine.h"
41 #include <jack/thread.h>
45 using namespace ARDOUR;
49 hardware_concurrency()
51 #if defined(PTW32_VERSION) || defined(__hpux)
52 return pthread_num_processors_np();
53 #elif defined(__APPLE__) || defined(__FreeBSD__)
55 size_t size=sizeof(count);
56 return sysctlbyname("hw.ncpu",&count,&size,NULL,0)?0:count;
57 #elif defined(HAVE_UNISTD) && defined(_SC_NPROCESSORS_ONLN)
58 int const count=sysconf(_SC_NPROCESSORS_ONLN);
59 return (count>0)?count:0;
65 Graph::Graph (Session & session)
66 : SessionHandleRef (session)
68 pthread_mutex_init( &_trigger_mutex, NULL);
69 sem_init( &_execution_sem, 0, 0 );
71 sem_init( &_callback_start_sem, 0, 0 );
72 sem_init( &_callback_done_sem, 0, 0 );
74 _execution_tokens = 0;
76 pthread_mutex_init (&_swap_mutex, NULL);
80 _quit_threads = false;
83 int num_cpu = hardware_concurrency();
84 info << string_compose (_("Using %1 CPUs via %1 threads\n"), num_cpu) << endmsg;
85 _thread_list.push_back( Glib::Thread::create( sigc::mem_fun( *this, &Graph::main_thread), 100000, true, true, Glib::THREAD_PRIORITY_NORMAL) );
86 for (int i=1; i<num_cpu; i++)
87 _thread_list.push_back( Glib::Thread::create( sigc::mem_fun( *this, &Graph::helper_thread), 100000, true, true, Glib::THREAD_PRIORITY_NORMAL) );
91 Graph::session_going_away()
95 for (unsigned int i=0; i<_thread_list.size(); i++) {
96 sem_post( &_execution_sem);
99 sem_post( &_callback_start_sem);
101 for (std::list<Glib::Thread *>::iterator i=_thread_list.begin(); i!=_thread_list.end(); i++) {
105 // now drop all references on the nodes.
107 _nodes_rt[0].clear();
108 _nodes_rt[1].clear();
109 _init_trigger_list[0].clear();
110 _init_trigger_list[1].clear();
111 _trigger_queue.clear();
117 node_list_t::iterator i;
120 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++) {
136 _graph_empty = false;
138 _finished_refcount = _init_finished_refcount[chain];
140 for (i=_init_trigger_list[chain].begin(); i!=_init_trigger_list[chain].end(); i++) {
141 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();
166 Graph::restart_cycle()
168 //printf( "cycle_done chain: %d\n", _current_chain);
170 // we are through. wakeup our caller.
172 sem_post( &_callback_done_sem);
174 // block until we are triggered.
175 sem_wait( &_callback_start_sem);
179 //printf( "cycle_start\n" );
184 //printf( "cycle_start chain: %d\n", _current_chain);
186 // returning will restart the cycle.
187 // starting with waking up the others.
191 is_feedback (boost::shared_ptr<RouteList> routelist, Route * from, boost::shared_ptr<Route> to)
193 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
194 if ((*ri).get() == from)
204 is_feedback (boost::shared_ptr<RouteList> routelist, boost::shared_ptr<Route> from, Route * to)
206 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
207 if ((*ri).get() == to)
217 Graph::rechain (boost::shared_ptr<RouteList> routelist)
219 node_list_t::iterator ni;
221 pthread_mutex_lock (&_swap_mutex);
222 int chain = _setup_chain;
223 printf ("============== setup %d\n", chain);
224 // set all refcounts to 0;
226 _init_finished_refcount[chain] = 0;
227 _init_trigger_list[chain].clear();
229 _nodes_rt[chain].clear();
231 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
232 node_ptr_t n = boost::dynamic_pointer_cast<GraphNode> (*ri);
234 n->_init_refcount[chain] = 0;
235 n->_activation_set[chain].clear();
236 _nodes_rt[chain].push_back(n);
239 // now add refs for the connections.
241 for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) {
242 bool has_input = false;
243 bool has_output = false;
245 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni);
247 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++)
249 if (rp->direct_feeds (*ri))
251 if (is_feedback (routelist, rp.get(), *ri))
255 (*ni)->_activation_set[chain].insert (boost::dynamic_pointer_cast<GraphNode> (*ri) );
259 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() ))
266 for (node_set_t::iterator ai=(*ni)->_activation_set[chain].begin(); ai!=(*ni)->_activation_set[chain].end(); ai++)
268 (*ai)->_init_refcount[chain] += 1;
272 _init_trigger_list[chain].push_back (*ni);
275 _init_finished_refcount[chain] += 1;
278 _pending_chain = chain;
280 pthread_mutex_unlock (&_swap_mutex);
289 pthread_mutex_lock (&_trigger_mutex);
290 if (_trigger_queue.size()) {
291 to_run = _trigger_queue.back();
292 _trigger_queue.pop_back();
297 int wakeup = std::min ((int) _execution_tokens, (int) _trigger_queue.size());
298 _execution_tokens -= wakeup;
300 for (int i=0; i<wakeup; i++ ) {
301 sem_post (&_execution_sem);
304 while (to_run == 0) {
305 _execution_tokens += 1;
306 pthread_mutex_unlock (&_trigger_mutex);
307 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 goes to sleep\n", pthread_self()));
308 sem_wait (&_execution_sem);
311 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 is awake\n", pthread_self()));
312 pthread_mutex_lock (&_trigger_mutex);
313 if (_trigger_queue.size()) {
314 to_run = _trigger_queue.back();
315 _trigger_queue.pop_back();
318 pthread_mutex_unlock (&_trigger_mutex);
321 to_run->finish (_current_chain);
328 if (!jack_is_realtime (AudioEngine::instance()->jack())) {
332 int priority = jack_client_real_time_priority (AudioEngine::instance()->jack());
335 struct sched_param rtparam;
337 memset (&rtparam, 0, sizeof (rtparam));
338 rtparam.sched_priority = priority;
340 pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam);
345 Graph::helper_thread()
347 ProcessThread *pt = new ProcessThread;
364 ProcessThread *pt = new ProcessThread;
370 sem_wait (&_callback_start_sem);
375 sem_post (&_callback_done_sem);
389 Graph::dump (int chain)
392 node_list_t::iterator ni;
393 node_set_t::iterator ai;
395 chain = _pending_chain;
397 printf ("--------------------------------------------Graph dump:\n" );
398 for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) {
399 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni);
400 printf ("GraphNode: %s refcount: %d\n", rp->name().c_str(), (*ni)->_init_refcount[chain] );
401 for (ai=(*ni)->_activation_set[chain].begin(); ai!=(*ni)->_activation_set[chain].end(); ai++) {
402 printf (" triggers: %s\n", boost::dynamic_pointer_cast<Route>(*ai)->name().c_str() );
406 printf ("------------- trigger list:\n" );
407 for (ni=_init_trigger_list[chain].begin(); ni!=_init_trigger_list[chain].end(); ni++) {
408 printf ("GraphNode: %s refcount: %d\n", boost::dynamic_pointer_cast<Route>(*ni)->name().c_str(), (*ni)->_init_refcount[chain] );
411 printf ("final activation refcount: %d\n", _init_finished_refcount[chain] );
416 Graph::silent_process_routes (nframes_t nframes, framepos_t start_frame, framepos_t end_frame,
417 bool can_record, bool rec_monitors_input, bool& need_butler)
419 _process_nframes = nframes;
420 _process_start_frame = start_frame;
421 _process_end_frame = end_frame;
422 _process_can_record = can_record;
423 _process_rec_monitors_input = rec_monitors_input;
425 _process_silent = true;
426 _process_noroll = false;
428 _process_need_butler = false;
431 sem_post (&_callback_start_sem);
432 sem_wait (&_callback_done_sem);
435 need_butler = _process_need_butler;
437 return _process_retval;
441 Graph::process_routes (nframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick,
442 bool can_record, bool rec_monitors_input, bool& need_butler)
444 _process_nframes = nframes;
445 _process_start_frame = start_frame;
446 _process_end_frame = end_frame;
447 _process_can_record = can_record;
448 _process_rec_monitors_input = rec_monitors_input;
449 _process_declick = declick;
451 _process_silent = false;
452 _process_noroll = false;
454 _process_need_butler = false;
456 sem_post (&_callback_start_sem);
457 sem_wait (&_callback_done_sem);
459 need_butler = _process_need_butler;
461 return _process_retval;
465 Graph::routes_no_roll (nframes_t nframes, framepos_t start_frame, framepos_t end_frame,
466 bool non_rt_pending, bool can_record, int declick)
468 _process_nframes = nframes;
469 _process_start_frame = start_frame;
470 _process_end_frame = end_frame;
471 _process_can_record = can_record;
472 _process_declick = declick;
473 _process_non_rt_pending = non_rt_pending;
475 _process_silent = false;
476 _process_noroll = true;
478 _process_need_butler = false;
480 sem_post (&_callback_start_sem);
481 sem_wait (&_callback_done_sem);
483 return _process_retval;
486 Graph::process_one_route (Route * route)
488 bool need_butler = false;
493 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 runs route %2\n", pthread_self(), route->name()));
495 if (_process_silent) {
496 retval = route->silent_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_can_record, _process_rec_monitors_input, need_butler);
497 } else if (_process_noroll) {
498 route->set_pending_declick (_process_declick);
499 retval = route->no_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_non_rt_pending, _process_can_record, _process_declick);
501 route->set_pending_declick (_process_declick);
502 retval = route->roll (_process_nframes, _process_start_frame, _process_end_frame, _process_declick, _process_can_record, _process_rec_monitors_input, need_butler);
506 _process_retval = retval;
510 _process_need_butler = true;