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