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