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