clear activation_sets component of Graph, thus restoring full Route deletion
[ardour2.git] / libs / ardour / graph.cc
blobdd56cb98805a0fd70507d8bb0fea6ff100de6c7e
1 /*
2 Copyright (C) 2010 Paul Davis
3 Author: Torben Hohn
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.
20 #include <iostream>
21 #include <stdio.h>
22 #include <cmath>
24 #include "pbd/compose.h"
25 #include "pbd/cpus.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>
37 #include "i18n.h"
39 using namespace ARDOUR;
40 using namespace PBD;
41 using namespace std;
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);
57 _current_chain = 0;
58 _pending_chain = 0;
59 _setup_chain = 1;
60 _quit_threads = false;
61 _graph_empty = true;
63 int num_cpu = hardware_concurrency();
64 int num_threads = num_cpu;
65 int pu = Config->get_processor_usage ();
67 if (pu < 0) {
68 /* pu is negative: use "pu" less cores for DSP than appear to be available
71 if (-pu < num_threads) {
72 num_threads += pu;
73 } else {
74 num_threads = 1;
76 } else {
77 /* use "pu" cores, if available
80 if (pu <= num_threads) {
81 num_threads = pu;
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));
94 void
95 Graph::session_going_away()
97 _quit_threads = true;
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++) {
106 void* status;
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();
118 void
119 Graph::clear_other_chain ()
121 while (1) {
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);
133 return;
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;
145 void
146 Graph::prep()
148 node_list_t::iterator i;
149 int chain;
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;
166 _graph_empty = true;
167 for (i=_nodes_rt[chain].begin(); i!=_nodes_rt[chain].end(); i++) {
168 (*i)->prep( chain);
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() );
178 void
179 Graph::trigger (GraphNode* n)
181 pthread_mutex_lock (&_trigger_mutex);
182 _trigger_queue.push_back( n);
183 pthread_mutex_unlock (&_trigger_mutex);
186 void
187 Graph::dec_ref()
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();
198 void
199 Graph::restart_cycle()
201 //printf( "cycle_done chain: %d\n", _current_chain);
203 // we are through. wakeup our caller.
204 again:
205 sem_post( &_callback_done_sem);
207 // block until we are triggered.
208 sem_wait( &_callback_start_sem);
209 if (_quit_threads)
210 return;
212 //printf( "cycle_start\n" );
214 this->prep();
215 if (_graph_empty)
216 goto again;
217 //printf( "cycle_start chain: %d\n", _current_chain);
219 // returning will restart the cycle.
220 // starting with waking up the others.
223 static bool
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)
228 return false;
229 if ((*ri) == to)
230 return true;
232 assert(0);
233 return false;
236 static bool
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)
241 return true;
242 if ((*ri) == from)
243 return false;
245 assert(0);
246 return false;
249 void
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)) {
283 continue;
286 has_output = true;
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())) {
294 has_input = true;
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;
303 if (!has_input)
304 _init_trigger_list[chain].push_back (*ni);
306 if (!has_output)
307 _init_finished_refcount[chain] += 1;
310 _pending_chain = chain;
311 dump(chain);
312 pthread_mutex_unlock (&_swap_mutex);
316 bool
317 Graph::run_one()
319 GraphNode* to_run;
321 pthread_mutex_lock (&_trigger_mutex);
322 if (_trigger_queue.size()) {
323 to_run = _trigger_queue.back();
324 _trigger_queue.pop_back();
325 } else {
326 to_run = 0;
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);
341 if (_quit_threads)
342 return true;
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);
352 to_run->process();
353 to_run->finish (_current_chain);
355 return false;
358 static void get_rt()
360 if (!jack_is_realtime (AudioEngine::instance()->jack())) {
361 return;
364 int priority = jack_client_real_time_priority (AudioEngine::instance()->jack());
366 if (priority) {
367 struct sched_param rtparam;
369 memset (&rtparam, 0, sizeof (rtparam));
370 rtparam.sched_priority = priority;
372 pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam);
376 void
377 Graph::helper_thread()
379 ProcessThread *pt = new ProcessThread;
381 pt->get_buffers();
382 get_rt();
384 while(1) {
385 if (run_one()) {
386 break;
390 pt->drop_buffers();
393 void
394 Graph::main_thread()
396 ProcessThread *pt = new ProcessThread;
398 pt->get_buffers();
399 get_rt();
401 again:
402 sem_wait (&_callback_start_sem);
404 this->prep();
406 if (_graph_empty) {
407 sem_post (&_callback_done_sem);
408 goto again;
411 while (1) {
412 if (run_one()) {
413 break;
417 pt->drop_buffers();
420 void
421 Graph::dump (int chain)
423 #ifndef NDEBUG
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]));
444 #endif
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;
459 _process_retval = 0;
460 _process_need_butler = false;
462 if (!_graph_empty) {
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;
485 _process_retval = 0;
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;
509 _process_retval = 0;
510 _process_need_butler = false;
512 sem_post (&_callback_start_sem);
513 sem_wait (&_callback_done_sem);
515 return _process_retval;
517 void
518 Graph::process_one_route (Route* route)
520 bool need_butler = false;
521 int retval;
523 assert (route);
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);
532 } else {
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);
537 if (retval) {
538 _process_retval = retval;
541 if (need_butler) {
542 _process_need_butler = true;