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);
303 while (to_run == 0) {
304 _execution_tokens += 1;
305 pthread_mutex_unlock (&_trigger_mutex);
306 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 goes to sleep\n", pthread_self()));
307 sem_wait (&_execution_sem);
310 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 is awake\n", pthread_self()));
311 pthread_mutex_lock (&_trigger_mutex);
312 if (_trigger_queue.size()) {
313 to_run = _trigger_queue.back();
314 _trigger_queue.pop_back();
317 pthread_mutex_unlock (&_trigger_mutex);
320 to_run->finish (_current_chain);
327 int priority = jack_client_real_time_priority (AudioEngine::instance()->jack());
330 struct sched_param rtparam;
332 memset (&rtparam, 0, sizeof (rtparam));
333 rtparam.sched_priority = priority;
335 pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam);
340 Graph::helper_thread()
342 ProcessThread *pt = new ProcessThread;
358 ProcessThread *pt = new ProcessThread;
364 sem_wait (&_callback_start_sem);
369 sem_post (&_callback_done_sem);
381 Graph::dump (int chain)
384 node_list_t::iterator ni;
385 node_set_t::iterator ai;
387 chain = _pending_chain;
389 printf ("--------------------------------------------Graph dump:\n" );
390 for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) {
391 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni);
392 printf ("GraphNode: %s refcount: %d\n", rp->name().c_str(), (*ni)->_init_refcount[chain] );
393 for (ai=(*ni)->_activation_set[chain].begin(); ai!=(*ni)->_activation_set[chain].end(); ai++) {
394 printf (" triggers: %s\n", boost::dynamic_pointer_cast<Route>(*ai)->name().c_str() );
398 printf ("------------- trigger list:\n" );
399 for (ni=_init_trigger_list[chain].begin(); ni!=_init_trigger_list[chain].end(); ni++) {
400 printf ("GraphNode: %s refcount: %d\n", boost::dynamic_pointer_cast<Route>(*ni)->name().c_str(), (*ni)->_init_refcount[chain] );
403 printf ("final activation refcount: %d\n", _init_finished_refcount[chain] );
408 Graph::silent_process_routes (nframes_t nframes, sframes_t start_frame, sframes_t end_frame,
409 bool can_record, bool rec_monitors_input, bool& need_butler)
411 _process_nframes = nframes;
412 _process_start_frame = start_frame;
413 _process_end_frame = end_frame;
414 _process_can_record = can_record;
415 _process_rec_monitors_input = rec_monitors_input;
417 _process_silent = true;
418 _process_noroll = false;
420 _process_need_butler = false;
423 sem_post (&_callback_start_sem);
424 sem_wait (&_callback_done_sem);
427 need_butler = _process_need_butler;
429 return _process_retval;
433 Graph::process_routes (nframes_t nframes, sframes_t start_frame, sframes_t end_frame, int declick,
434 bool can_record, bool rec_monitors_input, bool& need_butler)
436 _process_nframes = nframes;
437 _process_start_frame = start_frame;
438 _process_end_frame = end_frame;
439 _process_can_record = can_record;
440 _process_rec_monitors_input = rec_monitors_input;
441 _process_declick = declick;
443 _process_silent = false;
444 _process_noroll = false;
446 _process_need_butler = false;
448 sem_post (&_callback_start_sem);
449 sem_wait (&_callback_done_sem);
451 need_butler = _process_need_butler;
453 return _process_retval;
457 Graph::routes_no_roll (nframes_t nframes, sframes_t start_frame, sframes_t end_frame,
458 bool non_rt_pending, bool can_record, int declick)
460 _process_nframes = nframes;
461 _process_start_frame = start_frame;
462 _process_end_frame = end_frame;
463 _process_can_record = can_record;
464 _process_declick = declick;
465 _process_non_rt_pending = non_rt_pending;
467 _process_silent = false;
468 _process_noroll = true;
470 _process_need_butler = false;
472 sem_post (&_callback_start_sem);
473 sem_wait (&_callback_done_sem);
475 return _process_retval;
478 Graph::process_one_route (Route * route)
480 bool need_butler = false;
485 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 runs route %2\n", pthread_self(), route->name()));
487 if (_process_silent) {
488 retval = route->silent_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_can_record, _process_rec_monitors_input, need_butler);
489 } else if (_process_noroll) {
490 route->set_pending_declick (_process_declick);
491 retval = route->no_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_non_rt_pending, _process_can_record, _process_declick);
493 route->set_pending_declick (_process_declick);
494 retval = route->roll (_process_nframes, _process_start_frame, _process_end_frame, _process_declick, _process_can_record, _process_rec_monitors_input, need_butler);
498 _process_retval = retval;
502 _process_need_butler = true;