clear activation_sets component of Graph, thus restoring full Route deletion
[ardour.git] / libs / ardour / graph.cc
1 /*
2   Copyright (C) 2010 Paul Davis
3   Author: Torben Hohn
4
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.
9
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.
14
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.
18
19 */
20 #include <iostream>
21 #include <stdio.h>
22 #include <cmath>
23
24 #include "pbd/compose.h"
25 #include "pbd/cpus.h"
26
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"
34
35 #include <jack/thread.h>
36
37 #include "i18n.h"
38
39 using namespace ARDOUR;
40 using namespace PBD;
41 using namespace std;
42
43
44 Graph::Graph (Session & session) 
45         : SessionHandleRef (session) 
46 {
47         pthread_mutex_init( &_trigger_mutex, NULL);
48         sem_init( &_execution_sem, 0, 0 );
49
50         sem_init( &_callback_start_sem, 0, 0 );
51         sem_init( &_callback_done_sem,  0, 0 );
52         sem_init( &_cleanup_sem,  0, 0 );
53
54         _execution_tokens = 0;
55
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;
62
63         int num_cpu = hardware_concurrency();
64         int num_threads = num_cpu;
65         int pu = Config->get_processor_usage ();
66
67         if (pu < 0) {
68                 /* pu is negative: use "pu" less cores for DSP than appear to be available
69                  */
70
71                 if (-pu < num_threads) {
72                         num_threads += pu; 
73                 } else {
74                         num_threads = 1;
75                 }
76         } else {
77                 /* use "pu" cores, if available
78                  */
79
80                 if (pu <= num_threads) {
81                         num_threads = pu;
82                 } 
83         }
84
85         info << string_compose (_("Using %2 threads on %1 CPUs"), num_cpu, num_threads) << endmsg;
86
87         _thread_list.push_back (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::main_thread, this), 100000));
88
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));
91         }
92 }
93
94 void
95 Graph::session_going_away()
96 {
97         _quit_threads = true;
98
99         for (unsigned int i=0; i<_thread_list.size(); i++) {
100                 sem_post( &_execution_sem);
101         }
102
103         sem_post( &_callback_start_sem);
104
105         for (list<pthread_t>::iterator i = _thread_list.begin(); i != _thread_list.end(); i++) {
106                 void* status;
107                 pthread_join (*i, &status);
108         }
109
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();
116 }
117
118 void
119 Graph::clear_other_chain ()
120 {
121         while (1) {
122                 pthread_mutex_lock (&_swap_mutex);
123                 if (_setup_chain != _pending_chain) {
124
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();
127                         }
128
129                         _nodes_rt[_setup_chain].clear ();
130                         _init_trigger_list[_setup_chain].clear ();
131                         pthread_mutex_unlock (&_swap_mutex);
132
133                         return;
134                 }
135                 pthread_mutex_unlock (&_swap_mutex);
136                 /* setup chain == pending chain - we have
137                    to wait till this is no longer true.
138                 */
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;
142         }
143 }
144
145 void
146 Graph::prep()
147 {
148         node_list_t::iterator i;
149         int chain;
150
151         if (pthread_mutex_trylock (&_swap_mutex) == 0) {
152                 // we got the swap mutex.
153                 if (_current_chain != _pending_chain)
154                 {
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);
160                 }
161                 pthread_mutex_unlock (&_swap_mutex);
162         }
163
164         chain = _current_chain;
165
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;
170         }
171         _finished_refcount = _init_finished_refcount[chain];
172
173         for (i=_init_trigger_list[chain].begin(); i!=_init_trigger_list[chain].end(); i++) {
174                 this->trigger( i->get() );
175         }
176 }
177
178 void
179 Graph::trigger (GraphNode* n)
180 {
181         pthread_mutex_lock (&_trigger_mutex);
182         _trigger_queue.push_back( n);
183         pthread_mutex_unlock (&_trigger_mutex);
184 }
185
186 void
187 Graph::dec_ref()
188 {
189         if (g_atomic_int_dec_and_test (&_finished_refcount)) {
190
191                 // ok... this cycle is finished now.
192                 // we are the only thread alive.
193         
194                 this->restart_cycle();
195         }
196 }
197
198 void
199 Graph::restart_cycle()
200 {
201         //printf( "cycle_done chain: %d\n", _current_chain);
202
203         // we are through. wakeup our caller.
204   again:
205         sem_post( &_callback_done_sem);
206
207         // block until we are triggered.
208         sem_wait( &_callback_start_sem);
209         if (_quit_threads)
210                 return;
211
212         //printf( "cycle_start\n" );
213
214         this->prep();
215         if (_graph_empty)
216                 goto again;
217         //printf( "cycle_start chain: %d\n", _current_chain);
218
219         // returning will restart the cycle.
220         //  starting with waking up the others.
221 }
222
223 static bool
224 is_feedback (boost::shared_ptr<RouteList> routelist, Route* from, boost::shared_ptr<Route> to)
225 {
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;
231         }
232         assert(0);
233         return false;
234 }
235
236 static bool
237 is_feedback (boost::shared_ptr<RouteList> routelist, boost::shared_ptr<Route> from, Route* to)
238 {
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;
244         }
245         assert(0);
246         return false;
247 }
248
249 void
250 Graph::rechain (boost::shared_ptr<RouteList> routelist)
251 {
252         node_list_t::iterator ni;
253
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;
258
259         _init_finished_refcount[chain] = 0;
260         _init_trigger_list[chain].clear();
261
262         _nodes_rt[chain].clear();
263
264         for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
265                 node_ptr_t n = boost::dynamic_pointer_cast<GraphNode> (*ri);
266
267                 n->_init_refcount[chain] = 0;
268                 n->_activation_set[chain].clear();
269                 _nodes_rt[chain].push_back(n);
270         }
271
272         // now add refs for the connections.
273
274         for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) {
275                 bool has_input  = false;
276                 bool has_output = false;
277
278                 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni);
279
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; 
284                                 }
285
286                                 has_output = true;
287                                 (*ni)->_activation_set[chain].insert (boost::dynamic_pointer_cast<GraphNode> (*ri) );
288                         }
289                 }
290
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;
295                                 }
296                         }
297                 }
298
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;
301                 }
302
303                 if (!has_input)
304                         _init_trigger_list[chain].push_back (*ni);
305
306                 if (!has_output)
307                         _init_finished_refcount[chain] += 1;
308         } 
309
310         _pending_chain = chain;
311         dump(chain);
312         pthread_mutex_unlock (&_swap_mutex);
313 }
314
315
316 bool
317 Graph::run_one()
318 {
319         GraphNode* to_run;
320
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;
327         }
328
329         int wakeup = min ((int) _execution_tokens, (int) _trigger_queue.size());
330         _execution_tokens -= wakeup;
331
332         for (int i=0; i<wakeup; i++ ) {
333                 sem_post (&_execution_sem);
334         }
335
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();
348                 }
349         }
350         pthread_mutex_unlock (&_trigger_mutex);
351
352         to_run->process();
353         to_run->finish (_current_chain);
354
355         return false;
356 }
357
358 static void get_rt()
359 {
360         if (!jack_is_realtime (AudioEngine::instance()->jack())) {
361                 return;
362         }
363
364         int priority = jack_client_real_time_priority (AudioEngine::instance()->jack());
365
366         if (priority) {
367                 struct sched_param rtparam;
368         
369                 memset (&rtparam, 0, sizeof (rtparam));
370                 rtparam.sched_priority = priority;
371         
372                 pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam);
373         }
374 }
375
376 void
377 Graph::helper_thread()
378 {
379         ProcessThread *pt = new ProcessThread;
380
381         pt->get_buffers();
382         get_rt();
383
384         while(1) {
385                 if (run_one()) {
386                         break;
387                 }
388         }
389
390         pt->drop_buffers();
391 }
392
393 void
394 Graph::main_thread()
395 {
396         ProcessThread *pt = new ProcessThread;
397
398         pt->get_buffers();
399         get_rt();
400
401   again:
402         sem_wait (&_callback_start_sem);
403
404         this->prep();
405
406         if (_graph_empty) {
407                 sem_post (&_callback_done_sem);
408                 goto again;
409         }
410
411         while (1) {
412                 if (run_one()) {
413                         break;
414                 }
415         }
416
417         pt->drop_buffers();
418 }
419
420 void
421 Graph::dump (int chain)
422 {
423 #ifndef NDEBUG
424         node_list_t::iterator ni;
425         node_set_t::iterator ai;
426
427         chain = _pending_chain;
428
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()));
435                 }
436         }
437
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]));
441         }
442
443         DEBUG_TRACE (DEBUG::Graph, string_compose ("final activation refcount: %1\n", _init_finished_refcount[chain]));
444 #endif
445 }
446
447 int
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)
450 {
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;
456
457         _process_silent = true;
458         _process_noroll = false;
459         _process_retval = 0;
460         _process_need_butler = false;
461
462         if (!_graph_empty) {
463                 sem_post (&_callback_start_sem);
464                 sem_wait (&_callback_done_sem);
465         }
466
467         need_butler = _process_need_butler;
468
469         return _process_retval;
470 }
471
472 int
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)
475 {
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;
482
483         _process_silent = false;
484         _process_noroll = false;
485         _process_retval = 0;
486         _process_need_butler = false;
487
488         sem_post (&_callback_start_sem);
489         sem_wait (&_callback_done_sem);
490
491         need_butler = _process_need_butler;
492
493         return _process_retval;
494 }
495
496 int
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)
499 {
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;
506
507         _process_silent = false;
508         _process_noroll = true;
509         _process_retval = 0;
510         _process_need_butler = false;
511
512         sem_post (&_callback_start_sem);
513         sem_wait (&_callback_done_sem);
514
515         return _process_retval;
516 }
517 void
518 Graph::process_one_route (Route* route)
519 {
520         bool need_butler = false;
521         int retval;
522
523         assert (route);
524
525         DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 runs route %2\n", pthread_self(), route->name()));
526
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);
535         }
536
537         if (retval) {
538                 _process_retval = retval;
539         }
540     
541         if (need_butler) {
542                 _process_need_butler = true;
543         }
544 }
545
546
547