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
;
44 Graph::Graph (Session
& session
)
45 : SessionHandleRef (session
)
47 pthread_mutex_init( &_trigger_mutex
, NULL
);
48 sem_init( &_execution_sem
, 0, 0 );
50 sem_init( &_callback_start_sem
, 0, 0 );
51 sem_init( &_callback_done_sem
, 0, 0 );
52 sem_init( &_cleanup_sem
, 0, 0 );
54 _execution_tokens
= 0;
56 pthread_mutex_init (&_swap_mutex
, NULL
);
60 _quit_threads
= false;
63 int num_cpu
= hardware_concurrency();
64 int num_threads
= num_cpu
;
65 int pu
= Config
->get_processor_usage ();
68 /* pu is negative: use "pu" less cores for DSP than appear to be available
71 if (-pu
< num_threads
) {
77 /* use "pu" cores, if available
80 if (pu
<= num_threads
) {
85 info
<< string_compose (_("Using %2 threads on %1 CPUs"), num_cpu
, num_threads
) << endmsg
;
87 _thread_list
.push_back (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::main_thread
, this), 100000));
89 for (int i
= 1; i
< num_threads
; ++i
) {
90 _thread_list
.push_back (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::helper_thread
, this), 100000));
95 Graph::session_going_away()
99 for (unsigned int i
=0; i
<_thread_list
.size(); i
++) {
100 sem_post( &_execution_sem
);
103 sem_post( &_callback_start_sem
);
105 for (list
<pthread_t
>::iterator i
= _thread_list
.begin(); i
!= _thread_list
.end(); i
++) {
107 pthread_join (*i
, &status
);
110 // now drop all references on the nodes.
111 _nodes_rt
[0].clear();
112 _nodes_rt
[1].clear();
113 _init_trigger_list
[0].clear();
114 _init_trigger_list
[1].clear();
115 _trigger_queue
.clear();
119 Graph::clear_other_chain ()
122 pthread_mutex_lock (&_swap_mutex
);
123 if (_setup_chain
!= _pending_chain
) {
125 for (node_list_t::iterator ni
=_nodes_rt
[_setup_chain
].begin(); ni
!=_nodes_rt
[_setup_chain
].end(); ni
++) {
126 (*ni
)->_activation_set
[_setup_chain
].clear();
129 _nodes_rt
[_setup_chain
].clear ();
130 _init_trigger_list
[_setup_chain
].clear ();
131 pthread_mutex_unlock (&_swap_mutex
);
135 pthread_mutex_unlock (&_swap_mutex
);
136 /* setup chain == pending chain - we have
137 to wait till this is no longer true.
139 cerr
<< "Wait for setup != pending (currently " << _setup_chain
<< '/' << _pending_chain
<< endl
;
140 int ret
= sem_wait (&_cleanup_sem
);
141 cerr
<< " back from that wait, ret = " << ret
<< endl
;
148 node_list_t::iterator i
;
151 if (pthread_mutex_trylock (&_swap_mutex
) == 0) {
152 // we got the swap mutex.
153 if (_current_chain
!= _pending_chain
)
155 printf ("chain swap ! %d -> %d\n", _current_chain
, _pending_chain
);
156 _setup_chain
= _current_chain
;
157 _current_chain
= _pending_chain
;
158 printf ("\tNOW: setup %d current %d pending %d\n", _setup_chain
, _current_chain
, _pending_chain
);
159 sem_post (&_cleanup_sem
);
161 pthread_mutex_unlock (&_swap_mutex
);
164 chain
= _current_chain
;
167 for (i
=_nodes_rt
[chain
].begin(); i
!=_nodes_rt
[chain
].end(); i
++) {
169 _graph_empty
= false;
171 _finished_refcount
= _init_finished_refcount
[chain
];
173 for (i
=_init_trigger_list
[chain
].begin(); i
!=_init_trigger_list
[chain
].end(); i
++) {
174 this->trigger( i
->get() );
179 Graph::trigger (GraphNode
* n
)
181 pthread_mutex_lock (&_trigger_mutex
);
182 _trigger_queue
.push_back( n
);
183 pthread_mutex_unlock (&_trigger_mutex
);
189 if (g_atomic_int_dec_and_test (&_finished_refcount
)) {
191 // ok... this cycle is finished now.
192 // we are the only thread alive.
194 this->restart_cycle();
199 Graph::restart_cycle()
201 //printf( "cycle_done chain: %d\n", _current_chain);
203 // we are through. wakeup our caller.
205 sem_post( &_callback_done_sem
);
207 // block until we are triggered.
208 sem_wait( &_callback_start_sem
);
212 //printf( "cycle_start\n" );
217 //printf( "cycle_start chain: %d\n", _current_chain);
219 // returning will restart the cycle.
220 // starting with waking up the others.
224 is_feedback (boost::shared_ptr
<RouteList
> routelist
, Route
* from
, boost::shared_ptr
<Route
> to
)
226 for (RouteList::iterator ri
=routelist
->begin(); ri
!=routelist
->end(); ri
++) {
227 if ((*ri
).get() == from
)
237 is_feedback (boost::shared_ptr
<RouteList
> routelist
, boost::shared_ptr
<Route
> from
, Route
* to
)
239 for (RouteList::iterator ri
=routelist
->begin(); ri
!=routelist
->end(); ri
++) {
240 if ((*ri
).get() == to
)
250 Graph::rechain (boost::shared_ptr
<RouteList
> routelist
)
252 node_list_t::iterator ni
;
254 pthread_mutex_lock (&_swap_mutex
);
255 int chain
= _setup_chain
;
256 DEBUG_TRACE (DEBUG::Graph
, string_compose ("============== setup %1\n", chain
));
257 // set all refcounts to 0;
259 _init_finished_refcount
[chain
] = 0;
260 _init_trigger_list
[chain
].clear();
262 _nodes_rt
[chain
].clear();
264 for (RouteList::iterator ri
=routelist
->begin(); ri
!=routelist
->end(); ri
++) {
265 node_ptr_t n
= boost::dynamic_pointer_cast
<GraphNode
> (*ri
);
267 n
->_init_refcount
[chain
] = 0;
268 n
->_activation_set
[chain
].clear();
269 _nodes_rt
[chain
].push_back(n
);
272 // now add refs for the connections.
274 for (ni
=_nodes_rt
[chain
].begin(); ni
!=_nodes_rt
[chain
].end(); ni
++) {
275 bool has_input
= false;
276 bool has_output
= false;
278 boost::shared_ptr
<Route
> rp
= boost::dynamic_pointer_cast
<Route
>( *ni
);
280 for (RouteList::iterator ri
=routelist
->begin(); ri
!=routelist
->end(); ri
++) {
281 if (rp
->direct_feeds (*ri
)) {
282 if (is_feedback (routelist
, rp
.get(), *ri
)) {
287 (*ni
)->_activation_set
[chain
].insert (boost::dynamic_pointer_cast
<GraphNode
> (*ri
) );
291 for (Route::FedBy::iterator fi
=rp
->fed_by().begin(); fi
!=rp
->fed_by().end(); fi
++) {
292 if (boost::shared_ptr
<Route
> r
= fi
->r
.lock()) {
293 if (!is_feedback (routelist
, r
, rp
.get())) {
299 for (node_set_t::iterator ai
=(*ni
)->_activation_set
[chain
].begin(); ai
!=(*ni
)->_activation_set
[chain
].end(); ai
++) {
300 (*ai
)->_init_refcount
[chain
] += 1;
304 _init_trigger_list
[chain
].push_back (*ni
);
307 _init_finished_refcount
[chain
] += 1;
310 _pending_chain
= chain
;
312 pthread_mutex_unlock (&_swap_mutex
);
321 pthread_mutex_lock (&_trigger_mutex
);
322 if (_trigger_queue
.size()) {
323 to_run
= _trigger_queue
.back();
324 _trigger_queue
.pop_back();
329 int wakeup
= min ((int) _execution_tokens
, (int) _trigger_queue
.size());
330 _execution_tokens
-= wakeup
;
332 for (int i
=0; i
<wakeup
; i
++ ) {
333 sem_post (&_execution_sem
);
336 while (to_run
== 0) {
337 _execution_tokens
+= 1;
338 pthread_mutex_unlock (&_trigger_mutex
);
339 DEBUG_TRACE (DEBUG::ProcessThreads
, string_compose ("%1 goes to sleep\n", pthread_self()));
340 sem_wait (&_execution_sem
);
343 DEBUG_TRACE (DEBUG::ProcessThreads
, string_compose ("%1 is awake\n", pthread_self()));
344 pthread_mutex_lock (&_trigger_mutex
);
345 if (_trigger_queue
.size()) {
346 to_run
= _trigger_queue
.back();
347 _trigger_queue
.pop_back();
350 pthread_mutex_unlock (&_trigger_mutex
);
353 to_run
->finish (_current_chain
);
360 if (!jack_is_realtime (AudioEngine::instance()->jack())) {
364 int priority
= jack_client_real_time_priority (AudioEngine::instance()->jack());
367 struct sched_param rtparam
;
369 memset (&rtparam
, 0, sizeof (rtparam
));
370 rtparam
.sched_priority
= priority
;
372 pthread_setschedparam (pthread_self(), SCHED_FIFO
, &rtparam
);
377 Graph::helper_thread()
379 ProcessThread
*pt
= new ProcessThread
;
396 ProcessThread
*pt
= new ProcessThread
;
402 sem_wait (&_callback_start_sem
);
407 sem_post (&_callback_done_sem
);
421 Graph::dump (int chain
)
424 node_list_t::iterator ni
;
425 node_set_t::iterator ai
;
427 chain
= _pending_chain
;
429 DEBUG_TRACE (DEBUG::Graph
, "--------------------------------------------Graph dump:\n");
430 for (ni
=_nodes_rt
[chain
].begin(); ni
!=_nodes_rt
[chain
].end(); ni
++) {
431 boost::shared_ptr
<Route
> rp
= boost::dynamic_pointer_cast
<Route
>( *ni
);
432 DEBUG_TRACE (DEBUG::Graph
, string_compose ("GraphNode: %1 refcount: %2\n", rp
->name().c_str(), (*ni
)->_init_refcount
[chain
]));
433 for (ai
=(*ni
)->_activation_set
[chain
].begin(); ai
!=(*ni
)->_activation_set
[chain
].end(); ai
++) {
434 DEBUG_TRACE (DEBUG::Graph
, string_compose (" triggers: %1\n", boost::dynamic_pointer_cast
<Route
>(*ai
)->name().c_str()));
438 DEBUG_TRACE (DEBUG::Graph
, "------------- trigger list:\n");
439 for (ni
=_init_trigger_list
[chain
].begin(); ni
!=_init_trigger_list
[chain
].end(); ni
++) {
440 DEBUG_TRACE (DEBUG::Graph
, string_compose ("GraphNode: %1 refcount: %2\n", boost::dynamic_pointer_cast
<Route
>(*ni
)->name().c_str(), (*ni
)->_init_refcount
[chain
]));
443 DEBUG_TRACE (DEBUG::Graph
, string_compose ("final activation refcount: %1\n", _init_finished_refcount
[chain
]));
448 Graph::silent_process_routes (nframes_t nframes
, framepos_t start_frame
, framepos_t end_frame
,
449 bool can_record
, bool rec_monitors_input
, bool& need_butler
)
451 _process_nframes
= nframes
;
452 _process_start_frame
= start_frame
;
453 _process_end_frame
= end_frame
;
454 _process_can_record
= can_record
;
455 _process_rec_monitors_input
= rec_monitors_input
;
457 _process_silent
= true;
458 _process_noroll
= false;
460 _process_need_butler
= false;
463 sem_post (&_callback_start_sem
);
464 sem_wait (&_callback_done_sem
);
467 need_butler
= _process_need_butler
;
469 return _process_retval
;
473 Graph::process_routes (nframes_t nframes
, framepos_t start_frame
, framepos_t end_frame
, int declick
,
474 bool can_record
, bool rec_monitors_input
, bool& need_butler
)
476 _process_nframes
= nframes
;
477 _process_start_frame
= start_frame
;
478 _process_end_frame
= end_frame
;
479 _process_can_record
= can_record
;
480 _process_rec_monitors_input
= rec_monitors_input
;
481 _process_declick
= declick
;
483 _process_silent
= false;
484 _process_noroll
= false;
486 _process_need_butler
= false;
488 sem_post (&_callback_start_sem
);
489 sem_wait (&_callback_done_sem
);
491 need_butler
= _process_need_butler
;
493 return _process_retval
;
497 Graph::routes_no_roll (nframes_t nframes
, framepos_t start_frame
, framepos_t end_frame
,
498 bool non_rt_pending
, bool can_record
, int declick
)
500 _process_nframes
= nframes
;
501 _process_start_frame
= start_frame
;
502 _process_end_frame
= end_frame
;
503 _process_can_record
= can_record
;
504 _process_declick
= declick
;
505 _process_non_rt_pending
= non_rt_pending
;
507 _process_silent
= false;
508 _process_noroll
= true;
510 _process_need_butler
= false;
512 sem_post (&_callback_start_sem
);
513 sem_wait (&_callback_done_sem
);
515 return _process_retval
;
518 Graph::process_one_route (Route
* route
)
520 bool need_butler
= false;
525 DEBUG_TRACE (DEBUG::ProcessThreads
, string_compose ("%1 runs route %2\n", pthread_self(), route
->name()));
527 if (_process_silent
) {
528 retval
= route
->silent_roll (_process_nframes
, _process_start_frame
, _process_end_frame
, _process_can_record
, _process_rec_monitors_input
, need_butler
);
529 } else if (_process_noroll
) {
530 route
->set_pending_declick (_process_declick
);
531 retval
= route
->no_roll (_process_nframes
, _process_start_frame
, _process_end_frame
, _process_non_rt_pending
, _process_can_record
, _process_declick
);
533 route
->set_pending_declick (_process_declick
);
534 retval
= route
->roll (_process_nframes
, _process_start_frame
, _process_end_frame
, _process_declick
, _process_can_record
, _process_rec_monitors_input
, need_butler
);
538 _process_retval
= retval
;
542 _process_need_butler
= true;