7c1ac085693ac4fdc821ad0df51f4802b9a2e741
[babeltrace.git] / include / babeltrace / graph / graph-internal.h
1 #ifndef BABELTRACE_GRAPH_GRAPH_INTERNAL_H
2 #define BABELTRACE_GRAPH_GRAPH_INTERNAL_H
3
4 /*
5 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
6 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #include <babeltrace/graph/graph.h>
28 #include <babeltrace/graph/connection-internal.h>
29 #include <babeltrace/graph/message-const.h>
30 #include <babeltrace/babeltrace-internal.h>
31 #include <babeltrace/object-internal.h>
32 #include <babeltrace/object-pool-internal.h>
33 #include <babeltrace/assert-internal.h>
34 #include <stdlib.h>
35 #include <glib.h>
36
37 struct bt_component;
38 struct bt_port;
39
40 struct bt_graph {
41 /**
42 * A component graph contains components and point-to-point connection
43 * between these components.
44 *
45 * In terms of ownership:
46 * 1) The graph is the components' parent,
47 * 2) The graph is the connnections' parent,
48 * 3) Components share the ownership of their connections,
49 * 4) A connection holds weak references to its two component endpoints.
50 */
51 struct bt_object base;
52
53 /* Array of pointers to bt_connection. */
54 GPtrArray *connections;
55 /* Array of pointers to bt_component. */
56 GPtrArray *components;
57 /* Queue of pointers (weak references) to sink bt_components. */
58 GQueue *sinks_to_consume;
59
60 bool canceled;
61 bool in_remove_listener;
62 bool has_sink;
63
64 /*
65 * If this is false, then the public API's consuming
66 * functions (bt_graph_consume() and bt_graph_run()) return
67 * BT_GRAPH_STATUS_CANNOT_CONSUME. The internal "no check"
68 * functions always work.
69 *
70 * In bt_port_output_message_iterator_create(), on success,
71 * this flag is cleared so that the iterator remains the only
72 * consumer for the graph's lifetime.
73 */
74 bool can_consume;
75
76 struct {
77 GArray *source_output_port_added;
78 GArray *filter_output_port_added;
79 GArray *filter_input_port_added;
80 GArray *sink_input_port_added;
81 GArray *source_output_port_removed;
82 GArray *filter_output_port_removed;
83 GArray *filter_input_port_removed;
84 GArray *sink_input_port_removed;
85 GArray *source_filter_ports_connected;
86 GArray *source_sink_ports_connected;
87 GArray *filter_filter_ports_connected;
88 GArray *filter_sink_ports_connected;
89 GArray *source_filter_ports_disconnected;
90 GArray *source_sink_ports_disconnected;
91 GArray *filter_filter_ports_disconnected;
92 GArray *filter_sink_ports_disconnected;
93 } listeners;
94
95 /* Pool of `struct bt_message_event *` */
96 struct bt_object_pool event_msg_pool;
97
98 /* Pool of `struct bt_message_packet_beginning *` */
99 struct bt_object_pool packet_begin_msg_pool;
100
101 /* Pool of `struct bt_message_packet_end *` */
102 struct bt_object_pool packet_end_msg_pool;
103
104 /*
105 * Array of `struct bt_message *` (weak).
106 *
107 * This is an array of all the messages ever created from
108 * this graph. Some of them can be in one of the pools above,
109 * some of them can be at large. Because each message has a
110 * weak pointer to the graph containing its pool, we need to
111 * notify each message that the graph is gone on graph
112 * destruction.
113 *
114 * TODO: When we support a maximum size for object pools,
115 * add a way for a message to remove itself from this
116 * array (on destruction).
117 */
118 GPtrArray *messages;
119 };
120
121 static inline
122 void _bt_graph_set_can_consume(struct bt_graph *graph, bool can_consume)
123 {
124 BT_ASSERT(graph);
125 graph->can_consume = can_consume;
126 }
127
128 #ifdef BT_DEV_MODE
129 # define bt_graph_set_can_consume _bt_graph_set_can_consume
130 #else
131 # define bt_graph_set_can_consume(_graph, _can_consume)
132 #endif
133
134 BT_HIDDEN
135 enum bt_graph_status bt_graph_consume_sink_no_check(struct bt_graph *graph,
136 struct bt_component_sink *sink);
137
138 BT_HIDDEN
139 void bt_graph_notify_port_added(struct bt_graph *graph, struct bt_port *port);
140
141 BT_HIDDEN
142 void bt_graph_notify_port_removed(struct bt_graph *graph,
143 struct bt_component *comp, struct bt_port *port);
144
145 BT_HIDDEN
146 void bt_graph_notify_ports_connected(struct bt_graph *graph,
147 struct bt_port *upstream_port, struct bt_port *downstream_port);
148
149 BT_HIDDEN
150 void bt_graph_notify_ports_disconnected(struct bt_graph *graph,
151 struct bt_component *upstream_comp,
152 struct bt_component *downstream_comp,
153 struct bt_port *upstream_port,
154 struct bt_port *downstream_port);
155
156 BT_HIDDEN
157 void bt_graph_remove_connection(struct bt_graph *graph,
158 struct bt_connection *connection);
159
160 /*
161 * This only works with a component which is not connected at this
162 * point.
163 *
164 * Also the reference count of `component` should be 0 when you call
165 * this function, which means only `graph` owns the component, so it
166 * is safe to destroy.
167 */
168 BT_HIDDEN
169 int bt_graph_remove_unconnected_component(struct bt_graph *graph,
170 struct bt_component *component);
171
172 BT_HIDDEN
173 void bt_graph_add_message(struct bt_graph *graph,
174 struct bt_message *msg);
175
176 static inline
177 const char *bt_graph_status_string(enum bt_graph_status status)
178 {
179 switch (status) {
180 case BT_GRAPH_STATUS_CANCELED:
181 return "BT_GRAPH_STATUS_CANCELED";
182 case BT_GRAPH_STATUS_AGAIN:
183 return "BT_GRAPH_STATUS_AGAIN";
184 case BT_GRAPH_STATUS_END:
185 return "BT_GRAPH_STATUS_END";
186 case BT_GRAPH_STATUS_OK:
187 return "BT_GRAPH_STATUS_OK";
188 case BT_GRAPH_STATUS_NO_SINK:
189 return "BT_GRAPH_STATUS_NO_SINK";
190 case BT_GRAPH_STATUS_ERROR:
191 return "BT_GRAPH_STATUS_ERROR";
192 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION:
193 return "BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION";
194 case BT_GRAPH_STATUS_NOMEM:
195 return "BT_GRAPH_STATUS_NOMEM";
196 default:
197 return "(unknown)";
198 }
199 }
200
201 #endif /* BABELTRACE_GRAPH_GRAPH_INTERNAL_H */
This page took 0.036133 seconds and 3 git commands to generate.