fix crash when copy'ing latent plugins
[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 <stdio.h>
21 #include <cmath>
22
23 #include "pbd/compose.h"
24 #include "pbd/debug_rt_alloc.h"
25 #include "pbd/pthread_utils.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 "pbd/i18n.h"
36
37 using namespace ARDOUR;
38 using namespace PBD;
39 using namespace std;
40
41 #ifdef DEBUG_RT_ALLOC
42 static Graph* graph = 0;
43
44 extern "C" {
45
46 int alloc_allowed ()
47 {
48         return !graph->in_process_thread ();
49 }
50
51 }
52 #endif
53
54 Graph::Graph (Session & session)
55         : SessionHandleRef (session)
56         , _threads_active (false)
57         , _execution_sem ("graph_execution", 0)
58         , _callback_start_sem ("graph_start", 0)
59         , _callback_done_sem ("graph_done", 0)
60         , _cleanup_sem ("graph_cleanup", 0)
61 {
62         pthread_mutex_init( &_trigger_mutex, NULL);
63
64         /* XXX: rather hacky `fix' to stop _trigger_queue.push_back() allocating
65            memory in the RT thread.
66         */
67         _trigger_queue.reserve (8192);
68
69         _execution_tokens = 0;
70
71         _current_chain = 0;
72         _pending_chain = 0;
73         _setup_chain   = 1;
74         _graph_empty = true;
75
76
77         ARDOUR::AudioEngine::instance()->Running.connect_same_thread (engine_connections, boost::bind (&Graph::reset_thread_list, this));
78         ARDOUR::AudioEngine::instance()->Stopped.connect_same_thread (engine_connections, boost::bind (&Graph::engine_stopped, this));
79         ARDOUR::AudioEngine::instance()->Halted.connect_same_thread (engine_connections, boost::bind (&Graph::engine_stopped, this));
80
81         reset_thread_list ();
82
83 #ifdef DEBUG_RT_ALLOC
84         graph = this;
85         pbd_alloc_allowed = &::alloc_allowed;
86 #endif
87 }
88
89 void
90 Graph::engine_stopped ()
91 {
92         if (AudioEngine::instance()->process_thread_count() != 0) {
93                 drop_threads ();
94         }
95 }
96
97 /** Set up threads for running the graph */
98 void
99 Graph::reset_thread_list ()
100 {
101         uint32_t num_threads = how_many_dsp_threads ();
102
103         /* For now, we shouldn't be using the graph code if we only have 1 DSP thread */
104         assert (num_threads > 1);
105
106         /* don't bother doing anything here if we already have the right
107            number of threads.
108         */
109
110         if (AudioEngine::instance()->process_thread_count() == num_threads) {
111                 return;
112         }
113
114         Glib::Threads::Mutex::Lock lm (_session.engine().process_lock());
115
116         if (AudioEngine::instance()->process_thread_count() != 0) {
117                 drop_threads ();
118         }
119
120         _threads_active = true;
121
122         if (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::main_thread, this)) != 0) {
123                 throw failed_constructor ();
124         }
125
126         for (uint32_t i = 1; i < num_threads; ++i) {
127                 if (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::helper_thread, this))) {
128                         throw failed_constructor ();
129                 }
130         }
131 }
132
133 void
134 Graph::session_going_away()
135 {
136         drop_threads ();
137
138         // now drop all references on the nodes.
139         _nodes_rt[0].clear();
140         _nodes_rt[1].clear();
141         _init_trigger_list[0].clear();
142         _init_trigger_list[1].clear();
143         _trigger_queue.clear();
144 }
145
146 void
147 Graph::drop_threads ()
148 {
149         Glib::Threads::Mutex::Lock ls (_swap_mutex);
150         _threads_active = false;
151
152         uint32_t thread_count = AudioEngine::instance()->process_thread_count ();
153
154         for (unsigned int i=0; i < thread_count; i++) {
155                 pthread_mutex_lock (&_trigger_mutex);
156                 _execution_sem.signal ();
157                 pthread_mutex_unlock (&_trigger_mutex);
158         }
159
160         pthread_mutex_lock (&_trigger_mutex);
161         _callback_start_sem.signal ();
162         pthread_mutex_unlock (&_trigger_mutex);
163
164         AudioEngine::instance()->join_process_threads ();
165
166         _execution_tokens = 0;
167 }
168
169 void
170 Graph::clear_other_chain ()
171 {
172         Glib::Threads::Mutex::Lock ls (_swap_mutex);
173
174         while (1) {
175                 if (_setup_chain != _pending_chain) {
176
177                         for (node_list_t::iterator ni=_nodes_rt[_setup_chain].begin(); ni!=_nodes_rt[_setup_chain].end(); ni++) {
178                                 (*ni)->_activation_set[_setup_chain].clear();
179                         }
180
181                         _nodes_rt[_setup_chain].clear ();
182                         _init_trigger_list[_setup_chain].clear ();
183                         break;
184                 }
185                 /* setup chain == pending chain - we have
186                    to wait till this is no longer true.
187                 */
188                 _cleanup_cond.wait (_swap_mutex);
189         }
190 }
191
192 void
193 Graph::prep()
194 {
195         node_list_t::iterator i;
196         int chain;
197
198         if (_swap_mutex.trylock()) {
199                 // we got the swap mutex.
200                 if (_current_chain != _pending_chain)
201                 {
202                         // printf ("chain swap ! %d -> %d\n", _current_chain, _pending_chain);
203                         _setup_chain = _current_chain;
204                         _current_chain = _pending_chain;
205                         _cleanup_cond.signal ();
206                 }
207                 _swap_mutex.unlock ();
208         }
209
210         chain = _current_chain;
211
212         _graph_empty = true;
213         for (i=_nodes_rt[chain].begin(); i!=_nodes_rt[chain].end(); i++) {
214                 (*i)->prep( chain);
215                 _graph_empty = false;
216         }
217         _finished_refcount = _init_finished_refcount[chain];
218
219         /* Trigger the initial nodes for processing, which are the ones at the `input' end */
220         pthread_mutex_lock (&_trigger_mutex);
221         for (i=_init_trigger_list[chain].begin(); i!=_init_trigger_list[chain].end(); i++) {
222                 /* don't use ::trigger here, as we have already locked the mutex */
223                 _trigger_queue.push_back (i->get ());
224         }
225         pthread_mutex_unlock (&_trigger_mutex);
226 }
227
228 void
229 Graph::trigger (GraphNode* n)
230 {
231         pthread_mutex_lock (&_trigger_mutex);
232         _trigger_queue.push_back (n);
233         pthread_mutex_unlock (&_trigger_mutex);
234 }
235
236 /** Called when a node at the `output' end of the chain (ie one that has no-one to feed)
237  *  is finished.
238  */
239 void
240 Graph::dec_ref()
241 {
242         if (g_atomic_int_dec_and_test (const_cast<gint*> (&_finished_refcount))) {
243
244                 /* We have run all the nodes that are at the `output' end of
245                    the graph, so there is nothing more to do this time around.
246                 */
247
248                 restart_cycle ();
249         }
250 }
251
252 void
253 Graph::restart_cycle()
254 {
255         // we are through. wakeup our caller.
256
257   again:
258         _callback_done_sem.signal ();
259
260         /* Block until the a process callback triggers us */
261         _callback_start_sem.wait();
262
263         if (!_threads_active) {
264                 return;
265         }
266
267         prep ();
268
269         if (_graph_empty && _threads_active) {
270                 goto again;
271         }
272
273         // returning will restart the cycle.
274         // starting with waking up the others.
275 }
276
277 /** Rechain our stuff using a list of routes (which can be in any order) and
278  *  a directed graph of their interconnections, which is guaranteed to be
279  *  acyclic.
280  */
281
282 void
283 Graph::rechain (boost::shared_ptr<RouteList> routelist, GraphEdges const & edges)
284 {
285         Glib::Threads::Mutex::Lock ls (_swap_mutex);
286
287         int chain = _setup_chain;
288         DEBUG_TRACE (DEBUG::Graph, string_compose ("============== setup %1\n", chain));
289
290         /* This will become the number of nodes that do not feed any other node;
291            once we have processed this number of those nodes, we have finished.
292         */
293         _init_finished_refcount[chain] = 0;
294
295         /* This will become a list of nodes that are not fed by another node, ie
296            those at the `input' end.
297         */
298         _init_trigger_list[chain].clear();
299
300         _nodes_rt[chain].clear();
301
302         /* Clear things out, and make _nodes_rt[chain] a copy of routelist */
303         for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) {
304                 (*ri)->_init_refcount[chain] = 0;
305                 (*ri)->_activation_set[chain].clear();
306                 _nodes_rt[chain].push_back (*ri);
307         }
308
309         // now add refs for the connections.
310
311         for (node_list_t::iterator ni = _nodes_rt[chain].begin(); ni != _nodes_rt[chain].end(); ni++) {
312
313                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (*ni);
314
315                 /* The routes that are directly fed by r */
316                 set<GraphVertex> fed_from_r = edges.from (r);
317
318                 /* Hence whether r has an output */
319                 bool const has_output = !fed_from_r.empty ();
320
321                 /* Set up r's activation set */
322                 for (set<GraphVertex>::iterator i = fed_from_r.begin(); i != fed_from_r.end(); ++i) {
323                         r->_activation_set[chain].insert (*i);
324                 }
325
326                 /* r has an input if there are some incoming edges to r in the graph */
327                 bool const has_input = !edges.has_none_to (r);
328
329                 /* Increment the refcount of any route that we directly feed */
330                 for (node_set_t::iterator ai = r->_activation_set[chain].begin(); ai != r->_activation_set[chain].end(); ai++) {
331                         (*ai)->_init_refcount[chain] += 1;
332                 }
333
334                 if (!has_input) {
335                         /* no input, so this node needs to be triggered initially to get things going */
336                         _init_trigger_list[chain].push_back (*ni);
337                 }
338
339                 if (!has_output) {
340                         /* no output, so this is one of the nodes that we can count off to decide
341                            if we've finished
342                         */
343                         _init_finished_refcount[chain] += 1;
344                 }
345         }
346
347         _pending_chain = chain;
348         dump(chain);
349 }
350
351 /** Called by both the main thread and all helpers.
352  *  @return true to quit, false to carry on.
353  */
354 bool
355 Graph::run_one()
356 {
357         GraphNode* to_run;
358
359         pthread_mutex_lock (&_trigger_mutex);
360         if (_trigger_queue.size()) {
361                 to_run = _trigger_queue.back();
362                 _trigger_queue.pop_back();
363         } else {
364                 to_run = 0;
365         }
366
367         /* the number of threads that are asleep */
368         int et = _execution_tokens;
369         /* the number of nodes that need to be run */
370         int ts = _trigger_queue.size();
371
372         /* hence how many threads to wake up */
373         int wakeup = min (et, ts);
374         /* update the number of threads that will still be sleeping */
375         _execution_tokens -= wakeup;
376
377         DEBUG_TRACE(DEBUG::ProcessThreads, string_compose ("%1 signals %2\n", pthread_name(), wakeup));
378
379         for (int i = 0; i < wakeup; i++) {
380                 _execution_sem.signal ();
381         }
382
383         while (to_run == 0) {
384                 _execution_tokens += 1;
385                 pthread_mutex_unlock (&_trigger_mutex);
386                 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 goes to sleep\n", pthread_name()));
387                 _execution_sem.wait ();
388                 if (!_threads_active) {
389                         return true;
390                 }
391                 DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 is awake\n", pthread_name()));
392                 pthread_mutex_lock (&_trigger_mutex);
393                 if (_trigger_queue.size()) {
394                         to_run = _trigger_queue.back();
395                         _trigger_queue.pop_back();
396                 }
397         }
398         pthread_mutex_unlock (&_trigger_mutex);
399
400         to_run->process();
401         to_run->finish (_current_chain);
402
403         DEBUG_TRACE(DEBUG::ProcessThreads, string_compose ("%1 has finished run_one()\n", pthread_name()));
404
405         return !_threads_active;
406 }
407
408 void
409 Graph::helper_thread()
410 {
411         suspend_rt_malloc_checks ();
412         ProcessThread* pt = new ProcessThread ();
413         resume_rt_malloc_checks ();
414
415         pt->get_buffers();
416
417         while(1) {
418                 if (run_one()) {
419                         break;
420                 }
421         }
422
423         pt->drop_buffers();
424         delete pt;
425 }
426
427 /** Here's the main graph thread */
428 void
429 Graph::main_thread()
430 {
431         suspend_rt_malloc_checks ();
432         ProcessThread* pt = new ProcessThread ();
433         resume_rt_malloc_checks ();
434
435         pt->get_buffers();
436
437 again:
438         _callback_start_sem.wait ();
439
440         DEBUG_TRACE(DEBUG::ProcessThreads, "main thread is awake\n");
441
442         if (!_threads_active) {
443                 return;
444         }
445
446         prep ();
447
448         if (_graph_empty && _threads_active) {
449                 _callback_done_sem.signal ();
450                 DEBUG_TRACE(DEBUG::ProcessThreads, "main thread sees graph done, goes back to sleep\n");
451                 goto again;
452         }
453
454         /* This loop will run forever */
455         while (1) {
456                 DEBUG_TRACE(DEBUG::ProcessThreads, "main thread runs one graph node\n");
457                 if (run_one()) {
458                         break;
459                 }
460         }
461
462         pt->drop_buffers();
463         delete (pt);
464 }
465
466 void
467 Graph::dump (int chain)
468 {
469 #ifndef NDEBUG
470         node_list_t::iterator ni;
471         node_set_t::iterator ai;
472
473         chain = _pending_chain;
474
475         DEBUG_TRACE (DEBUG::Graph, "--------------------------------------------Graph dump:\n");
476         for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) {
477                 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni);
478                 DEBUG_TRACE (DEBUG::Graph, string_compose ("GraphNode: %1  refcount: %2\n", rp->name().c_str(), (*ni)->_init_refcount[chain]));
479                 for (ai=(*ni)->_activation_set[chain].begin(); ai!=(*ni)->_activation_set[chain].end(); ai++) {
480                         DEBUG_TRACE (DEBUG::Graph, string_compose ("  triggers: %1\n", boost::dynamic_pointer_cast<Route>(*ai)->name().c_str()));
481                 }
482         }
483
484         DEBUG_TRACE (DEBUG::Graph, "------------- trigger list:\n");
485         for (ni=_init_trigger_list[chain].begin(); ni!=_init_trigger_list[chain].end(); ni++) {
486                 DEBUG_TRACE (DEBUG::Graph, string_compose ("GraphNode: %1  refcount: %2\n", boost::dynamic_pointer_cast<Route>(*ni)->name().c_str(), (*ni)->_init_refcount[chain]));
487         }
488
489         DEBUG_TRACE (DEBUG::Graph, string_compose ("final activation refcount: %1\n", _init_finished_refcount[chain]));
490 #endif
491 }
492
493 int
494 Graph::silent_process_routes (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, bool& need_butler)
495 {
496         if (!_threads_active) return 0;
497
498         _process_nframes = nframes;
499         _process_start_frame = start_frame;
500         _process_end_frame = end_frame;
501
502         _process_silent = true;
503         _process_noroll = false;
504         _process_retval = 0;
505         _process_need_butler = false;
506
507         if (!_graph_empty) {
508                 DEBUG_TRACE(DEBUG::ProcessThreads, "wake graph for silent process\n");
509                 _callback_start_sem.signal ();
510                 _callback_done_sem.wait ();
511         }
512
513         need_butler = _process_need_butler;
514
515         return _process_retval;
516 }
517
518 int
519 Graph::process_routes (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler)
520 {
521         DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("graph execution from %1 to %2 = %3\n", start_frame, end_frame, nframes));
522
523         if (!_threads_active) return 0;
524
525         _process_nframes = nframes;
526         _process_start_frame = start_frame;
527         _process_end_frame = end_frame;
528         _process_declick = declick;
529
530         _process_silent = false;
531         _process_noroll = false;
532         _process_retval = 0;
533         _process_need_butler = false;
534
535         DEBUG_TRACE(DEBUG::ProcessThreads, "wake graph for non-silent process\n");
536         _callback_start_sem.signal ();
537         _callback_done_sem.wait ();
538
539         DEBUG_TRACE (DEBUG::ProcessThreads, "graph execution complete\n");
540
541         need_butler = _process_need_butler;
542
543         return _process_retval;
544 }
545
546 int
547 Graph::routes_no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
548                        bool non_rt_pending, int declick)
549 {
550         DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("no-roll graph execution from %1 to %2 = %3\n", start_frame, end_frame, nframes));
551
552         if (!_threads_active) return 0;
553
554         _process_nframes = nframes;
555         _process_start_frame = start_frame;
556         _process_end_frame = end_frame;
557         _process_declick = declick;
558         _process_non_rt_pending = non_rt_pending;
559
560         _process_silent = false;
561         _process_noroll = true;
562         _process_retval = 0;
563         _process_need_butler = false;
564
565         DEBUG_TRACE(DEBUG::ProcessThreads, "wake graph for no-roll process\n");
566         _callback_start_sem.signal ();
567         _callback_done_sem.wait ();
568
569         return _process_retval;
570 }
571 void
572 Graph::process_one_route (Route* route)
573 {
574         bool need_butler = false;
575         int retval;
576
577         assert (route);
578
579         DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 runs route %2\n", pthread_name(), route->name()));
580
581         if (_process_silent) {
582                 retval = route->silent_roll (_process_nframes, _process_start_frame, _process_end_frame, need_butler);
583         } else if (_process_noroll) {
584                 route->set_pending_declick (_process_declick);
585                 retval = route->no_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_non_rt_pending);
586         } else {
587                 route->set_pending_declick (_process_declick);
588                 retval = route->roll (_process_nframes, _process_start_frame, _process_end_frame, _process_declick, need_butler);
589         }
590
591         if (retval) {
592                 _process_retval = retval;
593         }
594
595         if (need_butler) {
596                 _process_need_butler = true;
597         }
598 }
599
600 bool
601 Graph::in_process_thread () const
602 {
603         return AudioEngine::instance()->in_process_thread ();
604 }