Cancel the graph on destruction
[babeltrace.git] / lib / graph / graph.c
1 /*
2 * graph.c
3 *
4 * Babeltrace Plugin Component Graph
5 *
6 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
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
28 #define BT_LOG_TAG "GRAPH"
29 #include <babeltrace/lib-logging-internal.h>
30
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>
38 #include <babeltrace/compiler-internal.h>
39 #include <babeltrace/types.h>
40 #include <unistd.h>
41 #include <glib.h>
42
43 struct bt_graph_listener {
44 void *func;
45 void *data;
46 };
47
48 static
49 void bt_graph_destroy(struct bt_object *obj)
50 {
51 struct bt_graph *graph = container_of(obj,
52 struct bt_graph, base);
53
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 */
80 BT_LOGD("Destroying graph: addr=%p", graph);
81 obj->ref_count.count++;
82
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
89 if (graph->connections) {
90 BT_LOGD_STR("Destroying connections.");
91 g_ptr_array_free(graph->connections, TRUE);
92 }
93 if (graph->components) {
94 BT_LOGD_STR("Destroying components.");
95 g_ptr_array_free(graph->components, TRUE);
96 }
97 if (graph->sinks_to_consume) {
98 g_queue_free(graph->sinks_to_consume);
99 }
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
109 if (graph->listeners.ports_connected) {
110 g_array_free(graph->listeners.ports_connected, TRUE);
111 }
112
113 if (graph->listeners.ports_disconnected) {
114 g_array_free(graph->listeners.ports_disconnected, TRUE);
115 }
116
117 g_free(graph);
118 }
119
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) {
128 BT_LOGE_STR("Failed to allocate one GArray.");
129 ret = -1;
130 goto end;
131 }
132
133 end:
134 return ret;
135 }
136
137 struct bt_graph *bt_graph_create(void)
138 {
139 struct bt_graph *graph;
140 int ret;
141
142 BT_LOGD_STR("Creating graph object.");
143 graph = g_new0(struct bt_graph, 1);
144 if (!graph) {
145 BT_LOGE_STR("Failed to allocate one graph.");
146 goto end;
147 }
148
149 bt_object_init(graph, bt_graph_destroy);
150
151 graph->connections = g_ptr_array_new_with_free_func(bt_object_release);
152 if (!graph->connections) {
153 BT_LOGE_STR("Failed to allocate one GPtrArray.");
154 goto error;
155 }
156 graph->components = g_ptr_array_new_with_free_func(bt_object_release);
157 if (!graph->components) {
158 BT_LOGE_STR("Failed to allocate one GPtrArray.");
159 goto error;
160 }
161 graph->sinks_to_consume = g_queue_new();
162 if (!graph->sinks_to_consume) {
163 BT_LOGE_STR("Failed to allocate one GQueue.");
164 goto error;
165 }
166
167 ret = init_listeners_array(&graph->listeners.port_added);
168 if (ret) {
169 BT_LOGE_STR("Cannot create the \"port added\" listener array.");
170 goto error;
171 }
172
173 ret = init_listeners_array(&graph->listeners.port_removed);
174 if (ret) {
175 BT_LOGE_STR("Cannot create the \"port removed\" listener array.");
176 goto error;
177 }
178
179 ret = init_listeners_array(&graph->listeners.ports_connected);
180 if (ret) {
181 BT_LOGE_STR("Cannot create the \"port connected\" listener array.");
182 goto error;
183 }
184
185 ret = init_listeners_array(&graph->listeners.ports_disconnected);
186 if (ret) {
187 BT_LOGE_STR("Cannot create the \"port disconneted\" listener array.");
188 goto error;
189 }
190
191 BT_LOGD("Created graph object: addr=%p", graph);
192
193 end:
194 return graph;
195 error:
196 BT_PUT(graph);
197 goto end;
198 }
199
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)
203 {
204 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
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;
211 bt_bool upstream_was_already_in_graph;
212 bt_bool downstream_was_already_in_graph;
213
214 if (!graph) {
215 BT_LOGW_STR("Invalid parameter: graph is NULL.");
216 status = BT_GRAPH_STATUS_INVALID;
217 goto end;
218 }
219
220 if (!upstream_port) {
221 BT_LOGW_STR("Invalid parameter: upstream port is NULL.");
222 status = BT_GRAPH_STATUS_INVALID;
223 goto end;
224 }
225
226 if (!downstream_port) {
227 BT_LOGW_STR("Invalid parameter: downstream port is NULL.");
228 status = BT_GRAPH_STATUS_INVALID;
229 goto end;
230 }
231
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
239 if (graph->canceled) {
240 BT_LOGW_STR("Invalid parameter: graph is canceled.");
241 status = BT_GRAPH_STATUS_CANCELED;
242 goto end;
243 }
244
245 /* Ensure appropriate types for upstream and downstream ports. */
246 if (bt_port_get_type(upstream_port) != BT_PORT_TYPE_OUTPUT) {
247 BT_LOGW_STR("Invalid parameter: upstream port is not an output port.");
248 status = BT_GRAPH_STATUS_INVALID;
249 goto end;
250 }
251 if (bt_port_get_type(downstream_port) != BT_PORT_TYPE_INPUT) {
252 BT_LOGW_STR("Invalid parameter: downstream port is not an input port.");
253 status = BT_GRAPH_STATUS_INVALID;
254 goto end;
255 }
256
257 /* Ensure that both ports are currently unconnected. */
258 if (bt_port_is_connected(upstream_port)) {
259 BT_LOGW_STR("Invalid parameter: upstream port is already connected.");
260 status = BT_GRAPH_STATUS_INVALID;
261 goto end;
262 }
263
264 if (bt_port_is_connected(downstream_port)) {
265 BT_LOGW_STR("Invalid parameter: downstream port is already connected.");
266 status = BT_GRAPH_STATUS_INVALID;
267 goto end;
268 }
269
270 /*
271 * Ensure that both ports are still attached to their creating
272 * component.
273 */
274 upstream_component = bt_port_get_component(upstream_port);
275 if (!upstream_component) {
276 BT_LOGW_STR("Invalid parameter: upstream port is loose (does not belong to a component)");
277 status = BT_GRAPH_STATUS_INVALID;
278 goto end;
279 }
280
281 downstream_component = bt_port_get_component(downstream_port);
282 if (!downstream_component) {
283 BT_LOGW_STR("Invalid parameter: downstream port is loose (does not belong to a component)");
284 status = BT_GRAPH_STATUS_INVALID;
285 goto end;
286 }
287
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
294 /* Ensure the components are not already part of another graph. */
295 upstream_graph = bt_component_get_graph(upstream_component);
296 if (upstream_graph && (graph != upstream_graph)) {
297 BT_LOGW("Invalid parameter: upstream port's component is already part of another graph: "
298 "other-graph-addr=%p", upstream_graph);
299 status = BT_GRAPH_STATUS_ALREADY_IN_A_GRAPH;
300 goto end;
301 }
302 upstream_was_already_in_graph = (graph == upstream_graph);
303 downstream_graph = bt_component_get_graph(downstream_component);
304 if (downstream_graph && (graph != downstream_graph)) {
305 BT_LOGW("Invalid parameter: downstream port's component is already part of another graph: "
306 "other-graph-addr=%p", downstream_graph);
307 status = BT_GRAPH_STATUS_ALREADY_IN_A_GRAPH;
308 goto end;
309 }
310 downstream_was_already_in_graph = (graph == downstream_graph);
311
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 */
317 BT_LOGD_STR("Asking upstream component to accept the connection.");
318 component_status = bt_component_accept_port_connection(
319 upstream_component, upstream_port, downstream_port);
320 if (component_status != BT_COMPONENT_STATUS_OK) {
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
328 status = bt_graph_status_from_component_status(
329 component_status);
330 goto end;
331 }
332
333 BT_LOGD_STR("Asking downstream component to accept the connection.");
334 component_status = bt_component_accept_port_connection(
335 downstream_component, downstream_port, upstream_port);
336 if (component_status != BT_COMPONENT_STATUS_OK) {
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
344 status = bt_graph_status_from_component_status(
345 component_status);
346 goto end;
347 }
348
349 BT_LOGD_STR("Creating connection.");
350 connection = bt_connection_create(graph, upstream_port,
351 downstream_port);
352 if (!connection) {
353 BT_LOGW("Cannot create connection object.");
354 status = BT_GRAPH_STATUS_NOMEM;
355 goto end;
356 }
357
358 BT_LOGD("Connection object created: conn-addr=%p", connection);
359
360 /*
361 * Ownership of upstream_component/downstream_component and of
362 * the connection object is transferred to the graph.
363 */
364 g_ptr_array_add(graph->connections, connection);
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 }
378 }
379
380 /*
381 * The graph is now the parent of these components which
382 * garantees their existence for the duration of the graph's
383 * lifetime.
384 */
385
386 /*
387 * Notify both components that their port is connected.
388 */
389 BT_LOGD_STR("Notifying upstream component that its port is connected.");
390 bt_component_port_connected(upstream_component, upstream_port,
391 downstream_port);
392 BT_LOGD_STR("Notifying downstream component that its port is connected.");
393 bt_component_port_connected(downstream_component, downstream_port,
394 upstream_port);
395
396 /*
397 * Notify the graph's creator that both ports are connected.
398 */
399 BT_LOGD_STR("Notifying graph's user that new component ports are connected.");
400 bt_graph_notify_ports_connected(graph, upstream_port, downstream_port);
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));
412
413 if (user_connection) {
414 /* Move reference to user */
415 *user_connection = connection;
416 connection = NULL;
417 }
418
419 end:
420 bt_put(upstream_graph);
421 bt_put(downstream_graph);
422 bt_put(upstream_component);
423 bt_put(downstream_component);
424 bt_put(connection);
425 return status;
426 }
427
428 static
429 enum bt_graph_status bt_graph_consume_no_check(struct bt_graph *graph)
430 {
431 struct bt_component *sink;
432 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
433 enum bt_component_status comp_status;
434 GList *current_node;
435
436 BT_LOGV("Making next sink consume: addr=%p", graph);
437
438 if (g_queue_is_empty(graph->sinks_to_consume)) {
439 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
440 status = BT_GRAPH_STATUS_END;
441 goto end;
442 }
443
444 current_node = g_queue_pop_head_link(graph->sinks_to_consume);
445 sink = current_node->data;
446 BT_LOGV("Chose next sink to consume: comp-addr=%p, comp-name=\"%s\"",
447 sink, bt_component_get_name(sink));
448 comp_status = bt_component_sink_consume(sink);
449 BT_LOGV("Consumed from sink: status=%s",
450 bt_component_status_string(comp_status));
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) {
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:
482 BT_LOGV("Graph consumed: status=%s", bt_graph_status_string(status));
483 return status;
484 }
485
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
509 enum bt_graph_status bt_graph_run(struct bt_graph *graph)
510 {
511 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
512
513 if (!graph) {
514 BT_LOGW_STR("Invalid parameter: graph is NULL.");
515 status = BT_GRAPH_STATUS_INVALID;
516 goto end;
517 }
518
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
526 BT_LOGV("Running graph: addr=%p", graph);
527
528 do {
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
542 status = bt_graph_consume(graph);
543 if (status == BT_GRAPH_STATUS_AGAIN) {
544 /*
545 * If AGAIN is received and there are multiple
546 * sinks, go ahead and consume from the next
547 * sink.
548 *
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.
554 */
555 if (graph->sinks_to_consume->length > 1) {
556 status = BT_GRAPH_STATUS_OK;
557 }
558 }
559 } while (status == BT_GRAPH_STATUS_OK);
560
561 if (g_queue_is_empty(graph->sinks_to_consume)) {
562 status = BT_GRAPH_STATUS_END;
563 }
564
565 end:
566 BT_LOGV("Graph ran: status=%s", bt_graph_status_string(status));
567 return status;
568 }
569
570 static
571 int add_listener(GArray *listeners, void *func, void *data)
572 {
573 struct bt_graph_listener listener = {
574 .func = func,
575 .data = data,
576 };
577
578 g_array_append_val(listeners, listener);
579 return listeners->len - 1;
580 }
581
582 int bt_graph_add_port_added_listener(
583 struct bt_graph *graph,
584 bt_graph_port_added_listener listener, void *data)
585 {
586 int ret;
587
588 if (!graph) {
589 BT_LOGW_STR("Invalid parameter: graph is NULL.");
590 ret = -1;
591 goto end;
592 }
593
594 if (!listener) {
595 BT_LOGW_STR("Invalid parameter: listener is NULL.");
596 ret = -1;
597 goto end;
598 }
599
600 ret = add_listener(graph->listeners.port_added, listener, data);
601 BT_LOGV("Added \"port added\" listener to graph: "
602 "graph-addr=%p, listener-addr=%p, pos=%d",
603 graph, listener, ret);
604
605 end:
606 return ret;
607 }
608
609 int bt_graph_add_port_removed_listener(
610 struct bt_graph *graph,
611 bt_graph_port_removed_listener listener, void *data)
612 {
613 int ret;
614
615 if (!graph) {
616 BT_LOGW_STR("Invalid parameter: graph is NULL.");
617 ret = -1;
618 goto end;
619 }
620
621 if (!listener) {
622 BT_LOGW_STR("Invalid parameter: listener is NULL.");
623 ret = -1;
624 goto end;
625 }
626
627 ret = add_listener(graph->listeners.port_removed, listener, data);
628 BT_LOGV("Added \"port removed\" listener to graph: "
629 "graph-addr=%p, listener-addr=%p, pos=%d",
630 graph, listener, ret);
631
632 end:
633 return ret;
634 }
635
636 int bt_graph_add_ports_connected_listener(
637 struct bt_graph *graph,
638 bt_graph_ports_connected_listener listener, void *data)
639 {
640 int ret;
641
642 if (!graph) {
643 BT_LOGW_STR("Invalid parameter: graph is NULL.");
644 ret = -1;
645 goto end;
646 }
647
648 if (!listener) {
649 BT_LOGW_STR("Invalid parameter: listener is NULL.");
650 ret = -1;
651 goto end;
652 }
653
654 ret = add_listener(graph->listeners.ports_connected, listener, data);
655 BT_LOGV("Added \"port connected\" listener to graph: "
656 "graph-addr=%p, listener-addr=%p, pos=%d",
657 graph, listener, ret);
658
659 end:
660 return ret;
661 }
662
663 int bt_graph_add_ports_disconnected_listener(
664 struct bt_graph *graph,
665 bt_graph_ports_disconnected_listener listener, void *data)
666 {
667 int ret;
668
669 if (!graph) {
670 BT_LOGW_STR("Invalid parameter: graph is NULL.");
671 ret = -1;
672 goto end;
673 }
674
675 if (!listener) {
676 BT_LOGW_STR("Invalid parameter: listener is NULL.");
677 ret = -1;
678 goto end;
679 }
680
681 ret = add_listener(graph->listeners.ports_disconnected, listener, data);
682 BT_LOGV("Added \"port disconnected\" listener to graph: "
683 "graph-addr=%p, listener-addr=%p, pos=%d",
684 graph, listener, ret);
685
686 end:
687 return ret;
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
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
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
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
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
732 void bt_graph_notify_ports_connected(struct bt_graph *graph,
733 struct bt_port *upstream_port, struct bt_port *downstream_port)
734 {
735 size_t i;
736
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
744 for (i = 0; i < graph->listeners.ports_connected->len; i++) {
745 struct bt_graph_listener listener =
746 g_array_index(graph->listeners.ports_connected,
747 struct bt_graph_listener, i);
748 bt_graph_ports_connected_listener func = listener.func;
749
750 assert(func);
751 func(upstream_port, downstream_port, listener.data);
752 }
753 }
754
755 BT_HIDDEN
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)
760 {
761 size_t i;
762
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
770 for (i = 0; i < graph->listeners.ports_disconnected->len; i++) {
771 struct bt_graph_listener listener =
772 g_array_index(graph->listeners.ports_disconnected,
773 struct bt_graph_listener, i);
774 bt_graph_ports_disconnected_listener func = listener.func;
775
776 assert(func);
777 func(upstream_comp, downstream_comp, upstream_port,
778 downstream_port, listener.data);
779 }
780 }
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) {
787 BT_LOGW_STR("Invalid parameter: graph is NULL.");
788 ret = BT_GRAPH_STATUS_INVALID;
789 goto end;
790 }
791
792 graph->canceled = BT_TRUE;
793 BT_LOGV("Canceled graph: addr=%p", graph);
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 }
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);
810 BT_LOGV("Removing graph's connection: graph-addr=%p, conn-addr=%p",
811 graph, connection);
812 g_ptr_array_remove(graph->connections, connection);
813 }
This page took 0.045752 seconds and 5 git commands to generate.