Commit | Line | Data |
---|---|---|
c0418dd9 | 1 | /* |
7d55361f | 2 | * graph.c |
c0418dd9 JG |
3 | * |
4 | * Babeltrace Plugin Component Graph | |
5 | * | |
f60c8b34 | 6 | * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
262e5473 | 7 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> |
c0418dd9 JG |
8 | * |
9 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
10 | * of this software and associated documentation files (the "Software"), to deal | |
11 | * in the Software without restriction, including without limitation the rights | |
12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
13 | * copies of the Software, and to permit persons to whom the Software is | |
14 | * furnished to do so, subject to the following conditions: | |
15 | * | |
16 | * The above copyright notice and this permission notice shall be included in | |
17 | * all copies or substantial portions of the Software. | |
18 | * | |
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
25 | * SOFTWARE. | |
26 | */ | |
27 | ||
262e5473 PP |
28 | #define BT_LOG_TAG "GRAPH" |
29 | #include <babeltrace/lib-logging-internal.h> | |
30 | ||
b2e0c907 PP |
31 | #include <babeltrace/graph/component-internal.h> |
32 | #include <babeltrace/graph/graph-internal.h> | |
33 | #include <babeltrace/graph/connection-internal.h> | |
34 | #include <babeltrace/graph/component-sink-internal.h> | |
35 | #include <babeltrace/graph/component-source.h> | |
36 | #include <babeltrace/graph/component-filter.h> | |
37 | #include <babeltrace/graph/port.h> | |
3d9990ac | 38 | #include <babeltrace/compiler-internal.h> |
c55a9f58 | 39 | #include <babeltrace/types.h> |
f60c8b34 | 40 | #include <unistd.h> |
1bf957a0 PP |
41 | #include <glib.h> |
42 | ||
43 | struct bt_graph_listener { | |
44 | void *func; | |
45 | void *data; | |
46 | }; | |
c0418dd9 | 47 | |
f60c8b34 JG |
48 | static |
49 | void bt_graph_destroy(struct bt_object *obj) | |
c0418dd9 | 50 | { |
f60c8b34 JG |
51 | struct bt_graph *graph = container_of(obj, |
52 | struct bt_graph, base); | |
c0418dd9 | 53 | |
bd14d768 PP |
54 | /* |
55 | * The graph's reference count is 0 if we're here. Increment | |
56 | * it to avoid a double-destroy (possibly infinitely recursive) | |
57 | * in this situation: | |
58 | * | |
59 | * 1. We put and destroy a connection. | |
60 | * 2. This connection's destructor finalizes its active | |
61 | * notification iterators. | |
62 | * 3. A notification iterator's finalization function gets a | |
63 | * new reference on its component (reference count goes from | |
64 | * 0 to 1). | |
65 | * 4. Since this component's reference count goes to 1, it takes | |
66 | * a reference on its parent (this graph). This graph's | |
67 | * reference count goes from 0 to 1. | |
68 | * 5. The notification iterator's finalization function puts its | |
69 | * component reference (reference count goes from 1 to 0). | |
70 | * 6. Since this component's reference count goes from 1 to 0, | |
71 | * it puts its parent (this graph). This graph's reference | |
72 | * count goes from 1 to 0. | |
73 | * 7. Since this graph's reference count goes from 1 to 0, | |
74 | * its destructor is called (this function). | |
75 | * | |
76 | * With the incrementation below, the graph's reference count at | |
77 | * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This | |
78 | * ensures that this function is not called two times. | |
79 | */ | |
262e5473 | 80 | BT_LOGD("Destroying graph: addr=%p", graph); |
bd14d768 PP |
81 | obj->ref_count.count++; |
82 | ||
49682acd PP |
83 | /* |
84 | * Cancel the graph to disallow some operations, like creating | |
85 | * notification iterators and adding ports to components. | |
86 | */ | |
87 | (void) bt_graph_cancel(graph); | |
88 | ||
c0418dd9 | 89 | if (graph->connections) { |
262e5473 | 90 | BT_LOGD_STR("Destroying connections."); |
c0418dd9 JG |
91 | g_ptr_array_free(graph->connections, TRUE); |
92 | } | |
bd14d768 | 93 | if (graph->components) { |
262e5473 | 94 | BT_LOGD_STR("Destroying components."); |
bd14d768 PP |
95 | g_ptr_array_free(graph->components, TRUE); |
96 | } | |
f60c8b34 JG |
97 | if (graph->sinks_to_consume) { |
98 | g_queue_free(graph->sinks_to_consume); | |
c0418dd9 | 99 | } |
1bf957a0 PP |
100 | |
101 | if (graph->listeners.port_added) { | |
102 | g_array_free(graph->listeners.port_added, TRUE); | |
103 | } | |
104 | ||
105 | if (graph->listeners.port_removed) { | |
106 | g_array_free(graph->listeners.port_removed, TRUE); | |
107 | } | |
108 | ||
f345f8bb PP |
109 | if (graph->listeners.ports_connected) { |
110 | g_array_free(graph->listeners.ports_connected, TRUE); | |
1bf957a0 PP |
111 | } |
112 | ||
f345f8bb PP |
113 | if (graph->listeners.ports_disconnected) { |
114 | g_array_free(graph->listeners.ports_disconnected, TRUE); | |
1bf957a0 PP |
115 | } |
116 | ||
c0418dd9 JG |
117 | g_free(graph); |
118 | } | |
119 | ||
1bf957a0 PP |
120 | static |
121 | int init_listeners_array(GArray **listeners) | |
122 | { | |
123 | int ret = 0; | |
124 | ||
125 | assert(listeners); | |
126 | *listeners = g_array_new(FALSE, TRUE, sizeof(struct bt_graph_listener)); | |
127 | if (!*listeners) { | |
262e5473 | 128 | BT_LOGE_STR("Failed to allocate one GArray."); |
1bf957a0 PP |
129 | ret = -1; |
130 | goto end; | |
131 | } | |
132 | ||
133 | end: | |
134 | return ret; | |
135 | } | |
136 | ||
f60c8b34 | 137 | struct bt_graph *bt_graph_create(void) |
c0418dd9 | 138 | { |
f60c8b34 | 139 | struct bt_graph *graph; |
1bf957a0 | 140 | int ret; |
c0418dd9 | 141 | |
262e5473 | 142 | BT_LOGD_STR("Creating graph object."); |
f60c8b34 | 143 | graph = g_new0(struct bt_graph, 1); |
c0418dd9 | 144 | if (!graph) { |
262e5473 | 145 | BT_LOGE_STR("Failed to allocate one graph."); |
c0418dd9 JG |
146 | goto end; |
147 | } | |
148 | ||
f60c8b34 | 149 | bt_object_init(graph, bt_graph_destroy); |
c0418dd9 | 150 | |
f60c8b34 | 151 | graph->connections = g_ptr_array_new_with_free_func(bt_object_release); |
c0418dd9 | 152 | if (!graph->connections) { |
262e5473 | 153 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
c0418dd9 JG |
154 | goto error; |
155 | } | |
f60c8b34 JG |
156 | graph->components = g_ptr_array_new_with_free_func(bt_object_release); |
157 | if (!graph->components) { | |
262e5473 | 158 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
f60c8b34 JG |
159 | goto error; |
160 | } | |
161 | graph->sinks_to_consume = g_queue_new(); | |
162 | if (!graph->sinks_to_consume) { | |
262e5473 | 163 | BT_LOGE_STR("Failed to allocate one GQueue."); |
c0418dd9 JG |
164 | goto error; |
165 | } | |
1bf957a0 PP |
166 | |
167 | ret = init_listeners_array(&graph->listeners.port_added); | |
168 | if (ret) { | |
262e5473 | 169 | BT_LOGE_STR("Cannot create the \"port added\" listener array."); |
1bf957a0 PP |
170 | goto error; |
171 | } | |
172 | ||
173 | ret = init_listeners_array(&graph->listeners.port_removed); | |
174 | if (ret) { | |
262e5473 | 175 | BT_LOGE_STR("Cannot create the \"port removed\" listener array."); |
1bf957a0 PP |
176 | goto error; |
177 | } | |
178 | ||
f345f8bb | 179 | ret = init_listeners_array(&graph->listeners.ports_connected); |
1bf957a0 | 180 | if (ret) { |
262e5473 | 181 | BT_LOGE_STR("Cannot create the \"port connected\" listener array."); |
1bf957a0 PP |
182 | goto error; |
183 | } | |
184 | ||
f345f8bb | 185 | ret = init_listeners_array(&graph->listeners.ports_disconnected); |
1bf957a0 | 186 | if (ret) { |
262e5473 | 187 | BT_LOGE_STR("Cannot create the \"port disconneted\" listener array."); |
1bf957a0 PP |
188 | goto error; |
189 | } | |
190 | ||
262e5473 PP |
191 | BT_LOGD("Created graph object: addr=%p", graph); |
192 | ||
c0418dd9 JG |
193 | end: |
194 | return graph; | |
195 | error: | |
196 | BT_PUT(graph); | |
197 | goto end; | |
198 | } | |
199 | ||
a256a42d PP |
200 | enum bt_graph_status bt_graph_connect_ports(struct bt_graph *graph, |
201 | struct bt_port *upstream_port, struct bt_port *downstream_port, | |
202 | struct bt_connection **user_connection) | |
f60c8b34 | 203 | { |
a256a42d | 204 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
f60c8b34 JG |
205 | struct bt_connection *connection = NULL; |
206 | struct bt_graph *upstream_graph = NULL; | |
207 | struct bt_graph *downstream_graph = NULL; | |
208 | struct bt_component *upstream_component = NULL; | |
209 | struct bt_component *downstream_component = NULL; | |
210 | enum bt_component_status component_status; | |
c55a9f58 PP |
211 | bt_bool upstream_was_already_in_graph; |
212 | bt_bool downstream_was_already_in_graph; | |
f60c8b34 | 213 | |
262e5473 PP |
214 | if (!graph) { |
215 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
a256a42d | 216 | status = BT_GRAPH_STATUS_INVALID; |
262e5473 PP |
217 | goto end; |
218 | } | |
219 | ||
220 | if (!upstream_port) { | |
221 | BT_LOGW_STR("Invalid parameter: upstream port is NULL."); | |
a256a42d | 222 | status = BT_GRAPH_STATUS_INVALID; |
262e5473 PP |
223 | goto end; |
224 | } | |
225 | ||
226 | if (!downstream_port) { | |
227 | BT_LOGW_STR("Invalid parameter: downstream port is NULL."); | |
a256a42d | 228 | status = BT_GRAPH_STATUS_INVALID; |
f60c8b34 JG |
229 | goto end; |
230 | } | |
231 | ||
262e5473 PP |
232 | BT_LOGD("Connecting component ports within graph: " |
233 | "graph-addr=%p, " | |
234 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
235 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", | |
236 | graph, upstream_port, bt_port_get_name(upstream_port), | |
237 | downstream_port, bt_port_get_name(downstream_port)); | |
238 | ||
202a3a13 | 239 | if (graph->canceled) { |
262e5473 | 240 | BT_LOGW_STR("Invalid parameter: graph is canceled."); |
a256a42d | 241 | status = BT_GRAPH_STATUS_CANCELED; |
202a3a13 PP |
242 | goto end; |
243 | } | |
244 | ||
72b913fb | 245 | /* Ensure appropriate types for upstream and downstream ports. */ |
f60c8b34 | 246 | if (bt_port_get_type(upstream_port) != BT_PORT_TYPE_OUTPUT) { |
262e5473 | 247 | BT_LOGW_STR("Invalid parameter: upstream port is not an output port."); |
a256a42d | 248 | status = BT_GRAPH_STATUS_INVALID; |
f60c8b34 JG |
249 | goto end; |
250 | } | |
251 | if (bt_port_get_type(downstream_port) != BT_PORT_TYPE_INPUT) { | |
262e5473 | 252 | BT_LOGW_STR("Invalid parameter: downstream port is not an input port."); |
a256a42d | 253 | status = BT_GRAPH_STATUS_INVALID; |
f60c8b34 JG |
254 | goto end; |
255 | } | |
256 | ||
72b913fb | 257 | /* Ensure that both ports are currently unconnected. */ |
0d8b4d8e | 258 | if (bt_port_is_connected(upstream_port)) { |
262e5473 | 259 | BT_LOGW_STR("Invalid parameter: upstream port is already connected."); |
a256a42d | 260 | status = BT_GRAPH_STATUS_INVALID; |
72b913fb PP |
261 | goto end; |
262 | } | |
263 | ||
0d8b4d8e | 264 | if (bt_port_is_connected(downstream_port)) { |
262e5473 | 265 | BT_LOGW_STR("Invalid parameter: downstream port is already connected."); |
a256a42d | 266 | status = BT_GRAPH_STATUS_INVALID; |
72b913fb PP |
267 | goto end; |
268 | } | |
269 | ||
270 | /* | |
271 | * Ensure that both ports are still attached to their creating | |
272 | * component. | |
273 | */ | |
f60c8b34 | 274 | upstream_component = bt_port_get_component(upstream_port); |
72b913fb | 275 | if (!upstream_component) { |
262e5473 | 276 | BT_LOGW_STR("Invalid parameter: upstream port is loose (does not belong to a component)"); |
a256a42d | 277 | status = BT_GRAPH_STATUS_INVALID; |
72b913fb PP |
278 | goto end; |
279 | } | |
280 | ||
281 | downstream_component = bt_port_get_component(downstream_port); | |
282 | if (!downstream_component) { | |
262e5473 | 283 | BT_LOGW_STR("Invalid parameter: downstream port is loose (does not belong to a component)"); |
a256a42d | 284 | status = BT_GRAPH_STATUS_INVALID; |
72b913fb PP |
285 | goto end; |
286 | } | |
287 | ||
262e5473 PP |
288 | BT_LOGD("Connecting component ports: " |
289 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " | |
290 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\"", | |
291 | upstream_component, bt_component_get_name(upstream_component), | |
292 | downstream_component, bt_component_get_name(downstream_component)); | |
293 | ||
72b913fb | 294 | /* Ensure the components are not already part of another graph. */ |
f60c8b34 JG |
295 | upstream_graph = bt_component_get_graph(upstream_component); |
296 | if (upstream_graph && (graph != upstream_graph)) { | |
262e5473 PP |
297 | BT_LOGW("Invalid parameter: upstream port's component is already part of another graph: " |
298 | "other-graph-addr=%p", upstream_graph); | |
a256a42d PP |
299 | status = BT_GRAPH_STATUS_ALREADY_IN_A_GRAPH; |
300 | goto end; | |
f60c8b34 | 301 | } |
ffeb0eed | 302 | upstream_was_already_in_graph = (graph == upstream_graph); |
f60c8b34 JG |
303 | downstream_graph = bt_component_get_graph(downstream_component); |
304 | if (downstream_graph && (graph != downstream_graph)) { | |
262e5473 PP |
305 | BT_LOGW("Invalid parameter: downstream port's component is already part of another graph: " |
306 | "other-graph-addr=%p", downstream_graph); | |
a256a42d PP |
307 | status = BT_GRAPH_STATUS_ALREADY_IN_A_GRAPH; |
308 | goto end; | |
f60c8b34 | 309 | } |
ffeb0eed | 310 | downstream_was_already_in_graph = (graph == downstream_graph); |
f60c8b34 | 311 | |
0d8b4d8e PP |
312 | /* |
313 | * At this point the ports are not connected yet. Both | |
314 | * components need to accept an eventual connection to their | |
315 | * port by the other port before we continue. | |
316 | */ | |
262e5473 | 317 | BT_LOGD_STR("Asking upstream component to accept the connection."); |
0d8b4d8e PP |
318 | component_status = bt_component_accept_port_connection( |
319 | upstream_component, upstream_port, downstream_port); | |
320 | if (component_status != BT_COMPONENT_STATUS_OK) { | |
262e5473 PP |
321 | if (component_status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) { |
322 | BT_LOGD_STR("Upstream component refused the connection."); | |
323 | } else { | |
324 | BT_LOGW("Cannot ask upstream component to accept the connection: " | |
325 | "status=%s", bt_component_status_string(component_status)); | |
326 | } | |
327 | ||
a256a42d PP |
328 | status = bt_graph_status_from_component_status( |
329 | component_status); | |
330 | goto end; | |
0d8b4d8e | 331 | } |
262e5473 PP |
332 | |
333 | BT_LOGD_STR("Asking downstream component to accept the connection."); | |
0d8b4d8e PP |
334 | component_status = bt_component_accept_port_connection( |
335 | downstream_component, downstream_port, upstream_port); | |
336 | if (component_status != BT_COMPONENT_STATUS_OK) { | |
262e5473 PP |
337 | if (component_status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) { |
338 | BT_LOGD_STR("Downstream component refused the connection."); | |
339 | } else { | |
340 | BT_LOGW("Cannot ask downstream component to accept the connection: " | |
341 | "status=%s", bt_component_status_string(component_status)); | |
342 | } | |
343 | ||
a256a42d PP |
344 | status = bt_graph_status_from_component_status( |
345 | component_status); | |
346 | goto end; | |
0d8b4d8e PP |
347 | } |
348 | ||
262e5473 | 349 | BT_LOGD_STR("Creating connection."); |
f60c8b34 JG |
350 | connection = bt_connection_create(graph, upstream_port, |
351 | downstream_port); | |
352 | if (!connection) { | |
262e5473 | 353 | BT_LOGW("Cannot create connection object."); |
a256a42d PP |
354 | status = BT_GRAPH_STATUS_NOMEM; |
355 | goto end; | |
f60c8b34 JG |
356 | } |
357 | ||
262e5473 PP |
358 | BT_LOGD("Connection object created: conn-addr=%p", connection); |
359 | ||
f60c8b34 | 360 | /* |
72b913fb PP |
361 | * Ownership of upstream_component/downstream_component and of |
362 | * the connection object is transferred to the graph. | |
f60c8b34 JG |
363 | */ |
364 | g_ptr_array_add(graph->connections, connection); | |
ffeb0eed JG |
365 | |
366 | if (!upstream_was_already_in_graph) { | |
367 | g_ptr_array_add(graph->components, upstream_component); | |
368 | bt_component_set_graph(upstream_component, graph); | |
369 | } | |
370 | if (!downstream_was_already_in_graph) { | |
371 | g_ptr_array_add(graph->components, downstream_component); | |
372 | bt_component_set_graph(downstream_component, graph); | |
373 | if (bt_component_get_class_type(downstream_component) == | |
374 | BT_COMPONENT_CLASS_TYPE_SINK) { | |
375 | g_queue_push_tail(graph->sinks_to_consume, | |
376 | downstream_component); | |
377 | } | |
f60c8b34 JG |
378 | } |
379 | ||
380 | /* | |
0d8b4d8e PP |
381 | * The graph is now the parent of these components which |
382 | * garantees their existence for the duration of the graph's | |
383 | * lifetime. | |
f60c8b34 | 384 | */ |
f60c8b34 JG |
385 | |
386 | /* | |
0d8b4d8e | 387 | * Notify both components that their port is connected. |
f60c8b34 | 388 | */ |
262e5473 | 389 | BT_LOGD_STR("Notifying upstream component that its port is connected."); |
0d8b4d8e PP |
390 | bt_component_port_connected(upstream_component, upstream_port, |
391 | downstream_port); | |
262e5473 | 392 | BT_LOGD_STR("Notifying downstream component that its port is connected."); |
0d8b4d8e PP |
393 | bt_component_port_connected(downstream_component, downstream_port, |
394 | upstream_port); | |
1bf957a0 PP |
395 | |
396 | /* | |
0d8b4d8e | 397 | * Notify the graph's creator that both ports are connected. |
1bf957a0 | 398 | */ |
262e5473 | 399 | BT_LOGD_STR("Notifying graph's user that new component ports are connected."); |
f345f8bb | 400 | bt_graph_notify_ports_connected(graph, upstream_port, downstream_port); |
262e5473 PP |
401 | BT_LOGD("Connected component ports within graph: " |
402 | "graph-addr=%p, " | |
403 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " | |
404 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\", " | |
405 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
406 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", | |
407 | graph, | |
408 | upstream_component, bt_component_get_name(upstream_component), | |
409 | downstream_component, bt_component_get_name(downstream_component), | |
410 | upstream_port, bt_port_get_name(upstream_port), | |
411 | downstream_port, bt_port_get_name(downstream_port)); | |
1bf957a0 | 412 | |
a256a42d | 413 | if (user_connection) { |
1a6a376a PP |
414 | /* Move reference to user */ |
415 | *user_connection = connection; | |
416 | connection = NULL; | |
a256a42d PP |
417 | } |
418 | ||
f60c8b34 JG |
419 | end: |
420 | bt_put(upstream_graph); | |
421 | bt_put(downstream_graph); | |
3eeacbb9 JG |
422 | bt_put(upstream_component); |
423 | bt_put(downstream_component); | |
a256a42d PP |
424 | bt_put(connection); |
425 | return status; | |
f60c8b34 JG |
426 | } |
427 | ||
851b70bd PP |
428 | static |
429 | enum bt_graph_status bt_graph_consume_no_check(struct bt_graph *graph) | |
c0418dd9 | 430 | { |
f60c8b34 | 431 | struct bt_component *sink; |
72b913fb PP |
432 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
433 | enum bt_component_status comp_status; | |
f60c8b34 JG |
434 | GList *current_node; |
435 | ||
851b70bd | 436 | BT_LOGV("Making next sink consume: addr=%p", graph); |
202a3a13 | 437 | |
f60c8b34 | 438 | if (g_queue_is_empty(graph->sinks_to_consume)) { |
262e5473 | 439 | BT_LOGV_STR("Graph's sink queue is empty: end of graph."); |
72b913fb | 440 | status = BT_GRAPH_STATUS_END; |
f60c8b34 JG |
441 | goto end; |
442 | } | |
443 | ||
444 | current_node = g_queue_pop_head_link(graph->sinks_to_consume); | |
445 | sink = current_node->data; | |
262e5473 PP |
446 | BT_LOGV("Chose next sink to consume: comp-addr=%p, comp-name=\"%s\"", |
447 | sink, bt_component_get_name(sink)); | |
72b913fb | 448 | comp_status = bt_component_sink_consume(sink); |
262e5473 PP |
449 | BT_LOGV("Consumed from sink: status=%s", |
450 | bt_component_status_string(comp_status)); | |
72b913fb PP |
451 | switch (comp_status) { |
452 | case BT_COMPONENT_STATUS_OK: | |
453 | break; | |
454 | case BT_COMPONENT_STATUS_END: | |
455 | status = BT_GRAPH_STATUS_END; | |
456 | break; | |
457 | case BT_COMPONENT_STATUS_AGAIN: | |
458 | status = BT_GRAPH_STATUS_AGAIN; | |
459 | break; | |
460 | case BT_COMPONENT_STATUS_INVALID: | |
461 | status = BT_GRAPH_STATUS_INVALID; | |
462 | break; | |
463 | default: | |
464 | status = BT_GRAPH_STATUS_ERROR; | |
465 | break; | |
466 | } | |
467 | ||
468 | if (status != BT_GRAPH_STATUS_END) { | |
f60c8b34 JG |
469 | g_queue_push_tail_link(graph->sinks_to_consume, current_node); |
470 | goto end; | |
471 | } | |
472 | ||
473 | /* End reached, the node is not added back to the queue and free'd. */ | |
474 | g_queue_delete_link(graph->sinks_to_consume, current_node); | |
475 | ||
476 | /* Don't forward an END status if there are sinks left to consume. */ | |
477 | if (!g_queue_is_empty(graph->sinks_to_consume)) { | |
478 | status = BT_GRAPH_STATUS_OK; | |
479 | goto end; | |
480 | } | |
481 | end: | |
262e5473 | 482 | BT_LOGV("Graph consumed: status=%s", bt_graph_status_string(status)); |
f60c8b34 | 483 | return status; |
c0418dd9 JG |
484 | } |
485 | ||
851b70bd PP |
486 | enum bt_graph_status bt_graph_consume(struct bt_graph *graph) |
487 | { | |
488 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; | |
489 | ||
490 | if (!graph) { | |
491 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
492 | status = BT_GRAPH_STATUS_INVALID; | |
493 | goto end; | |
494 | } | |
495 | ||
496 | if (graph->canceled) { | |
497 | BT_LOGW("Invalid parameter: graph is canceled: " | |
498 | "graph-addr=%p", graph); | |
499 | status = BT_GRAPH_STATUS_CANCELED; | |
500 | goto end; | |
501 | } | |
502 | ||
503 | status = bt_graph_consume_no_check(graph); | |
504 | ||
505 | end: | |
506 | return status; | |
507 | } | |
508 | ||
72b913fb | 509 | enum bt_graph_status bt_graph_run(struct bt_graph *graph) |
f60c8b34 | 510 | { |
72b913fb | 511 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
f60c8b34 JG |
512 | |
513 | if (!graph) { | |
262e5473 | 514 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
72b913fb | 515 | status = BT_GRAPH_STATUS_INVALID; |
202a3a13 | 516 | goto end; |
f60c8b34 JG |
517 | } |
518 | ||
851b70bd PP |
519 | if (graph->canceled) { |
520 | BT_LOGW("Invalid parameter: graph is canceled: " | |
521 | "graph-addr=%p", graph); | |
522 | status = BT_GRAPH_STATUS_CANCELED; | |
523 | goto end; | |
524 | } | |
525 | ||
262e5473 PP |
526 | BT_LOGV("Running graph: addr=%p", graph); |
527 | ||
f60c8b34 | 528 | do { |
851b70bd PP |
529 | /* |
530 | * Check if the graph is canceled at each iteration. If | |
531 | * the graph was canceled by another thread or by a | |
532 | * signal, this is not a warning nor an error, it was | |
533 | * intentional: log with a DEBUG level only. | |
534 | */ | |
535 | if (graph->canceled) { | |
536 | BT_LOGD("Stopping the graph: graph is canceled: " | |
537 | "graph-addr=%p", graph); | |
538 | status = BT_GRAPH_STATUS_CANCELED; | |
539 | goto end; | |
540 | } | |
541 | ||
72b913fb PP |
542 | status = bt_graph_consume(graph); |
543 | if (status == BT_GRAPH_STATUS_AGAIN) { | |
f60c8b34 | 544 | /* |
202a3a13 PP |
545 | * If AGAIN is received and there are multiple |
546 | * sinks, go ahead and consume from the next | |
547 | * sink. | |
f60c8b34 | 548 | * |
202a3a13 PP |
549 | * However, in the case where a single sink is |
550 | * left, the caller can decide to busy-wait and | |
551 | * call bt_graph_run() continuously until the | |
552 | * source is ready or it can decide to sleep for | |
553 | * an arbitrary amount of time. | |
f60c8b34 JG |
554 | */ |
555 | if (graph->sinks_to_consume->length > 1) { | |
72b913fb | 556 | status = BT_GRAPH_STATUS_OK; |
f60c8b34 JG |
557 | } |
558 | } | |
72b913fb | 559 | } while (status == BT_GRAPH_STATUS_OK); |
f60c8b34 JG |
560 | |
561 | if (g_queue_is_empty(graph->sinks_to_consume)) { | |
72b913fb | 562 | status = BT_GRAPH_STATUS_END; |
f60c8b34 | 563 | } |
262e5473 | 564 | |
202a3a13 | 565 | end: |
262e5473 | 566 | BT_LOGV("Graph ran: status=%s", bt_graph_status_string(status)); |
72b913fb | 567 | return status; |
f60c8b34 | 568 | } |
1bf957a0 PP |
569 | |
570 | static | |
0d107cdd | 571 | int add_listener(GArray *listeners, void *func, void *data) |
1bf957a0 PP |
572 | { |
573 | struct bt_graph_listener listener = { | |
574 | .func = func, | |
575 | .data = data, | |
576 | }; | |
577 | ||
578 | g_array_append_val(listeners, listener); | |
0d107cdd | 579 | return listeners->len - 1; |
1bf957a0 PP |
580 | } |
581 | ||
0d107cdd | 582 | int bt_graph_add_port_added_listener( |
1bf957a0 PP |
583 | struct bt_graph *graph, |
584 | bt_graph_port_added_listener listener, void *data) | |
585 | { | |
0d107cdd | 586 | int ret; |
1bf957a0 | 587 | |
262e5473 PP |
588 | if (!graph) { |
589 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
0d107cdd | 590 | ret = -1; |
262e5473 PP |
591 | goto end; |
592 | } | |
593 | ||
594 | if (!listener) { | |
595 | BT_LOGW_STR("Invalid parameter: listener is NULL."); | |
0d107cdd | 596 | ret = -1; |
1bf957a0 PP |
597 | goto end; |
598 | } | |
599 | ||
0d107cdd | 600 | ret = add_listener(graph->listeners.port_added, listener, data); |
262e5473 | 601 | BT_LOGV("Added \"port added\" listener to graph: " |
0d107cdd PP |
602 | "graph-addr=%p, listener-addr=%p, pos=%d", |
603 | graph, listener, ret); | |
1bf957a0 PP |
604 | |
605 | end: | |
0d107cdd | 606 | return ret; |
1bf957a0 PP |
607 | } |
608 | ||
0d107cdd | 609 | int bt_graph_add_port_removed_listener( |
1bf957a0 PP |
610 | struct bt_graph *graph, |
611 | bt_graph_port_removed_listener listener, void *data) | |
612 | { | |
0d107cdd | 613 | int ret; |
1bf957a0 | 614 | |
262e5473 PP |
615 | if (!graph) { |
616 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
0d107cdd | 617 | ret = -1; |
262e5473 PP |
618 | goto end; |
619 | } | |
620 | ||
621 | if (!listener) { | |
622 | BT_LOGW_STR("Invalid parameter: listener is NULL."); | |
0d107cdd | 623 | ret = -1; |
1bf957a0 PP |
624 | goto end; |
625 | } | |
626 | ||
0d107cdd | 627 | ret = add_listener(graph->listeners.port_removed, listener, data); |
262e5473 | 628 | BT_LOGV("Added \"port removed\" listener to graph: " |
0d107cdd PP |
629 | "graph-addr=%p, listener-addr=%p, pos=%d", |
630 | graph, listener, ret); | |
1bf957a0 PP |
631 | |
632 | end: | |
0d107cdd | 633 | return ret; |
1bf957a0 PP |
634 | } |
635 | ||
0d107cdd | 636 | int bt_graph_add_ports_connected_listener( |
1bf957a0 | 637 | struct bt_graph *graph, |
f345f8bb | 638 | bt_graph_ports_connected_listener listener, void *data) |
1bf957a0 | 639 | { |
0d107cdd | 640 | int ret; |
1bf957a0 | 641 | |
262e5473 PP |
642 | if (!graph) { |
643 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
0d107cdd | 644 | ret = -1; |
262e5473 PP |
645 | goto end; |
646 | } | |
647 | ||
648 | if (!listener) { | |
649 | BT_LOGW_STR("Invalid parameter: listener is NULL."); | |
0d107cdd | 650 | ret = -1; |
1bf957a0 PP |
651 | goto end; |
652 | } | |
653 | ||
0d107cdd | 654 | ret = add_listener(graph->listeners.ports_connected, listener, data); |
262e5473 | 655 | BT_LOGV("Added \"port connected\" listener to graph: " |
0d107cdd PP |
656 | "graph-addr=%p, listener-addr=%p, pos=%d", |
657 | graph, listener, ret); | |
1bf957a0 PP |
658 | |
659 | end: | |
0d107cdd | 660 | return ret; |
1bf957a0 PP |
661 | } |
662 | ||
0d107cdd | 663 | int bt_graph_add_ports_disconnected_listener( |
1bf957a0 | 664 | struct bt_graph *graph, |
f345f8bb | 665 | bt_graph_ports_disconnected_listener listener, void *data) |
1bf957a0 | 666 | { |
0d107cdd | 667 | int ret; |
1bf957a0 | 668 | |
262e5473 PP |
669 | if (!graph) { |
670 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
0d107cdd | 671 | ret = -1; |
262e5473 PP |
672 | goto end; |
673 | } | |
674 | ||
675 | if (!listener) { | |
676 | BT_LOGW_STR("Invalid parameter: listener is NULL."); | |
0d107cdd | 677 | ret = -1; |
1bf957a0 PP |
678 | goto end; |
679 | } | |
680 | ||
0d107cdd | 681 | ret = add_listener(graph->listeners.ports_disconnected, listener, data); |
262e5473 | 682 | BT_LOGV("Added \"port disconnected\" listener to graph: " |
0d107cdd PP |
683 | "graph-addr=%p, listener-addr=%p, pos=%d", |
684 | graph, listener, ret); | |
1bf957a0 PP |
685 | |
686 | end: | |
0d107cdd | 687 | return ret; |
1bf957a0 PP |
688 | } |
689 | ||
690 | BT_HIDDEN | |
691 | void bt_graph_notify_port_added(struct bt_graph *graph, struct bt_port *port) | |
692 | { | |
693 | size_t i; | |
694 | ||
262e5473 PP |
695 | BT_LOGV("Notifying graph listeners that a port was added: " |
696 | "graph-addr=%p, port-addr=%p, port-name=\"%s\"", | |
697 | graph, port, bt_port_get_name(port)); | |
698 | ||
1bf957a0 PP |
699 | for (i = 0; i < graph->listeners.port_added->len; i++) { |
700 | struct bt_graph_listener listener = | |
701 | g_array_index(graph->listeners.port_added, | |
702 | struct bt_graph_listener, i); | |
703 | bt_graph_port_added_listener func = listener.func; | |
704 | ||
705 | assert(func); | |
706 | func(port, listener.data); | |
707 | } | |
708 | } | |
709 | ||
710 | BT_HIDDEN | |
711 | void bt_graph_notify_port_removed(struct bt_graph *graph, | |
712 | struct bt_component *comp, struct bt_port *port) | |
713 | { | |
714 | size_t i; | |
715 | ||
262e5473 PP |
716 | BT_LOGV("Notifying graph listeners that a port was removed: " |
717 | "graph-addr=%p, port-addr=%p, port-name=\"%s\"", | |
718 | graph, port, bt_port_get_name(port)); | |
719 | ||
1bf957a0 PP |
720 | for (i = 0; i < graph->listeners.port_removed->len; i++) { |
721 | struct bt_graph_listener listener = | |
722 | g_array_index(graph->listeners.port_removed, | |
723 | struct bt_graph_listener, i); | |
724 | bt_graph_port_removed_listener func = listener.func; | |
725 | ||
726 | assert(func); | |
727 | func(comp, port, listener.data); | |
728 | } | |
729 | } | |
730 | ||
731 | BT_HIDDEN | |
f345f8bb PP |
732 | void bt_graph_notify_ports_connected(struct bt_graph *graph, |
733 | struct bt_port *upstream_port, struct bt_port *downstream_port) | |
1bf957a0 PP |
734 | { |
735 | size_t i; | |
736 | ||
262e5473 PP |
737 | BT_LOGV("Notifying graph listeners that two ports were connected: " |
738 | "graph-addr=%p, " | |
739 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
740 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", | |
741 | graph, upstream_port, bt_port_get_name(upstream_port), | |
742 | downstream_port, bt_port_get_name(downstream_port)); | |
743 | ||
f345f8bb | 744 | for (i = 0; i < graph->listeners.ports_connected->len; i++) { |
1bf957a0 | 745 | struct bt_graph_listener listener = |
f345f8bb | 746 | g_array_index(graph->listeners.ports_connected, |
1bf957a0 | 747 | struct bt_graph_listener, i); |
f345f8bb | 748 | bt_graph_ports_connected_listener func = listener.func; |
1bf957a0 PP |
749 | |
750 | assert(func); | |
f345f8bb | 751 | func(upstream_port, downstream_port, listener.data); |
1bf957a0 PP |
752 | } |
753 | } | |
754 | ||
755 | BT_HIDDEN | |
f345f8bb PP |
756 | void bt_graph_notify_ports_disconnected(struct bt_graph *graph, |
757 | struct bt_component *upstream_comp, | |
758 | struct bt_component *downstream_comp, | |
759 | struct bt_port *upstream_port, struct bt_port *downstream_port) | |
1bf957a0 PP |
760 | { |
761 | size_t i; | |
762 | ||
262e5473 PP |
763 | BT_LOGV("Notifying graph listeners that two ports were disconnected: " |
764 | "graph-addr=%p, " | |
765 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
766 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", | |
767 | graph, upstream_port, bt_port_get_name(upstream_port), | |
768 | downstream_port, bt_port_get_name(downstream_port)); | |
769 | ||
f345f8bb | 770 | for (i = 0; i < graph->listeners.ports_disconnected->len; i++) { |
1bf957a0 | 771 | struct bt_graph_listener listener = |
f345f8bb | 772 | g_array_index(graph->listeners.ports_disconnected, |
1bf957a0 | 773 | struct bt_graph_listener, i); |
f345f8bb | 774 | bt_graph_ports_disconnected_listener func = listener.func; |
1bf957a0 PP |
775 | |
776 | assert(func); | |
f345f8bb PP |
777 | func(upstream_comp, downstream_comp, upstream_port, |
778 | downstream_port, listener.data); | |
1bf957a0 PP |
779 | } |
780 | } | |
202a3a13 PP |
781 | |
782 | extern enum bt_graph_status bt_graph_cancel(struct bt_graph *graph) | |
783 | { | |
784 | enum bt_graph_status ret = BT_GRAPH_STATUS_OK; | |
785 | ||
786 | if (!graph) { | |
262e5473 | 787 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
202a3a13 PP |
788 | ret = BT_GRAPH_STATUS_INVALID; |
789 | goto end; | |
790 | } | |
791 | ||
792 | graph->canceled = BT_TRUE; | |
262e5473 | 793 | BT_LOGV("Canceled graph: addr=%p", graph); |
202a3a13 PP |
794 | |
795 | end: | |
796 | return ret; | |
797 | } | |
798 | ||
799 | extern bt_bool bt_graph_is_canceled(struct bt_graph *graph) | |
800 | { | |
801 | return graph ? graph->canceled : BT_FALSE; | |
802 | } | |
f167d3c0 PP |
803 | |
804 | BT_HIDDEN | |
805 | void bt_graph_remove_connection(struct bt_graph *graph, | |
806 | struct bt_connection *connection) | |
807 | { | |
808 | assert(graph); | |
809 | assert(connection); | |
262e5473 PP |
810 | BT_LOGV("Removing graph's connection: graph-addr=%p, conn-addr=%p", |
811 | graph, connection); | |
f167d3c0 PP |
812 | g_ptr_array_remove(graph->connections, connection); |
813 | } |