lib: add internal object pool API and use it; adapt plugins/tests
[babeltrace.git] / lib / graph / graph.c
CommitLineData
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>
36712f1d
PP
40#include <babeltrace/values.h>
41#include <babeltrace/values-internal.h>
f6ccaed9
PP
42#include <babeltrace/assert-internal.h>
43#include <babeltrace/assert-pre-internal.h>
f60c8b34 44#include <unistd.h>
1bf957a0
PP
45#include <glib.h>
46
47struct bt_graph_listener {
48 void *func;
8cc092c9 49 bt_graph_listener_removed removed;
1bf957a0
PP
50 void *data;
51};
c0418dd9 52
8cc092c9
PP
53static
54int init_listeners_array(GArray **listeners)
55{
56 int ret = 0;
57
f6ccaed9 58 BT_ASSERT(listeners);
8cc092c9
PP
59 *listeners = g_array_new(FALSE, TRUE, sizeof(struct bt_graph_listener));
60 if (!*listeners) {
61 BT_LOGE_STR("Failed to allocate one GArray.");
62 ret = -1;
63 goto end;
64 }
65
66end:
67 return ret;
68}
69
70static
71void call_remove_listeners(GArray *listeners)
72{
73 size_t i;
74
75 for (i = 0; i < listeners->len; i++) {
76 struct bt_graph_listener listener =
77 g_array_index(listeners, struct bt_graph_listener, i);
78
79 if (listener.removed) {
80 listener.removed(listener.data);
81 }
82 }
83}
84
f60c8b34
JG
85static
86void bt_graph_destroy(struct bt_object *obj)
c0418dd9 87{
f60c8b34
JG
88 struct bt_graph *graph = container_of(obj,
89 struct bt_graph, base);
c0418dd9 90
bd14d768
PP
91 /*
92 * The graph's reference count is 0 if we're here. Increment
93 * it to avoid a double-destroy (possibly infinitely recursive)
94 * in this situation:
95 *
96 * 1. We put and destroy a connection.
97 * 2. This connection's destructor finalizes its active
98 * notification iterators.
99 * 3. A notification iterator's finalization function gets a
100 * new reference on its component (reference count goes from
101 * 0 to 1).
102 * 4. Since this component's reference count goes to 1, it takes
103 * a reference on its parent (this graph). This graph's
104 * reference count goes from 0 to 1.
105 * 5. The notification iterator's finalization function puts its
106 * component reference (reference count goes from 1 to 0).
107 * 6. Since this component's reference count goes from 1 to 0,
108 * it puts its parent (this graph). This graph's reference
109 * count goes from 1 to 0.
110 * 7. Since this graph's reference count goes from 1 to 0,
111 * its destructor is called (this function).
112 *
113 * With the incrementation below, the graph's reference count at
114 * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This
115 * ensures that this function is not called two times.
116 */
262e5473 117 BT_LOGD("Destroying graph: addr=%p", graph);
bd14d768
PP
118 obj->ref_count.count++;
119
49682acd
PP
120 /*
121 * Cancel the graph to disallow some operations, like creating
122 * notification iterators and adding ports to components.
123 */
124 (void) bt_graph_cancel(graph);
125
8cc092c9
PP
126 /* Call all remove listeners */
127 call_remove_listeners(graph->listeners.port_added);
128 call_remove_listeners(graph->listeners.port_removed);
129 call_remove_listeners(graph->listeners.ports_connected);
130 call_remove_listeners(graph->listeners.ports_disconnected);
131
c0418dd9 132 if (graph->connections) {
262e5473 133 BT_LOGD_STR("Destroying connections.");
c0418dd9
JG
134 g_ptr_array_free(graph->connections, TRUE);
135 }
bd14d768 136 if (graph->components) {
262e5473 137 BT_LOGD_STR("Destroying components.");
bd14d768
PP
138 g_ptr_array_free(graph->components, TRUE);
139 }
f60c8b34
JG
140 if (graph->sinks_to_consume) {
141 g_queue_free(graph->sinks_to_consume);
c0418dd9 142 }
1bf957a0
PP
143
144 if (graph->listeners.port_added) {
145 g_array_free(graph->listeners.port_added, TRUE);
146 }
147
148 if (graph->listeners.port_removed) {
149 g_array_free(graph->listeners.port_removed, TRUE);
150 }
151
f345f8bb
PP
152 if (graph->listeners.ports_connected) {
153 g_array_free(graph->listeners.ports_connected, TRUE);
1bf957a0
PP
154 }
155
f345f8bb
PP
156 if (graph->listeners.ports_disconnected) {
157 g_array_free(graph->listeners.ports_disconnected, TRUE);
1bf957a0
PP
158 }
159
c0418dd9
JG
160 g_free(graph);
161}
162
f60c8b34 163struct bt_graph *bt_graph_create(void)
c0418dd9 164{
f60c8b34 165 struct bt_graph *graph;
1bf957a0 166 int ret;
c0418dd9 167
262e5473 168 BT_LOGD_STR("Creating graph object.");
f60c8b34 169 graph = g_new0(struct bt_graph, 1);
c0418dd9 170 if (!graph) {
262e5473 171 BT_LOGE_STR("Failed to allocate one graph.");
c0418dd9
JG
172 goto end;
173 }
174
f60c8b34 175 bt_object_init(graph, bt_graph_destroy);
c0418dd9 176
f60c8b34 177 graph->connections = g_ptr_array_new_with_free_func(bt_object_release);
c0418dd9 178 if (!graph->connections) {
262e5473 179 BT_LOGE_STR("Failed to allocate one GPtrArray.");
c0418dd9
JG
180 goto error;
181 }
f60c8b34
JG
182 graph->components = g_ptr_array_new_with_free_func(bt_object_release);
183 if (!graph->components) {
262e5473 184 BT_LOGE_STR("Failed to allocate one GPtrArray.");
f60c8b34
JG
185 goto error;
186 }
187 graph->sinks_to_consume = g_queue_new();
188 if (!graph->sinks_to_consume) {
262e5473 189 BT_LOGE_STR("Failed to allocate one GQueue.");
c0418dd9
JG
190 goto error;
191 }
1bf957a0 192
1d915789 193 graph->can_consume = BT_TRUE;
1bf957a0
PP
194 ret = init_listeners_array(&graph->listeners.port_added);
195 if (ret) {
262e5473 196 BT_LOGE_STR("Cannot create the \"port added\" listener array.");
1bf957a0
PP
197 goto error;
198 }
199
200 ret = init_listeners_array(&graph->listeners.port_removed);
201 if (ret) {
262e5473 202 BT_LOGE_STR("Cannot create the \"port removed\" listener array.");
1bf957a0
PP
203 goto error;
204 }
205
f345f8bb 206 ret = init_listeners_array(&graph->listeners.ports_connected);
1bf957a0 207 if (ret) {
262e5473 208 BT_LOGE_STR("Cannot create the \"port connected\" listener array.");
1bf957a0
PP
209 goto error;
210 }
211
f345f8bb 212 ret = init_listeners_array(&graph->listeners.ports_disconnected);
1bf957a0 213 if (ret) {
262e5473 214 BT_LOGE_STR("Cannot create the \"port disconneted\" listener array.");
1bf957a0
PP
215 goto error;
216 }
217
262e5473
PP
218 BT_LOGD("Created graph object: addr=%p", graph);
219
c0418dd9
JG
220end:
221 return graph;
222error:
223 BT_PUT(graph);
224 goto end;
225}
226
a256a42d
PP
227enum bt_graph_status bt_graph_connect_ports(struct bt_graph *graph,
228 struct bt_port *upstream_port, struct bt_port *downstream_port,
229 struct bt_connection **user_connection)
f60c8b34 230{
a256a42d 231 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
f60c8b34
JG
232 struct bt_connection *connection = NULL;
233 struct bt_graph *upstream_graph = NULL;
234 struct bt_graph *downstream_graph = NULL;
235 struct bt_component *upstream_component = NULL;
236 struct bt_component *downstream_component = NULL;
237 enum bt_component_status component_status;
ecbe9f49 238 bt_bool init_can_consume;
f60c8b34 239
262e5473
PP
240 if (!graph) {
241 BT_LOGW_STR("Invalid parameter: graph is NULL.");
a256a42d 242 status = BT_GRAPH_STATUS_INVALID;
262e5473
PP
243 goto end;
244 }
4aa7981f 245 init_can_consume = graph->can_consume;
262e5473
PP
246
247 if (!upstream_port) {
248 BT_LOGW_STR("Invalid parameter: upstream port is NULL.");
a256a42d 249 status = BT_GRAPH_STATUS_INVALID;
262e5473
PP
250 goto end;
251 }
252
253 if (!downstream_port) {
254 BT_LOGW_STR("Invalid parameter: downstream port is NULL.");
a256a42d 255 status = BT_GRAPH_STATUS_INVALID;
f60c8b34
JG
256 goto end;
257 }
258
262e5473
PP
259 BT_LOGD("Connecting component ports within graph: "
260 "graph-addr=%p, "
261 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
262 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
263 graph, upstream_port, bt_port_get_name(upstream_port),
264 downstream_port, bt_port_get_name(downstream_port));
265
202a3a13 266 if (graph->canceled) {
262e5473 267 BT_LOGW_STR("Invalid parameter: graph is canceled.");
a256a42d 268 status = BT_GRAPH_STATUS_CANCELED;
202a3a13
PP
269 goto end;
270 }
271
1d915789
PP
272 graph->can_consume = BT_FALSE;
273
72b913fb 274 /* Ensure appropriate types for upstream and downstream ports. */
f60c8b34 275 if (bt_port_get_type(upstream_port) != BT_PORT_TYPE_OUTPUT) {
262e5473 276 BT_LOGW_STR("Invalid parameter: upstream port is not an output port.");
a256a42d 277 status = BT_GRAPH_STATUS_INVALID;
f60c8b34
JG
278 goto end;
279 }
280 if (bt_port_get_type(downstream_port) != BT_PORT_TYPE_INPUT) {
262e5473 281 BT_LOGW_STR("Invalid parameter: downstream port is not an input port.");
a256a42d 282 status = BT_GRAPH_STATUS_INVALID;
f60c8b34
JG
283 goto end;
284 }
285
72b913fb 286 /* Ensure that both ports are currently unconnected. */
0d8b4d8e 287 if (bt_port_is_connected(upstream_port)) {
262e5473 288 BT_LOGW_STR("Invalid parameter: upstream port is already connected.");
a256a42d 289 status = BT_GRAPH_STATUS_INVALID;
72b913fb
PP
290 goto end;
291 }
292
0d8b4d8e 293 if (bt_port_is_connected(downstream_port)) {
262e5473 294 BT_LOGW_STR("Invalid parameter: downstream port is already connected.");
a256a42d 295 status = BT_GRAPH_STATUS_INVALID;
72b913fb
PP
296 goto end;
297 }
298
299 /*
300 * Ensure that both ports are still attached to their creating
301 * component.
302 */
f60c8b34 303 upstream_component = bt_port_get_component(upstream_port);
72b913fb 304 if (!upstream_component) {
262e5473 305 BT_LOGW_STR("Invalid parameter: upstream port is loose (does not belong to a component)");
a256a42d 306 status = BT_GRAPH_STATUS_INVALID;
72b913fb
PP
307 goto end;
308 }
309
310 downstream_component = bt_port_get_component(downstream_port);
311 if (!downstream_component) {
262e5473 312 BT_LOGW_STR("Invalid parameter: downstream port is loose (does not belong to a component)");
a256a42d 313 status = BT_GRAPH_STATUS_INVALID;
72b913fb
PP
314 goto end;
315 }
316
262e5473
PP
317 BT_LOGD("Connecting component ports: "
318 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
319 "downstream-comp-addr=%p, downstream-comp-name=\"%s\"",
320 upstream_component, bt_component_get_name(upstream_component),
321 downstream_component, bt_component_get_name(downstream_component));
322
0d8b4d8e
PP
323 /*
324 * At this point the ports are not connected yet. Both
325 * components need to accept an eventual connection to their
326 * port by the other port before we continue.
327 */
262e5473 328 BT_LOGD_STR("Asking upstream component to accept the connection.");
0d8b4d8e
PP
329 component_status = bt_component_accept_port_connection(
330 upstream_component, upstream_port, downstream_port);
331 if (component_status != BT_COMPONENT_STATUS_OK) {
262e5473
PP
332 if (component_status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) {
333 BT_LOGD_STR("Upstream component refused the connection.");
334 } else {
335 BT_LOGW("Cannot ask upstream component to accept the connection: "
336 "status=%s", bt_component_status_string(component_status));
337 }
338
a256a42d
PP
339 status = bt_graph_status_from_component_status(
340 component_status);
341 goto end;
0d8b4d8e 342 }
262e5473
PP
343
344 BT_LOGD_STR("Asking downstream component to accept the connection.");
0d8b4d8e
PP
345 component_status = bt_component_accept_port_connection(
346 downstream_component, downstream_port, upstream_port);
347 if (component_status != BT_COMPONENT_STATUS_OK) {
262e5473
PP
348 if (component_status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) {
349 BT_LOGD_STR("Downstream component refused the connection.");
350 } else {
351 BT_LOGW("Cannot ask downstream component to accept the connection: "
352 "status=%s", bt_component_status_string(component_status));
353 }
354
a256a42d
PP
355 status = bt_graph_status_from_component_status(
356 component_status);
357 goto end;
0d8b4d8e
PP
358 }
359
262e5473 360 BT_LOGD_STR("Creating connection.");
f60c8b34
JG
361 connection = bt_connection_create(graph, upstream_port,
362 downstream_port);
363 if (!connection) {
262e5473 364 BT_LOGW("Cannot create connection object.");
a256a42d
PP
365 status = BT_GRAPH_STATUS_NOMEM;
366 goto end;
f60c8b34
JG
367 }
368
262e5473
PP
369 BT_LOGD("Connection object created: conn-addr=%p", connection);
370
f60c8b34 371 /*
72b913fb
PP
372 * Ownership of upstream_component/downstream_component and of
373 * the connection object is transferred to the graph.
f60c8b34
JG
374 */
375 g_ptr_array_add(graph->connections, connection);
ffeb0eed 376
f60c8b34 377 /*
0d8b4d8e 378 * Notify both components that their port is connected.
f60c8b34 379 */
262e5473 380 BT_LOGD_STR("Notifying upstream component that its port is connected.");
0d8b4d8e
PP
381 bt_component_port_connected(upstream_component, upstream_port,
382 downstream_port);
262e5473 383 BT_LOGD_STR("Notifying downstream component that its port is connected.");
0d8b4d8e
PP
384 bt_component_port_connected(downstream_component, downstream_port,
385 upstream_port);
1bf957a0
PP
386
387 /*
0d8b4d8e 388 * Notify the graph's creator that both ports are connected.
1bf957a0 389 */
262e5473 390 BT_LOGD_STR("Notifying graph's user that new component ports are connected.");
f345f8bb 391 bt_graph_notify_ports_connected(graph, upstream_port, downstream_port);
262e5473
PP
392 BT_LOGD("Connected component ports within graph: "
393 "graph-addr=%p, "
394 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
395 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
396 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
397 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
398 graph,
399 upstream_component, bt_component_get_name(upstream_component),
400 downstream_component, bt_component_get_name(downstream_component),
401 upstream_port, bt_port_get_name(upstream_port),
402 downstream_port, bt_port_get_name(downstream_port));
1bf957a0 403
a256a42d 404 if (user_connection) {
1a6a376a
PP
405 /* Move reference to user */
406 *user_connection = connection;
407 connection = NULL;
a256a42d
PP
408 }
409
f60c8b34
JG
410end:
411 bt_put(upstream_graph);
412 bt_put(downstream_graph);
3eeacbb9
JG
413 bt_put(upstream_component);
414 bt_put(downstream_component);
a256a42d 415 bt_put(connection);
4aa7981f
JG
416 if (graph) {
417 graph->can_consume = init_can_consume;
418 }
a256a42d 419 return status;
f60c8b34
JG
420}
421
851b70bd 422static
8ed535b5 423enum bt_graph_status consume_graph_sink(struct bt_component *sink)
c0418dd9 424{
72b913fb
PP
425 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
426 enum bt_component_status comp_status;
202a3a13 427
f6ccaed9 428 BT_ASSERT(sink);
72b913fb 429 comp_status = bt_component_sink_consume(sink);
8ed535b5
PP
430 BT_LOGV("Consumed from sink: addr=%p, name=\"%s\", status=%s",
431 sink, bt_component_get_name(sink),
262e5473 432 bt_component_status_string(comp_status));
8ed535b5 433
72b913fb
PP
434 switch (comp_status) {
435 case BT_COMPONENT_STATUS_OK:
436 break;
437 case BT_COMPONENT_STATUS_END:
438 status = BT_GRAPH_STATUS_END;
439 break;
440 case BT_COMPONENT_STATUS_AGAIN:
441 status = BT_GRAPH_STATUS_AGAIN;
442 break;
443 case BT_COMPONENT_STATUS_INVALID:
444 status = BT_GRAPH_STATUS_INVALID;
445 break;
446 default:
447 status = BT_GRAPH_STATUS_ERROR;
448 break;
449 }
450
8ed535b5
PP
451 return status;
452}
453
454/*
455 * `node` is removed from the queue of sinks to consume when passed to
456 * this function. This function adds it back to the queue if there's
457 * still something to consume afterwards.
458 */
459static
460enum bt_graph_status consume_sink_node(struct bt_graph *graph,
461 GList *node)
462{
463 enum bt_graph_status status;
464 struct bt_component *sink;
465
466 sink = node->data;
467 status = consume_graph_sink(sink);
72b913fb 468 if (status != BT_GRAPH_STATUS_END) {
8ed535b5 469 g_queue_push_tail_link(graph->sinks_to_consume, node);
f60c8b34
JG
470 goto end;
471 }
472
473 /* End reached, the node is not added back to the queue and free'd. */
8ed535b5 474 g_queue_delete_link(graph->sinks_to_consume, node);
f60c8b34
JG
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 }
8ed535b5
PP
481
482end:
483 BT_LOGV("Consumed sink node: status=%s", bt_graph_status_string(status));
484 return status;
485}
486
487BT_HIDDEN
488enum bt_graph_status bt_graph_consume_sink_no_check(struct bt_graph *graph,
489 struct bt_component *sink)
490{
491 enum bt_graph_status status;
492 GList *sink_node;
493 int index;
494
495 BT_LOGV("Making specific sink consume: addr=%p, "
496 "comp-addr=%p, comp-name=\"%s\"",
497 graph, sink, bt_component_get_name(sink));
498
f6ccaed9 499 BT_ASSERT(bt_component_borrow_graph(sink) == graph);
8ed535b5
PP
500
501 if (g_queue_is_empty(graph->sinks_to_consume)) {
502 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
503 status = BT_GRAPH_STATUS_END;
504 goto end;
505 }
506
507 index = g_queue_index(graph->sinks_to_consume, sink);
508 if (index < 0) {
509 BT_LOGV_STR("Sink is not marked as consumable: sink is ended.");
510 status = BT_GRAPH_STATUS_END;
511 goto end;
512 }
513
514 sink_node = g_queue_pop_nth_link(graph->sinks_to_consume, index);
f6ccaed9 515 BT_ASSERT(sink_node);
8ed535b5
PP
516 status = consume_sink_node(graph, sink_node);
517
518end:
519 return status;
520}
521
522BT_HIDDEN
523enum bt_graph_status bt_graph_consume_no_check(struct bt_graph *graph)
524{
525 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
526 struct bt_component *sink;
527 GList *current_node;
528
529 BT_LOGV("Making next sink consume: addr=%p", graph);
f6ccaed9
PP
530 BT_ASSERT_PRE(graph->has_sink,
531 "Graph has no sink component: %!+g", graph);
8ed535b5
PP
532
533 if (g_queue_is_empty(graph->sinks_to_consume)) {
534 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
535 status = BT_GRAPH_STATUS_END;
536 goto end;
537 }
538
539 current_node = g_queue_pop_head_link(graph->sinks_to_consume);
540 sink = current_node->data;
541 BT_LOGV("Chose next sink to consume: comp-addr=%p, comp-name=\"%s\"",
542 sink, bt_component_get_name(sink));
543 status = consume_sink_node(graph, current_node);
544
f60c8b34
JG
545end:
546 return status;
c0418dd9
JG
547}
548
851b70bd
PP
549enum bt_graph_status bt_graph_consume(struct bt_graph *graph)
550{
f6ccaed9 551 enum bt_graph_status status;
1d915789 552
f6ccaed9
PP
553 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
554 BT_ASSERT_PRE(!graph->canceled, "Graph is canceled: %!+g", graph);
555 BT_ASSERT_PRE(graph->can_consume,
556 "Cannot consume graph in its current state: %!+g", graph);
1d915789 557 graph->can_consume = BT_FALSE;
851b70bd 558 status = bt_graph_consume_no_check(graph);
1d915789 559 graph->can_consume = BT_TRUE;
f6ccaed9 560 return BT_GRAPH_STATUS_OK;
851b70bd
PP
561}
562
72b913fb 563enum bt_graph_status bt_graph_run(struct bt_graph *graph)
f60c8b34 564{
72b913fb 565 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
f60c8b34
JG
566
567 if (!graph) {
262e5473 568 BT_LOGW_STR("Invalid parameter: graph is NULL.");
72b913fb 569 status = BT_GRAPH_STATUS_INVALID;
202a3a13 570 goto end;
f60c8b34
JG
571 }
572
851b70bd
PP
573 if (graph->canceled) {
574 BT_LOGW("Invalid parameter: graph is canceled: "
575 "graph-addr=%p", graph);
576 status = BT_GRAPH_STATUS_CANCELED;
577 goto end;
578 }
579
1d915789
PP
580 if (!graph->can_consume) {
581 BT_LOGW_STR("Cannot run graph in its current state.");
582 status = BT_GRAPH_STATUS_CANNOT_CONSUME;
583 goto end;
584 }
585
586 graph->can_consume = BT_FALSE;
262e5473
PP
587 BT_LOGV("Running graph: addr=%p", graph);
588
f60c8b34 589 do {
851b70bd
PP
590 /*
591 * Check if the graph is canceled at each iteration. If
592 * the graph was canceled by another thread or by a
593 * signal, this is not a warning nor an error, it was
594 * intentional: log with a DEBUG level only.
595 */
596 if (graph->canceled) {
597 BT_LOGD("Stopping the graph: graph is canceled: "
598 "graph-addr=%p", graph);
599 status = BT_GRAPH_STATUS_CANCELED;
600 goto end;
601 }
602
4fef099a 603 status = bt_graph_consume_no_check(graph);
72b913fb 604 if (status == BT_GRAPH_STATUS_AGAIN) {
f60c8b34 605 /*
202a3a13
PP
606 * If AGAIN is received and there are multiple
607 * sinks, go ahead and consume from the next
608 * sink.
f60c8b34 609 *
202a3a13
PP
610 * However, in the case where a single sink is
611 * left, the caller can decide to busy-wait and
612 * call bt_graph_run() continuously until the
613 * source is ready or it can decide to sleep for
614 * an arbitrary amount of time.
f60c8b34
JG
615 */
616 if (graph->sinks_to_consume->length > 1) {
72b913fb 617 status = BT_GRAPH_STATUS_OK;
f60c8b34 618 }
2de524b3
PP
619 } else if (status == BT_GRAPH_STATUS_NO_SINK) {
620 goto end;
f60c8b34 621 }
72b913fb 622 } while (status == BT_GRAPH_STATUS_OK);
f60c8b34
JG
623
624 if (g_queue_is_empty(graph->sinks_to_consume)) {
72b913fb 625 status = BT_GRAPH_STATUS_END;
f60c8b34 626 }
262e5473 627
202a3a13 628end:
262e5473 629 BT_LOGV("Graph ran: status=%s", bt_graph_status_string(status));
4aa7981f
JG
630 if (graph) {
631 graph->can_consume = BT_TRUE;
632 }
72b913fb 633 return status;
f60c8b34 634}
1bf957a0
PP
635
636static
8cc092c9 637int add_listener(GArray *listeners, void *func, void *removed, void *data)
1bf957a0
PP
638{
639 struct bt_graph_listener listener = {
640 .func = func,
8cc092c9 641 .removed = removed,
1bf957a0
PP
642 .data = data,
643 };
644
645 g_array_append_val(listeners, listener);
0d107cdd 646 return listeners->len - 1;
1bf957a0
PP
647}
648
0d107cdd 649int bt_graph_add_port_added_listener(
1bf957a0 650 struct bt_graph *graph,
8cc092c9
PP
651 bt_graph_port_added_listener listener,
652 bt_graph_listener_removed listener_removed, void *data)
1bf957a0 653{
0d107cdd 654 int ret;
1bf957a0 655
262e5473
PP
656 if (!graph) {
657 BT_LOGW_STR("Invalid parameter: graph is NULL.");
0d107cdd 658 ret = -1;
262e5473
PP
659 goto end;
660 }
661
8cc092c9
PP
662 if (graph->in_remove_listener) {
663 BT_LOGW("Cannot call this function during the execution of a remove listener: "
664 "addr=%p", graph);
665 ret = -1;
666 goto end;
667 }
668
262e5473
PP
669 if (!listener) {
670 BT_LOGW_STR("Invalid parameter: listener is NULL.");
0d107cdd 671 ret = -1;
1bf957a0
PP
672 goto end;
673 }
674
8cc092c9
PP
675 ret = add_listener(graph->listeners.port_added, listener,
676 listener_removed, data);
262e5473 677 BT_LOGV("Added \"port added\" listener to graph: "
0d107cdd
PP
678 "graph-addr=%p, listener-addr=%p, pos=%d",
679 graph, listener, ret);
1bf957a0
PP
680
681end:
0d107cdd 682 return ret;
1bf957a0
PP
683}
684
0d107cdd 685int bt_graph_add_port_removed_listener(
1bf957a0 686 struct bt_graph *graph,
8cc092c9
PP
687 bt_graph_port_removed_listener listener,
688 bt_graph_listener_removed listener_removed, void *data)
1bf957a0 689{
0d107cdd 690 int ret;
1bf957a0 691
262e5473
PP
692 if (!graph) {
693 BT_LOGW_STR("Invalid parameter: graph is NULL.");
0d107cdd 694 ret = -1;
262e5473
PP
695 goto end;
696 }
697
8cc092c9
PP
698 if (graph->in_remove_listener) {
699 BT_LOGW("Cannot call this function during the execution of a remove listener: "
700 "addr=%p", graph);
701 ret = -1;
702 goto end;
703 }
704
262e5473
PP
705 if (!listener) {
706 BT_LOGW_STR("Invalid parameter: listener is NULL.");
0d107cdd 707 ret = -1;
1bf957a0
PP
708 goto end;
709 }
710
8cc092c9
PP
711 ret = add_listener(graph->listeners.port_removed, listener,
712 listener_removed, data);
262e5473 713 BT_LOGV("Added \"port removed\" listener to graph: "
0d107cdd
PP
714 "graph-addr=%p, listener-addr=%p, pos=%d",
715 graph, listener, ret);
1bf957a0
PP
716
717end:
0d107cdd 718 return ret;
1bf957a0
PP
719}
720
0d107cdd 721int bt_graph_add_ports_connected_listener(
1bf957a0 722 struct bt_graph *graph,
8cc092c9
PP
723 bt_graph_ports_connected_listener listener,
724 bt_graph_listener_removed listener_removed, void *data)
1bf957a0 725{
0d107cdd 726 int ret;
1bf957a0 727
262e5473
PP
728 if (!graph) {
729 BT_LOGW_STR("Invalid parameter: graph is NULL.");
0d107cdd 730 ret = -1;
262e5473
PP
731 goto end;
732 }
733
8cc092c9
PP
734 if (graph->in_remove_listener) {
735 BT_LOGW("Cannot call this function during the execution of a remove listener: "
736 "addr=%p", graph);
737 ret = -1;
738 goto end;
739 }
740
262e5473
PP
741 if (!listener) {
742 BT_LOGW_STR("Invalid parameter: listener is NULL.");
0d107cdd 743 ret = -1;
1bf957a0
PP
744 goto end;
745 }
746
8cc092c9
PP
747 ret = add_listener(graph->listeners.ports_connected, listener,
748 listener_removed, data);
262e5473 749 BT_LOGV("Added \"port connected\" listener to graph: "
0d107cdd
PP
750 "graph-addr=%p, listener-addr=%p, pos=%d",
751 graph, listener, ret);
1bf957a0
PP
752
753end:
0d107cdd 754 return ret;
1bf957a0
PP
755}
756
0d107cdd 757int bt_graph_add_ports_disconnected_listener(
1bf957a0 758 struct bt_graph *graph,
8cc092c9
PP
759 bt_graph_ports_disconnected_listener listener,
760 bt_graph_listener_removed listener_removed, void *data)
1bf957a0 761{
0d107cdd 762 int ret;
1bf957a0 763
262e5473
PP
764 if (!graph) {
765 BT_LOGW_STR("Invalid parameter: graph is NULL.");
0d107cdd 766 ret = -1;
262e5473
PP
767 goto end;
768 }
769
8cc092c9
PP
770 if (graph->in_remove_listener) {
771 BT_LOGW("Cannot call this function during the execution of a remove listener: "
772 "addr=%p", graph);
773 ret = -1;
774 goto end;
775 }
776
262e5473
PP
777 if (!listener) {
778 BT_LOGW_STR("Invalid parameter: listener is NULL.");
0d107cdd 779 ret = -1;
1bf957a0
PP
780 goto end;
781 }
782
8cc092c9
PP
783 ret = add_listener(graph->listeners.ports_disconnected, listener,
784 listener_removed, data);
262e5473 785 BT_LOGV("Added \"port disconnected\" listener to graph: "
0d107cdd
PP
786 "graph-addr=%p, listener-addr=%p, pos=%d",
787 graph, listener, ret);
1bf957a0
PP
788
789end:
0d107cdd 790 return ret;
1bf957a0
PP
791}
792
793BT_HIDDEN
794void bt_graph_notify_port_added(struct bt_graph *graph, struct bt_port *port)
795{
796 size_t i;
797
262e5473
PP
798 BT_LOGV("Notifying graph listeners that a port was added: "
799 "graph-addr=%p, port-addr=%p, port-name=\"%s\"",
800 graph, port, bt_port_get_name(port));
801
1bf957a0
PP
802 for (i = 0; i < graph->listeners.port_added->len; i++) {
803 struct bt_graph_listener listener =
804 g_array_index(graph->listeners.port_added,
805 struct bt_graph_listener, i);
806 bt_graph_port_added_listener func = listener.func;
807
f6ccaed9 808 BT_ASSERT(func);
1bf957a0
PP
809 func(port, listener.data);
810 }
811}
812
813BT_HIDDEN
814void bt_graph_notify_port_removed(struct bt_graph *graph,
815 struct bt_component *comp, struct bt_port *port)
816{
817 size_t i;
818
262e5473
PP
819 BT_LOGV("Notifying graph listeners that a port was removed: "
820 "graph-addr=%p, port-addr=%p, port-name=\"%s\"",
821 graph, port, bt_port_get_name(port));
822
1bf957a0
PP
823 for (i = 0; i < graph->listeners.port_removed->len; i++) {
824 struct bt_graph_listener listener =
825 g_array_index(graph->listeners.port_removed,
826 struct bt_graph_listener, i);
827 bt_graph_port_removed_listener func = listener.func;
828
f6ccaed9 829 BT_ASSERT(func);
1bf957a0
PP
830 func(comp, port, listener.data);
831 }
832}
833
834BT_HIDDEN
f345f8bb
PP
835void bt_graph_notify_ports_connected(struct bt_graph *graph,
836 struct bt_port *upstream_port, struct bt_port *downstream_port)
1bf957a0
PP
837{
838 size_t i;
839
262e5473
PP
840 BT_LOGV("Notifying graph listeners that two ports were connected: "
841 "graph-addr=%p, "
842 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
843 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
844 graph, upstream_port, bt_port_get_name(upstream_port),
845 downstream_port, bt_port_get_name(downstream_port));
846
f345f8bb 847 for (i = 0; i < graph->listeners.ports_connected->len; i++) {
1bf957a0 848 struct bt_graph_listener listener =
f345f8bb 849 g_array_index(graph->listeners.ports_connected,
1bf957a0 850 struct bt_graph_listener, i);
f345f8bb 851 bt_graph_ports_connected_listener func = listener.func;
1bf957a0 852
f6ccaed9 853 BT_ASSERT(func);
f345f8bb 854 func(upstream_port, downstream_port, listener.data);
1bf957a0
PP
855 }
856}
857
858BT_HIDDEN
f345f8bb
PP
859void bt_graph_notify_ports_disconnected(struct bt_graph *graph,
860 struct bt_component *upstream_comp,
861 struct bt_component *downstream_comp,
862 struct bt_port *upstream_port, struct bt_port *downstream_port)
1bf957a0
PP
863{
864 size_t i;
865
262e5473
PP
866 BT_LOGV("Notifying graph listeners that two ports were disconnected: "
867 "graph-addr=%p, "
868 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
869 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
870 graph, upstream_port, bt_port_get_name(upstream_port),
871 downstream_port, bt_port_get_name(downstream_port));
872
f345f8bb 873 for (i = 0; i < graph->listeners.ports_disconnected->len; i++) {
1bf957a0 874 struct bt_graph_listener listener =
f345f8bb 875 g_array_index(graph->listeners.ports_disconnected,
1bf957a0 876 struct bt_graph_listener, i);
f345f8bb 877 bt_graph_ports_disconnected_listener func = listener.func;
1bf957a0 878
f6ccaed9 879 BT_ASSERT(func);
f345f8bb
PP
880 func(upstream_comp, downstream_comp, upstream_port,
881 downstream_port, listener.data);
1bf957a0
PP
882 }
883}
202a3a13 884
c7eee084 885enum bt_graph_status bt_graph_cancel(struct bt_graph *graph)
202a3a13
PP
886{
887 enum bt_graph_status ret = BT_GRAPH_STATUS_OK;
888
889 if (!graph) {
262e5473 890 BT_LOGW_STR("Invalid parameter: graph is NULL.");
202a3a13
PP
891 ret = BT_GRAPH_STATUS_INVALID;
892 goto end;
893 }
894
895 graph->canceled = BT_TRUE;
262e5473 896 BT_LOGV("Canceled graph: addr=%p", graph);
202a3a13
PP
897
898end:
899 return ret;
900}
901
c7eee084 902bt_bool bt_graph_is_canceled(struct bt_graph *graph)
202a3a13 903{
c7eee084
PP
904 bt_bool canceled = BT_FALSE;
905
906 if (!graph) {
907 BT_LOGW_STR("Invalid parameter: graph is NULL.");
908 goto end;
909 }
910
911 canceled = graph->canceled;
912
913end:
914 return canceled;
202a3a13 915}
f167d3c0
PP
916
917BT_HIDDEN
918void bt_graph_remove_connection(struct bt_graph *graph,
919 struct bt_connection *connection)
920{
f6ccaed9
PP
921 BT_ASSERT(graph);
922 BT_ASSERT(connection);
262e5473
PP
923 BT_LOGV("Removing graph's connection: graph-addr=%p, conn-addr=%p",
924 graph, connection);
f167d3c0
PP
925 g_ptr_array_remove(graph->connections, connection);
926}
36712f1d
PP
927
928enum bt_graph_status bt_graph_add_component_with_init_method_data(
929 struct bt_graph *graph,
930 struct bt_component_class *component_class,
931 const char *name, struct bt_value *params,
932 void *init_method_data,
933 struct bt_component **user_component)
934{
935 enum bt_graph_status graph_status = BT_GRAPH_STATUS_OK;
936 enum bt_component_status comp_status;
937 struct bt_component *component = NULL;
938 enum bt_component_class_type type;
939 size_t i;
4aa7981f 940 bt_bool init_can_consume;
36712f1d
PP
941
942 bt_get(params);
943
944 if (!graph) {
945 BT_LOGW_STR("Invalid parameter: graph is NULL.");
946 graph_status = BT_GRAPH_STATUS_INVALID;
947 goto end;
948 }
4aa7981f 949 init_can_consume = graph->can_consume;
36712f1d
PP
950
951 if (!component_class) {
952 BT_LOGW_STR("Invalid parameter: component class is NULL.");
953 graph_status = BT_GRAPH_STATUS_INVALID;
954 goto end;
955 }
956
1d915789 957 graph->can_consume = BT_FALSE;
36712f1d
PP
958 type = bt_component_class_get_type(component_class);
959 BT_LOGD("Adding component to graph: "
960 "graph-addr=%p, comp-cls-addr=%p, "
961 "comp-cls-type=%s, name=\"%s\", params-addr=%p, "
962 "init-method-data-addr=%p",
963 graph, component_class, bt_component_class_type_string(type),
964 name, params, init_method_data);
965
966 if (!name) {
967 BT_LOGW_STR("Invalid parameter: name is NULL.");
968 graph_status = BT_GRAPH_STATUS_INVALID;
969 goto end;
970 }
971
972 if (graph->canceled) {
973 BT_LOGW_STR("Invalid parameter: graph is canceled.");
974 graph_status = BT_GRAPH_STATUS_CANCELED;
975 goto end;
976 }
977
978 if (type != BT_COMPONENT_CLASS_TYPE_SOURCE &&
979 type != BT_COMPONENT_CLASS_TYPE_FILTER &&
980 type != BT_COMPONENT_CLASS_TYPE_SINK) {
981 BT_LOGW("Invalid parameter: unknown component class type: "
982 "type=%d", type);
983 graph_status = BT_GRAPH_STATUS_INVALID;
984 goto end;
985 }
986
987 for (i = 0; i < graph->components->len; i++) {
988 void *other_comp = graph->components->pdata[i];
989
990 if (strcmp(name, bt_component_get_name(other_comp)) == 0) {
991 BT_LOGW("Invalid parameter: another component with the same name already exists in the graph: "
992 "other-comp-addr=%p, name=\"%s\"",
993 other_comp, name);
994 graph_status = BT_GRAPH_STATUS_INVALID;
995 goto end;
996 }
997 }
998
999 /*
1000 * Parameters must be a map value, but we create a convenient
1001 * empty one if it's NULL.
1002 */
1003 if (params) {
1004 if (!bt_value_is_map(params)) {
1005 BT_LOGW("Invalid parameter: initialization parameters must be a map value: "
1006 "type=%s",
1007 bt_value_type_string(bt_value_get_type(params)));
1008 graph_status = BT_GRAPH_STATUS_INVALID;
1009 goto end;
1010 }
1011 } else {
1012 params = bt_value_map_create();
1013 if (!params) {
1014 BT_LOGE_STR("Cannot create map value object.");
1015 graph_status = BT_GRAPH_STATUS_NOMEM;
1016 goto end;
1017 }
1018 }
1019
1020 comp_status = bt_component_create(component_class, name, &component);
1021 if (comp_status != BT_COMPONENT_STATUS_OK) {
1022 BT_LOGE("Cannot create empty component object: status=%s",
1023 bt_component_status_string(comp_status));
1024 graph_status = bt_graph_status_from_component_status(
1025 comp_status);
1026 goto end;
1027 }
1028
1029 /*
1030 * The user's initialization method needs to see that this
1031 * component is part of the graph. If the user method fails, we
1032 * immediately remove the component from the graph's components.
1033 */
1034 g_ptr_array_add(graph->components, component);
1035 bt_component_set_graph(component, graph);
1036
1037 if (component_class->methods.init) {
1038 BT_LOGD_STR("Calling user's initialization method.");
1039 comp_status = component_class->methods.init(
1040 bt_private_component_from_component(component), params,
1041 init_method_data);
1042 BT_LOGD("User method returned: status=%s",
1043 bt_component_status_string(comp_status));
1044 if (comp_status != BT_COMPONENT_STATUS_OK) {
1045 BT_LOGW_STR("Initialization method failed.");
1046 graph_status = bt_graph_status_from_component_status(
1047 comp_status);
1048 bt_component_set_graph(component, NULL);
1049 g_ptr_array_remove_fast(graph->components, component);
1050 goto end;
1051 }
1052 }
1053
1054 /*
1055 * Mark the component as initialized so that its finalization
1056 * method is called when it is destroyed.
1057 */
1058 component->initialized = true;
1059
1060 /*
1061 * If it's a sink component, it needs to be part of the graph's
1062 * sink queue to be consumed by bt_graph_consume().
1063 */
1064 if (bt_component_is_sink(component)) {
2de524b3 1065 graph->has_sink = BT_TRUE;
36712f1d
PP
1066 g_queue_push_tail(graph->sinks_to_consume, component);
1067 }
1068
1069 /*
1070 * Freeze the component class now that it's instantiated at
1071 * least once.
1072 */
1073 BT_LOGD_STR("Freezing component class.");
1074 bt_component_class_freeze(component->class);
1075 BT_LOGD("Added component to graph: "
1076 "graph-addr=%p, comp-cls-addr=%p, "
1077 "comp-cls-type=%s, name=\"%s\", params-addr=%p, "
1078 "init-method-data-addr=%p, comp-addr=%p",
1079 graph, component_class, bt_component_class_type_string(type),
1080 name, params, init_method_data, component);
1081
1082 if (user_component) {
1083 /* Move reference to user */
1084 *user_component = component;
1085 component = NULL;
1086 }
1087
1088end:
1089 bt_put(component);
1090 bt_put(params);
4aa7981f
JG
1091 if (graph) {
1092 graph->can_consume = init_can_consume;
1093 }
36712f1d
PP
1094 return graph_status;
1095}
1096
1097enum bt_graph_status bt_graph_add_component(
1098 struct bt_graph *graph,
1099 struct bt_component_class *component_class,
1100 const char *name, struct bt_value *params,
1101 struct bt_component **component)
1102{
1103 return bt_graph_add_component_with_init_method_data(graph,
1104 component_class, name, params, NULL, component);
1105}
8ed535b5
PP
1106
1107BT_HIDDEN
1108int bt_graph_remove_unconnected_component(struct bt_graph *graph,
1109 struct bt_component *component)
1110{
4aa7981f 1111 bt_bool init_can_consume;
8ed535b5
PP
1112 int64_t count;
1113 uint64_t i;
1114 int ret = 0;
1115
f6ccaed9
PP
1116 BT_ASSERT(graph);
1117 BT_ASSERT(component);
1118 BT_ASSERT(component->base.ref_count.count == 0);
1119 BT_ASSERT(bt_component_borrow_graph(component) == graph);
8ed535b5 1120
4aa7981f 1121 init_can_consume = graph->can_consume;
8ed535b5
PP
1122 count = bt_component_get_input_port_count(component);
1123
1124 for (i = 0; i < count; i++) {
1125 struct bt_port *port =
1126 bt_component_get_input_port_by_index(component, i);
1127
f6ccaed9 1128 BT_ASSERT(port);
8ed535b5
PP
1129 bt_put(port);
1130
1131 if (bt_port_is_connected(port)) {
1132 BT_LOGW("Cannot remove component from graph: "
1133 "an input port is connected: "
1134 "graph-addr=%p, comp-addr=%p, "
1135 "comp-name=\"%s\", connected-port-addr=%p, "
1136 "connected-port-name=\"%s\"",
1137 graph, component,
1138 bt_component_get_name(component),
1139 port, bt_port_get_name(port));
1140 goto error;
1141 }
1142 }
1143
1144 count = bt_component_get_output_port_count(component);
1145
1146 for (i = 0; i < count; i++) {
1147 struct bt_port *port =
1148 bt_component_get_output_port_by_index(component, i);
1149
f6ccaed9 1150 BT_ASSERT(port);
8ed535b5
PP
1151 bt_put(port);
1152
1153 if (bt_port_is_connected(port)) {
1154 BT_LOGW("Cannot remove component from graph: "
1155 "an output port is connected: "
1156 "graph-addr=%p, comp-addr=%p, "
1157 "comp-name=\"%s\", connected-port-addr=%p, "
1158 "connected-port-name=\"%s\"",
1159 graph, component,
1160 bt_component_get_name(component),
1161 port, bt_port_get_name(port));
1162 goto error;
1163 }
1164 }
1165
1166 graph->can_consume = BT_FALSE;
1167
1168 /* Possibly remove from sinks to consume */
1169 (void) g_queue_remove(graph->sinks_to_consume, component);
1170
1171 if (graph->sinks_to_consume->length == 0) {
1172 graph->has_sink = BT_FALSE;
1173 }
1174
1175 /*
1176 * This calls bt_object_release() on the component, and since
1177 * its reference count is 0, its destructor is called. Its
1178 * destructor calls the user's finalization method (if set).
1179 */
1180 g_ptr_array_remove(graph->components, component);
1181 goto end;
1182
1183error:
1184 ret = -1;
1185
1186end:
1187 graph->can_consume = init_can_consume;
1188 return ret;
1189}
This page took 0.092502 seconds and 4 git commands to generate.