split out ARDOUR::how_many_dsp_threads() ; fix test for whether to use use route_grap...
[ardour.git] / libs / ardour / ardour / graph.h
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
21 #ifndef __ardour_graph_h__
22 #define __ardour_graph_h__
23
24 #include <list>
25 #include <set>
26 #include <vector>
27 #include <string>
28
29 #include <boost/shared_ptr.hpp>
30
31 #include <glib/gatomic.h>
32 #include <cassert>
33
34 #include <pthread.h>
35
36 #include "pbd/semutils.h"
37
38 #include "ardour/types.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
50 typedef boost::shared_ptr<GraphNode> node_ptr_t;
51 typedef boost::shared_ptr<Graph> graph_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 Graph : public SessionHandleRef
57 {
58     public:
59         Graph (Session & session);
60
61         uint32_t threads_in_use () const { return _thread_list.size(); }
62
63         void prep();
64         void trigger (GraphNode * n);
65         void rechain (boost::shared_ptr<RouteList> r);
66
67         void dump (int chain);
68         void process();
69         void dec_ref();
70         void restart_cycle();
71
72         bool run_one();
73         void helper_thread();
74         void main_thread();
75
76         int silent_process_routes (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
77                                    bool can_record, bool rec_monitors_input, bool& need_butler);
78
79         int process_routes (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick,
80                             bool can_record, bool rec_monitors_input, bool& need_butler);
81
82         int routes_no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, 
83                             bool non_rt_pending, bool can_record, int declick);
84
85         void process_one_route (Route * route);
86
87         void clear_other_chain ();
88
89         bool in_process_thread () const;
90
91     protected:
92         virtual void session_going_away ();
93
94     private:
95         std::list<pthread_t> _thread_list;
96         volatile bool        _quit_threads;
97         PBD::ScopedConnection processor_usage_connection;
98
99         void parameter_changed (std::string);
100         void reset_thread_list ();
101         void drop_threads ();
102
103         node_list_t _nodes_rt[2];
104
105         node_list_t _init_trigger_list[2];
106
107         std::vector<GraphNode *> _trigger_queue;
108         pthread_mutex_t _trigger_mutex;
109
110         PBD::ProcessSemaphore _execution_sem;
111
112         PBD::ProcessSemaphore _callback_start_sem;
113         PBD::ProcessSemaphore _callback_done_sem;
114         PBD::ProcessSemaphore _cleanup_sem;
115
116         volatile gint _execution_tokens;
117         volatile gint _finished_refcount;
118         volatile gint _init_finished_refcount[2];
119
120         bool _graph_empty;
121
122         // chain swapping
123         Glib::Mutex  _swap_mutex;
124         Glib::Cond   _cleanup_cond;
125         volatile int _current_chain;
126         volatile int _pending_chain;
127         volatile int _setup_chain;
128
129         // parameter caches.
130         pframes_t       _process_nframes;
131         framepos_t      _process_start_frame;
132         framepos_t      _process_end_frame;
133         bool            _process_can_record;
134         bool            _process_rec_monitors_input;
135         bool            _process_non_rt_pending;
136         int             _process_declick;
137
138         bool            _process_silent;
139         bool            _process_noroll;
140         int             _process_retval;
141         bool            _process_need_butler;
142 };
143
144 } // namespace 
145
146 #endif /* __ardour_graph_h__ */