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.
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) {
120 // we got the swap mutex.
121 if (_current_chain != _pending_chain)
123 //printf ("chain swap ! %d -> %d\n", _current_chain, _pending_chain);
124 _setup_chain = _current_chain;
125 _current_chain = _pending_chain;
127 pthread_mutex_unlock (&_swap_mutex);
130 chain = _current_chain;
133 for (i=_nodes_rt[chain].begin(); i!=_nodes_rt[chain].end(); i++) {
135 _graph_empty = false;
137 _finished_refcount = _init_finished_refcount[chain];
139 for (i=_init_trigger_list[chain].begin(); i!=_init_trigger_list[chain].end(); i++) {
140 this->trigger( i->get() );
145 Graph::trigger (GraphNode* n)
147 pthread_mutex_lock (&_trigger_mutex);
148 _trigger_queue.push_back( n);
149 pthread_mutex_unlock (&_trigger_mutex);
155 if (g_atomic_int_dec_and_test (&_finished_refcount)) {
157 // ok... this cycle is finished now.
158 // we are the only thread alive.
160 this->restart_cycle();
165 Graph::restart_cycle()
167 //printf( "cycle_done chain: %d\n", _current_chain);
169 // we are through. wakeup our caller.
171 sem_post( &_callback_done_sem);
173 // block until we are triggered.
174 sem_wait( &_callback_start_sem);
178 //printf( "cycle_start\n" );
183 //printf( "cycle_start chain: %d\n", _current_chain);
185 // returning will restart the cycle.
186 // starting with waking up the others.
190 is_feedback (boost::shared_ptr<RouteList> routelist, Route* from, boost::shared_ptr<Route> to)
192 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
193 if ((*ri).get() == from)
203 is_feedback (boost::shared_ptr<RouteList> routelist, boost::shared_ptr<Route> from, Route* to)
205 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
206 if ((*ri).get() == to)
216 Graph::rechain (boost::shared_ptr<RouteList> routelist)
218 node_list_t::iterator ni;
220 pthread_mutex_lock (&_swap_mutex);
221 int chain = _setup_chain;
222 DEBUG_TRACE (DEBUG::Graph, string_compose ("============== setup %1\n", chain));
223 // set all refcounts to 0;
225 _init_finished_refcount[chain] = 0;
226 _init_trigger_list[chain].clear();
228 _nodes_rt[chain].clear();
230 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
231 node_ptr_t n = boost::dynamic_pointer_cast<GraphNode> (*ri);
233 n->_init_refcount[chain] = 0;
234 n->_activation_set[chain].clear();
235 _nodes_rt[chain].push_back(n);
238 // now add refs for the connections.
240 for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) {
241 bool has_input = false;
242 bool has_output = false;
244 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni);
246 for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
247 if (rp->direct_feeds (*ri)) {
248 if (is_feedback (routelist, rp.get(), *ri)) {
253 (*ni)->_activation_set[chain].insert (boost::dynamic_pointer_cast<GraphNode> (*ri) );
257 for (Route::FedBy::iterator fi=rp->fed_by().begin(); fi!=rp->fed_by().end(); fi++) {
258 if (boost::shared_ptr<Route> r = fi->r.lock()) {
259 if (!is_feedback (routelist, r, rp.get())) {
265 for (node_set_t::iterator ai=(*ni)->_activation_set[chain].begin(); ai!=(*ni)->_activation_set[chain].end(); ai++) {
266 (*ai)->_init_refcount[chain] += 1;
270 _init_trigger_list[chain].push_back (*ni);
273 _init_finished_refcount[chain] += 1;
276 _pending_chain = chain;
278 pthread_mutex_unlock (&_swap_mutex);
287 pthread_mutex_lock (&_trigger_mutex);
288 if (_trigger_queue.size()) {
289 to_run = _trigger_queue.back();
290 _trigger_queue.pop_back();
295 int wakeup = std::min ((int) _execution_tokens, (int) _trigger_queue.size());
296 _execution_tokens -= wakeup;
298 for (int i=0; i<wakeup; i++ ) {
299 sem_post (&_execution_sem);
302 while (to_run == 0) {
303 _execution_tokens += 1;
304 pthread_mutex_unlock (&_trigger_mutex);
305 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 goes to sleep\n", pthread_self()));
306 sem_wait (&_execution_sem);
309 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 is awake\n", pthread_self()));
310 pthread_mutex_lock (&_trigger_mutex);
311 if (_trigger_queue.size()) {
312 to_run = _trigger_queue.back();
313 _trigger_queue.pop_back();
316 pthread_mutex_unlock (&_trigger_mutex);
319 to_run->finish (_current_chain);
326 if (!jack_is_realtime (AudioEngine::instance()->jack())) {
330 int priority = jack_client_real_time_priority (AudioEngine::instance()->jack());
333 struct sched_param rtparam;
335 memset (&rtparam, 0, sizeof (rtparam));
336 rtparam.sched_priority = priority;
338 pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam);
343 Graph::helper_thread()
345 ProcessThread *pt = new ProcessThread;
362 ProcessThread *pt = new ProcessThread;
368 sem_wait (&_callback_start_sem);
373 sem_post (&_callback_done_sem);
387 Graph::dump (int chain)
390 node_list_t::iterator ni;
391 node_set_t::iterator ai;
393 chain = _pending_chain;
395 DEBUG_TRACE (DEBUG::Graph, "--------------------------------------------Graph dump:\n");
396 for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) {
397 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni);
398 DEBUG_TRACE (DEBUG::Graph, string_compose ("GraphNode: %1 refcount: %2\n", rp->name().c_str(), (*ni)->_init_refcount[chain]));
399 for (ai=(*ni)->_activation_set[chain].begin(); ai!=(*ni)->_activation_set[chain].end(); ai++) {
400 DEBUG_TRACE (DEBUG::Graph, string_compose (" triggers: %1\n", boost::dynamic_pointer_cast<Route>(*ai)->name().c_str()));
404 DEBUG_TRACE (DEBUG::Graph, "------------- trigger list:\n");
405 for (ni=_init_trigger_list[chain].begin(); ni!=_init_trigger_list[chain].end(); ni++) {
406 DEBUG_TRACE (DEBUG::Graph, string_compose ("GraphNode: %1 refcount: %2\n", boost::dynamic_pointer_cast<Route>(*ni)->name().c_str(), (*ni)->_init_refcount[chain]));
409 DEBUG_TRACE (DEBUG::Graph, string_compose ("final activation refcount: %1\n", _init_finished_refcount[chain]));
414 Graph::silent_process_routes (nframes_t nframes, framepos_t start_frame, framepos_t end_frame,
415 bool can_record, bool rec_monitors_input, bool& need_butler)
417 _process_nframes = nframes;
418 _process_start_frame = start_frame;
419 _process_end_frame = end_frame;
420 _process_can_record = can_record;
421 _process_rec_monitors_input = rec_monitors_input;
423 _process_silent = true;
424 _process_noroll = false;
426 _process_need_butler = false;
429 sem_post (&_callback_start_sem);
430 sem_wait (&_callback_done_sem);
433 need_butler = _process_need_butler;
435 return _process_retval;
439 Graph::process_routes (nframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick,
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;
447 _process_declick = declick;
449 _process_silent = false;
450 _process_noroll = false;
452 _process_need_butler = false;
454 sem_post (&_callback_start_sem);
455 sem_wait (&_callback_done_sem);
457 need_butler = _process_need_butler;
459 return _process_retval;
463 Graph::routes_no_roll (nframes_t nframes, framepos_t start_frame, framepos_t end_frame,
464 bool non_rt_pending, bool can_record, int declick)
466 _process_nframes = nframes;
467 _process_start_frame = start_frame;
468 _process_end_frame = end_frame;
469 _process_can_record = can_record;
470 _process_declick = declick;
471 _process_non_rt_pending = non_rt_pending;
473 _process_silent = false;
474 _process_noroll = true;
476 _process_need_butler = false;
478 sem_post (&_callback_start_sem);
479 sem_wait (&_callback_done_sem);
481 return _process_retval;
484 Graph::process_one_route (Route* route)
486 bool need_butler = false;
491 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 runs route %2\n", pthread_self(), route->name()));
493 if (_process_silent) {
494 retval = route->silent_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_can_record, _process_rec_monitors_input, need_butler);
495 } else if (_process_noroll) {
496 route->set_pending_declick (_process_declick);
497 retval = route->no_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_non_rt_pending, _process_can_record, _process_declick);
499 route->set_pending_declick (_process_declick);
500 retval = route->roll (_process_nframes, _process_start_frame, _process_end_frame, _process_declick, _process_can_record, _process_rec_monitors_input, need_butler);
504 _process_retval = retval;
508 _process_need_butler = true;