27238e5897dfe458730e7849097276148d67c546
[ardour.git] / libs / ardour / ardour / graph.h
1 /*
2     Copyright (C) 2010 Paul Davis
3     Copyright (C) 2017 Robin Gareus <robin@gareus.org>
4     Author: Torben Hohn
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21
22 #ifndef __ardour_graph_h__
23 #define __ardour_graph_h__
24
25 #include <list>
26 #include <set>
27 #include <vector>
28 #include <string>
29
30 #include <boost/shared_ptr.hpp>
31
32 #include <glib.h>
33
34 #include "pbd/semutils.h"
35
36 #include "ardour/libardour_visibility.h"
37 #include "ardour/types.h"
38 #include "ardour/audio_backend.h"
39 #include "ardour/session_handle.h"
40
41 namespace ARDOUR
42 {
43
44 class GraphNode;
45 class Graph;
46
47 class Route;
48 class Session;
49 class GraphEdges;
50
51 typedef boost::shared_ptr<GraphNode> node_ptr_t;
52
53 typedef std::list< node_ptr_t > node_list_t;
54 typedef std::set< node_ptr_t > node_set_t;
55
56 class LIBARDOUR_API Graph : public SessionHandleRef
57 {
58 public:
59         Graph (Session & session);
60
61         void trigger (GraphNode * n);
62         void rechain (boost::shared_ptr<RouteList>, GraphEdges const &);
63
64         void dump (int chain);
65         void dec_ref();
66
67         void helper_thread();
68
69         int process_routes (pframes_t nframes, samplepos_t start_sample, samplepos_t end_sample, bool& need_butler);
70
71         int routes_no_roll (pframes_t nframes, samplepos_t start_sample, samplepos_t end_sample, bool non_rt_pending );
72
73         void process_one_route (Route * route);
74
75         void clear_other_chain ();
76
77         bool in_process_thread () const;
78
79 protected:
80         virtual void session_going_away ();
81
82 private:
83         volatile bool _threads_active;
84
85         void reset_thread_list ();
86         void drop_threads ();
87         void restart_cycle();
88         bool run_one();
89         void main_thread();
90         void prep();
91
92         node_list_t _nodes_rt[2];
93
94         node_list_t _init_trigger_list[2];
95
96         std::vector<GraphNode *> _trigger_queue;
97         pthread_mutex_t          _trigger_mutex;
98
99         PBD::Semaphore _execution_sem;
100
101         /** Signalled to start a run of the graph for a process callback */
102         PBD::Semaphore _callback_start_sem;
103         PBD::Semaphore _callback_done_sem;
104
105         /** The number of processing threads that are asleep */
106         volatile gint _execution_tokens;
107         /** The number of unprocessed nodes that do not feed any other node; updated during processing */
108         volatile gint _finished_refcount;
109         /** The initial number of nodes that do not feed any other node (for each chain) */
110         volatile gint _init_finished_refcount[2];
111
112         bool _graph_empty;
113
114         /* chain swapping */
115         Glib::Threads::Mutex _swap_mutex;
116         Glib::Threads::Cond  _cleanup_cond;
117
118         volatile int _current_chain;
119         volatile int _pending_chain;
120         volatile int _setup_chain;
121
122         /* parameter caches */
123         pframes_t   _process_nframes;
124         samplepos_t _process_start_sample;
125         samplepos_t _process_end_sample;
126         bool        _process_can_record;
127         bool        _process_non_rt_pending;
128
129         bool _process_noroll;
130         int  _process_retval;
131         bool _process_need_butler;
132
133         /* engine / thread connection */
134         PBD::ScopedConnectionList engine_connections;
135         void engine_stopped ();
136 };
137
138 } // namespace
139
140 #endif /* __ardour_graph_h__ */