lib: add bt_{graph,query_executor}_add_interrupter()
[babeltrace.git] / src / lib / graph / graph.c
1 /*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #define BT_LOG_TAG "LIB/GRAPH"
25 #include "lib/logging.h"
26
27 #include "common/assert.h"
28 #include "lib/assert-pre.h"
29 #include "lib/assert-post.h"
30 #include <babeltrace2/graph/graph.h>
31 #include <babeltrace2/graph/graph-const.h>
32 #include <babeltrace2/graph/component-source-const.h>
33 #include <babeltrace2/graph/component-filter-const.h>
34 #include <babeltrace2/graph/port-const.h>
35 #include "lib/graph/message/message.h"
36 #include "compat/compiler.h"
37 #include "common/common.h"
38 #include <babeltrace2/types.h>
39 #include <babeltrace2/value.h>
40 #include <babeltrace2/value-const.h>
41 #include "lib/value.h"
42 #include <unistd.h>
43 #include <glib.h>
44
45 #include "component.h"
46 #include "component-sink.h"
47 #include "connection.h"
48 #include "graph.h"
49 #include "interrupter.h"
50 #include "message/event.h"
51 #include "message/packet.h"
52
53 typedef enum bt_graph_listener_func_status
54 (*port_added_func_t)(const void *, const void *, void *);
55
56 typedef enum bt_graph_listener_func_status
57 (*ports_connected_func_t)(const void *, const void *, const void *,
58 const void *, void *);
59
60 typedef enum bt_component_class_init_method_status
61 (*comp_init_method_t)(const void *, const void *, void *);
62
63 struct bt_graph_listener {
64 bt_graph_listener_removed_func removed;
65 void *data;
66 };
67
68 struct bt_graph_listener_port_added {
69 struct bt_graph_listener base;
70 port_added_func_t func;
71 };
72
73 struct bt_graph_listener_ports_connected {
74 struct bt_graph_listener base;
75 ports_connected_func_t func;
76 };
77
78 #define INIT_LISTENERS_ARRAY(_type, _listeners) \
79 do { \
80 _listeners = g_array_new(FALSE, TRUE, sizeof(_type)); \
81 if (!(_listeners)) { \
82 BT_LIB_LOGE_APPEND_CAUSE( \
83 "Failed to allocate one GArray."); \
84 } \
85 } while (0)
86
87 #define CALL_REMOVE_LISTENERS(_type, _listeners) \
88 do { \
89 size_t i; \
90 \
91 if (!_listeners) { \
92 break; \
93 } \
94 for (i = 0; i < (_listeners)->len; i++) { \
95 _type *listener = \
96 &g_array_index((_listeners), _type, i); \
97 \
98 if (listener->base.removed) { \
99 listener->base.removed(listener->base.data); \
100 } \
101 } \
102 } while (0)
103
104 static
105 void destroy_graph(struct bt_object *obj)
106 {
107 struct bt_graph *graph = container_of(obj, struct bt_graph, base);
108
109 /*
110 * The graph's reference count is 0 if we're here. Increment
111 * it to avoid a double-destroy (possibly infinitely recursive)
112 * in this situation:
113 *
114 * 1. We put and destroy a connection.
115 * 2. This connection's destructor finalizes its active message
116 * iterators.
117 * 3. A message iterator's finalization function gets a new
118 * reference on its component (reference count goes from 0 to
119 * 1).
120 * 4. Since this component's reference count goes to 1, it takes
121 * a reference on its parent (this graph). This graph's
122 * reference count goes from 0 to 1.
123 * 5. The message iterator's finalization function puts its
124 * component reference (reference count goes from 1 to 0).
125 * 6. Since this component's reference count goes from 1 to 0,
126 * it puts its parent (this graph). This graph's reference
127 * count goes from 1 to 0.
128 * 7. Since this graph's reference count goes from 1 to 0, its
129 * destructor is called (this function).
130 *
131 * With the incrementation below, the graph's reference count at
132 * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This
133 * ensures that this function is not called two times.
134 */
135 BT_LIB_LOGI("Destroying graph: %!+g", graph);
136 obj->ref_count++;
137 graph->config_state = BT_GRAPH_CONFIGURATION_STATE_DESTROYING;
138
139 /* Call all remove listeners */
140 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added,
141 graph->listeners.source_output_port_added);
142 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added,
143 graph->listeners.filter_output_port_added);
144 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added,
145 graph->listeners.filter_input_port_added);
146 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added,
147 graph->listeners.sink_input_port_added);
148 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected,
149 graph->listeners.source_filter_ports_connected);
150 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected,
151 graph->listeners.filter_filter_ports_connected);
152 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected,
153 graph->listeners.source_sink_ports_connected);
154 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected,
155 graph->listeners.filter_sink_ports_connected);
156
157 if (graph->messages) {
158 g_ptr_array_free(graph->messages, TRUE);
159 graph->messages = NULL;
160 }
161
162 if (graph->connections) {
163 BT_LOGD_STR("Destroying connections.");
164 g_ptr_array_free(graph->connections, TRUE);
165 graph->connections = NULL;
166 }
167
168 if (graph->components) {
169 BT_LOGD_STR("Destroying components.");
170 g_ptr_array_free(graph->components, TRUE);
171 graph->components = NULL;
172 }
173
174 if (graph->interrupters) {
175 BT_LOGD_STR("Putting interrupters.");
176 g_ptr_array_free(graph->interrupters, TRUE);
177 graph->interrupters = NULL;
178 }
179
180 BT_OBJECT_PUT_REF_AND_RESET(graph->default_interrupter);
181
182 if (graph->sinks_to_consume) {
183 g_queue_free(graph->sinks_to_consume);
184 graph->sinks_to_consume = NULL;
185 }
186
187 if (graph->listeners.source_output_port_added) {
188 g_array_free(graph->listeners.source_output_port_added, TRUE);
189 graph->listeners.source_output_port_added = NULL;
190 }
191
192 if (graph->listeners.filter_output_port_added) {
193 g_array_free(graph->listeners.filter_output_port_added, TRUE);
194 graph->listeners.filter_output_port_added = NULL;
195 }
196
197 if (graph->listeners.filter_input_port_added) {
198 g_array_free(graph->listeners.filter_input_port_added, TRUE);
199 graph->listeners.filter_input_port_added = NULL;
200 }
201
202 if (graph->listeners.sink_input_port_added) {
203 g_array_free(graph->listeners.sink_input_port_added, TRUE);
204 graph->listeners.sink_input_port_added = NULL;
205 }
206
207 if (graph->listeners.source_filter_ports_connected) {
208 g_array_free(graph->listeners.source_filter_ports_connected,
209 TRUE);
210 graph->listeners.source_filter_ports_connected = NULL;
211 }
212
213 if (graph->listeners.filter_filter_ports_connected) {
214 g_array_free(graph->listeners.filter_filter_ports_connected,
215 TRUE);
216 graph->listeners.filter_filter_ports_connected = NULL;
217 }
218
219 if (graph->listeners.source_sink_ports_connected) {
220 g_array_free(graph->listeners.source_sink_ports_connected,
221 TRUE);
222 graph->listeners.source_sink_ports_connected = NULL;
223 }
224
225 if (graph->listeners.filter_sink_ports_connected) {
226 g_array_free(graph->listeners.filter_sink_ports_connected,
227 TRUE);
228 graph->listeners.filter_sink_ports_connected = NULL;
229 }
230
231 bt_object_pool_finalize(&graph->event_msg_pool);
232 bt_object_pool_finalize(&graph->packet_begin_msg_pool);
233 bt_object_pool_finalize(&graph->packet_end_msg_pool);
234 g_free(graph);
235 }
236
237 static
238 void destroy_message_event(struct bt_message *msg,
239 struct bt_graph *graph)
240 {
241 bt_message_event_destroy(msg);
242 }
243
244 static
245 void destroy_message_packet_begin(struct bt_message *msg,
246 struct bt_graph *graph)
247 {
248 bt_message_packet_destroy(msg);
249 }
250
251 static
252 void destroy_message_packet_end(struct bt_message *msg,
253 struct bt_graph *graph)
254 {
255 bt_message_packet_destroy(msg);
256 }
257
258 static
259 void notify_message_graph_is_destroyed(struct bt_message *msg)
260 {
261 bt_message_unlink_graph(msg);
262 }
263
264 struct bt_graph *bt_graph_create(void)
265 {
266 struct bt_graph *graph;
267 int ret;
268
269 BT_LOGI_STR("Creating graph object.");
270 graph = g_new0(struct bt_graph, 1);
271 if (!graph) {
272 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one graph.");
273 goto end;
274 }
275
276 bt_object_init_shared(&graph->base, destroy_graph);
277 graph->connections = g_ptr_array_new_with_free_func(
278 (GDestroyNotify) bt_object_try_spec_release);
279 if (!graph->connections) {
280 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
281 goto error;
282 }
283 graph->components = g_ptr_array_new_with_free_func(
284 (GDestroyNotify) bt_object_try_spec_release);
285 if (!graph->components) {
286 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
287 goto error;
288 }
289 graph->sinks_to_consume = g_queue_new();
290 if (!graph->sinks_to_consume) {
291 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GQueue.");
292 goto error;
293 }
294
295 bt_graph_set_can_consume(graph, true);
296 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added,
297 graph->listeners.source_output_port_added);
298
299 if (!graph->listeners.source_output_port_added) {
300 ret = -1;
301 goto error;
302 }
303
304 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added,
305 graph->listeners.filter_output_port_added);
306
307 if (!graph->listeners.filter_output_port_added) {
308 ret = -1;
309 goto error;
310 }
311
312 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added,
313 graph->listeners.filter_input_port_added);
314
315 if (!graph->listeners.filter_input_port_added) {
316 ret = -1;
317 goto error;
318 }
319
320 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added,
321 graph->listeners.sink_input_port_added);
322
323 if (!graph->listeners.sink_input_port_added) {
324 ret = -1;
325 goto error;
326 }
327
328 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected,
329 graph->listeners.source_filter_ports_connected);
330
331 if (!graph->listeners.source_filter_ports_connected) {
332 ret = -1;
333 goto error;
334 }
335
336 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected,
337 graph->listeners.source_sink_ports_connected);
338
339 if (!graph->listeners.source_sink_ports_connected) {
340 ret = -1;
341 goto error;
342 }
343
344 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected,
345 graph->listeners.filter_filter_ports_connected);
346
347 if (!graph->listeners.filter_filter_ports_connected) {
348 ret = -1;
349 goto error;
350 }
351
352 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected,
353 graph->listeners.filter_sink_ports_connected);
354
355 if (!graph->listeners.filter_sink_ports_connected) {
356 ret = -1;
357 goto error;
358 }
359
360 graph->interrupters = g_ptr_array_new_with_free_func(
361 (GDestroyNotify) bt_object_put_no_null_check);
362 if (!graph->interrupters) {
363 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
364 goto error;
365 }
366
367 graph->default_interrupter = bt_interrupter_create();
368 if (!graph->default_interrupter) {
369 BT_LIB_LOGE_APPEND_CAUSE(
370 "Failed to create one interrupter object.");
371 goto error;
372 }
373
374 bt_graph_add_interrupter(graph, graph->default_interrupter);
375 ret = bt_object_pool_initialize(&graph->event_msg_pool,
376 (bt_object_pool_new_object_func) bt_message_event_new,
377 (bt_object_pool_destroy_object_func) destroy_message_event,
378 graph);
379 if (ret) {
380 BT_LIB_LOGE_APPEND_CAUSE(
381 "Failed to initialize event message pool: ret=%d",
382 ret);
383 goto error;
384 }
385
386 ret = bt_object_pool_initialize(&graph->packet_begin_msg_pool,
387 (bt_object_pool_new_object_func) bt_message_packet_beginning_new,
388 (bt_object_pool_destroy_object_func) destroy_message_packet_begin,
389 graph);
390 if (ret) {
391 BT_LIB_LOGE_APPEND_CAUSE(
392 "Failed to initialize packet beginning message pool: ret=%d",
393 ret);
394 goto error;
395 }
396
397 ret = bt_object_pool_initialize(&graph->packet_end_msg_pool,
398 (bt_object_pool_new_object_func) bt_message_packet_end_new,
399 (bt_object_pool_destroy_object_func) destroy_message_packet_end,
400 graph);
401 if (ret) {
402 BT_LIB_LOGE_APPEND_CAUSE(
403 "Failed to initialize packet end message pool: ret=%d",
404 ret);
405 goto error;
406 }
407
408 graph->messages = g_ptr_array_new_with_free_func(
409 (GDestroyNotify) notify_message_graph_is_destroyed);
410 BT_LIB_LOGI("Created graph object: %!+g", graph);
411
412 end:
413 return (void *) graph;
414
415 error:
416 BT_OBJECT_PUT_REF_AND_RESET(graph);
417 goto end;
418 }
419
420 enum bt_graph_connect_ports_status bt_graph_connect_ports(
421 struct bt_graph *graph,
422 const struct bt_port_output *upstream_port_out,
423 const struct bt_port_input *downstream_port_in,
424 const struct bt_connection **user_connection)
425 {
426 enum bt_graph_connect_ports_status status = BT_FUNC_STATUS_OK;
427 enum bt_graph_listener_func_status listener_status;
428 struct bt_connection *connection = NULL;
429 struct bt_port *upstream_port = (void *) upstream_port_out;
430 struct bt_port *downstream_port = (void *) downstream_port_in;
431 struct bt_component *upstream_component = NULL;
432 struct bt_component *downstream_component = NULL;
433 enum bt_component_class_port_connected_method_status port_connected_status;
434 bool init_can_consume;
435
436 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
437 BT_ASSERT_PRE_NON_NULL(upstream_port, "Upstream port");
438 BT_ASSERT_PRE_NON_NULL(downstream_port, "Downstream port port");
439 BT_ASSERT_PRE(
440 graph->config_state == BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
441 "Graph is not in the \"configuring\" state: %!+g", graph);
442 BT_ASSERT_PRE(!bt_port_is_connected(upstream_port),
443 "Upstream port is already connected: %!+p", upstream_port);
444 BT_ASSERT_PRE(!bt_port_is_connected(downstream_port),
445 "Downstream port is already connected: %!+p", downstream_port);
446 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) upstream_port),
447 "Upstream port does not belong to a component: %!+p",
448 upstream_port);
449 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) downstream_port),
450 "Downstream port does not belong to a component: %!+p",
451 downstream_port);
452 init_can_consume = graph->can_consume;
453 BT_LIB_LOGI("Connecting component ports within graph: "
454 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
455 graph, upstream_port, downstream_port);
456 bt_graph_set_can_consume(graph, false);
457 upstream_component = bt_port_borrow_component_inline(
458 (void *) upstream_port);
459 downstream_component = bt_port_borrow_component_inline(
460 (void *) downstream_port);
461
462 BT_LOGD_STR("Creating connection.");
463 connection = bt_connection_create(graph, (void *) upstream_port,
464 (void *) downstream_port);
465 if (!connection) {
466 BT_LIB_LOGE_APPEND_CAUSE("Cannot create connection object.");
467 status = BT_FUNC_STATUS_MEMORY_ERROR;
468 goto end;
469 }
470
471 BT_LIB_LOGD("Connection object created: %!+x", connection);
472
473 /*
474 * Ownership of upstream_component/downstream_component and of
475 * the connection object is transferred to the graph.
476 */
477 g_ptr_array_add(graph->connections, connection);
478
479 /*
480 * Notify both components that their port is connected.
481 */
482 BT_LIB_LOGD("Notifying upstream component that its port is connected: "
483 "%![comp-]+c, %![port-]+p", upstream_component, upstream_port);
484 port_connected_status = bt_component_port_connected(upstream_component,
485 (void *) upstream_port, (void *) downstream_port);
486 if (port_connected_status != BT_FUNC_STATUS_OK) {
487 if (port_connected_status < 0) {
488 BT_LIB_LOGW_APPEND_CAUSE(
489 "Upstream component's \"port connected\" method failed: "
490 "status=%s, %![graph-]+g, %![up-comp-]+c, "
491 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
492 bt_common_func_status_string(
493 port_connected_status),
494 graph, upstream_component, downstream_component,
495 upstream_port, downstream_port);
496 }
497
498 bt_connection_end(connection, true);
499 status = (int) port_connected_status;
500 goto end;
501 }
502
503 connection->notified_upstream_port_connected = true;
504 BT_LIB_LOGD("Notifying downstream component that its port is connected: "
505 "%![comp-]+c, %![port-]+p", downstream_component,
506 downstream_port);
507 port_connected_status = bt_component_port_connected(downstream_component,
508 (void *) downstream_port, (void *) upstream_port);
509 if (port_connected_status != BT_FUNC_STATUS_OK) {
510 if (port_connected_status < 0) {
511 BT_LIB_LOGW_APPEND_CAUSE(
512 "Downstream component's \"port connected\" method failed: "
513 "status=%s, %![graph-]+g, %![up-comp-]+c, "
514 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
515 bt_common_func_status_string(
516 port_connected_status),
517 graph, upstream_component, downstream_component,
518 upstream_port, downstream_port);
519 }
520
521 bt_connection_end(connection, true);
522 status = (int) port_connected_status;
523 goto end;
524 }
525
526 connection->notified_downstream_port_connected = true;
527
528 /*
529 * Notify the graph's creator that both ports are connected.
530 */
531 BT_LOGD_STR("Notifying graph's user that new component ports are connected.");
532 listener_status = bt_graph_notify_ports_connected(graph, upstream_port, downstream_port);
533 if (listener_status != BT_FUNC_STATUS_OK) {
534 if (listener_status < 0) {
535 BT_LIB_LOGW_APPEND_CAUSE(
536 "Graph \"ports connected\" listener failed: "
537 "status=%d, %![graph-]+g, %![up-comp-]+c, "
538 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
539 listener_status, graph,
540 upstream_component, downstream_component,
541 upstream_port, downstream_port);
542 }
543
544 status = (int) listener_status;
545 goto end;
546 }
547
548 connection->notified_graph_ports_connected = true;
549 BT_LIB_LOGI("Connected component ports within graph: "
550 "%![graph-]+g, %![up-comp-]+c, %![down-comp-]+c, "
551 "%![up-port-]+p, %![down-port-]+p",
552 graph, upstream_component, downstream_component,
553 upstream_port, downstream_port);
554
555 if (user_connection) {
556 /* Move reference to user */
557 *user_connection = connection;
558 connection = NULL;
559 }
560
561 end:
562 if (status != BT_FUNC_STATUS_OK) {
563 bt_graph_make_faulty(graph);
564 }
565
566 bt_object_put_ref(connection);
567 (void) init_can_consume;
568 bt_graph_set_can_consume(graph, init_can_consume);
569 return status;
570 }
571
572 static inline
573 int consume_graph_sink(struct bt_component_sink *comp)
574 {
575 enum bt_component_class_sink_consume_method_status consume_status;
576 struct bt_component_class_sink *sink_class = NULL;
577
578 BT_ASSERT(comp);
579 sink_class = (void *) comp->parent.class;
580 BT_ASSERT(sink_class->methods.consume);
581 BT_LIB_LOGD("Calling user's consume method: %!+c", comp);
582 consume_status = sink_class->methods.consume((void *) comp);
583 BT_LOGD("User method returned: status=%s",
584 bt_common_func_status_string(consume_status));
585 BT_ASSERT_POST_DEV(consume_status == BT_FUNC_STATUS_OK ||
586 consume_status == BT_FUNC_STATUS_END ||
587 consume_status == BT_FUNC_STATUS_AGAIN ||
588 consume_status == BT_FUNC_STATUS_ERROR ||
589 consume_status == BT_FUNC_STATUS_MEMORY_ERROR,
590 "Invalid component status returned by consuming method: "
591 "status=%s", bt_common_func_status_string(consume_status));
592 if (consume_status) {
593 if (consume_status < 0) {
594 BT_LIB_LOGW_APPEND_CAUSE(
595 "Component's \"consume\" method failed: "
596 "status=%s, %![comp-]+c",
597 bt_common_func_status_string(consume_status),
598 comp);
599 }
600
601 goto end;
602 }
603
604 BT_LIB_LOGD("Consumed from sink: %![comp-]+c, status=%s",
605 comp, bt_common_func_status_string(consume_status));
606
607 end:
608 return consume_status;
609 }
610
611 /*
612 * `node` is removed from the queue of sinks to consume when passed to
613 * this function. This function adds it back to the queue if there's
614 * still something to consume afterwards.
615 */
616 static inline
617 int consume_sink_node(struct bt_graph *graph, GList *node)
618 {
619 int status;
620 struct bt_component_sink *sink;
621
622 sink = node->data;
623 status = consume_graph_sink(sink);
624 if (G_UNLIKELY(status != BT_FUNC_STATUS_END)) {
625 g_queue_push_tail_link(graph->sinks_to_consume, node);
626 goto end;
627 }
628
629 /* End reached, the node is not added back to the queue and free'd. */
630 g_queue_delete_link(graph->sinks_to_consume, node);
631
632 /* Don't forward an END status if there are sinks left to consume. */
633 if (!g_queue_is_empty(graph->sinks_to_consume)) {
634 status = BT_FUNC_STATUS_OK;
635 goto end;
636 }
637
638 end:
639 BT_LIB_LOGD("Consumed sink node: %![comp-]+c, status=%s",
640 sink, bt_common_func_status_string(status));
641 return status;
642 }
643
644 BT_HIDDEN
645 int bt_graph_consume_sink_no_check(struct bt_graph *graph,
646 struct bt_component_sink *sink)
647 {
648 int status;
649 GList *sink_node;
650 int index;
651
652 BT_LIB_LOGD("Making specific sink consume: %![comp-]+c", sink);
653 BT_ASSERT(bt_component_borrow_graph((void *) sink) == graph);
654
655 if (g_queue_is_empty(graph->sinks_to_consume)) {
656 BT_LOGD_STR("Graph's sink queue is empty: end of graph.");
657 status = BT_FUNC_STATUS_END;
658 goto end;
659 }
660
661 index = g_queue_index(graph->sinks_to_consume, sink);
662 if (index < 0) {
663 BT_LIB_LOGD("Sink component is not marked as consumable: "
664 "component sink is ended: %![comp-]+c", sink);
665 status = BT_FUNC_STATUS_END;
666 goto end;
667 }
668
669 sink_node = g_queue_pop_nth_link(graph->sinks_to_consume, index);
670 BT_ASSERT(sink_node);
671 status = consume_sink_node(graph, sink_node);
672
673 end:
674 return status;
675 }
676
677 static inline
678 int consume_no_check(struct bt_graph *graph)
679 {
680 int status = BT_FUNC_STATUS_OK;
681 struct bt_component *sink;
682 GList *current_node;
683
684 BT_ASSERT_PRE_DEV(graph->has_sink,
685 "Graph has no sink component: %!+g", graph);
686 BT_LIB_LOGD("Making next sink component consume: %![graph-]+g", graph);
687
688 if (G_UNLIKELY(g_queue_is_empty(graph->sinks_to_consume))) {
689 BT_LOGD_STR("Graph's sink queue is empty: end of graph.");
690 status = BT_FUNC_STATUS_END;
691 goto end;
692 }
693
694 current_node = g_queue_pop_head_link(graph->sinks_to_consume);
695 sink = current_node->data;
696 BT_LIB_LOGD("Chose next sink to consume: %!+c", sink);
697 status = consume_sink_node(graph, current_node);
698
699 end:
700 return status;
701 }
702
703 enum bt_graph_consume_status bt_graph_consume(struct bt_graph *graph)
704 {
705 enum bt_graph_consume_status status;
706
707 BT_ASSERT_PRE_DEV_NON_NULL(graph, "Graph");
708 BT_ASSERT_PRE_DEV(graph->can_consume,
709 "Cannot consume graph in its current state: %!+g", graph);
710 BT_ASSERT_PRE_DEV(graph->config_state !=
711 BT_GRAPH_CONFIGURATION_STATE_FAULTY,
712 "Graph is in a faulty state: %!+g", graph);
713 bt_graph_set_can_consume(graph, false);
714 status = bt_graph_configure(graph);
715 if (G_UNLIKELY(status)) {
716 /* bt_graph_configure() logs errors */
717 goto end;
718 }
719
720 status = consume_no_check(graph);
721 bt_graph_set_can_consume(graph, true);
722
723 end:
724 return status;
725 }
726
727 enum bt_graph_run_status bt_graph_run(struct bt_graph *graph)
728 {
729 enum bt_graph_run_status status;
730
731 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
732 BT_ASSERT_PRE(graph->can_consume,
733 "Cannot consume graph in its current state: %!+g", graph);
734 BT_ASSERT_PRE(graph->config_state != BT_GRAPH_CONFIGURATION_STATE_FAULTY,
735 "Graph is in a faulty state: %!+g", graph);
736 bt_graph_set_can_consume(graph, false);
737 status = bt_graph_configure(graph);
738 if (G_UNLIKELY(status)) {
739 /* bt_graph_configure() logs errors */
740 goto end;
741 }
742
743 BT_LIB_LOGI("Running graph: %!+g", graph);
744
745 do {
746 /*
747 * Check if the graph is interrupted at each iteration.
748 * If the graph was interrupted by another thread or by
749 * a signal handler, this is NOT a warning nor an error;
750 * it was intentional: log with an INFO level only.
751 */
752 if (G_UNLIKELY(bt_graph_is_interrupted(graph))) {
753 BT_LIB_LOGI("Stopping the graph: "
754 "graph was interrupted: %!+g", graph);
755 status = BT_FUNC_STATUS_AGAIN;
756 goto end;
757 }
758
759 status = consume_no_check(graph);
760 if (G_UNLIKELY(status == BT_FUNC_STATUS_AGAIN)) {
761 /*
762 * If AGAIN is received and there are multiple
763 * sinks, go ahead and consume from the next
764 * sink.
765 *
766 * However, in the case where a single sink is
767 * left, the caller can decide to busy-wait and
768 * call bt_graph_run() continuously
769 * until the source is ready or it can decide to
770 * sleep for an arbitrary amount of time.
771 */
772 if (graph->sinks_to_consume->length > 1) {
773 status = BT_FUNC_STATUS_OK;
774 }
775 }
776 } while (status == BT_FUNC_STATUS_OK);
777
778 if (g_queue_is_empty(graph->sinks_to_consume)) {
779 status = BT_FUNC_STATUS_END;
780 }
781
782 end:
783 BT_LIB_LOGI("Graph ran: %![graph-]+g, status=%s", graph,
784 bt_common_func_status_string(status));
785 bt_graph_set_can_consume(graph, true);
786 return status;
787 }
788
789 enum bt_graph_add_listener_status
790 bt_graph_add_source_component_output_port_added_listener(
791 struct bt_graph *graph,
792 bt_graph_source_component_output_port_added_listener_func func,
793 bt_graph_listener_removed_func listener_removed, void *data,
794 int *out_listener_id)
795 {
796 struct bt_graph_listener_port_added listener = {
797 .base = {
798 .removed = listener_removed,
799 .data = data,
800 },
801 .func = (port_added_func_t) func,
802 };
803 int listener_id;
804
805 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
806 BT_ASSERT_PRE_NON_NULL(func, "Listener");
807 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
808 BT_ASSERT_PRE(!graph->in_remove_listener,
809 "Graph currently executing a \"listener removed\" listener: "
810 "%!+g", graph);
811 g_array_append_val(graph->listeners.source_output_port_added, listener);
812 listener_id = graph->listeners.source_output_port_added->len - 1;
813 BT_LIB_LOGD("Added \"source component output port added\" listener to graph: "
814 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
815 listener_id);
816
817 if (listener_id) {
818 *out_listener_id = listener_id;
819 }
820
821 return BT_FUNC_STATUS_OK;
822 }
823
824 enum bt_graph_add_listener_status
825 bt_graph_add_filter_component_output_port_added_listener(
826 struct bt_graph *graph,
827 bt_graph_filter_component_output_port_added_listener_func func,
828 bt_graph_listener_removed_func listener_removed, void *data,
829 int *out_listener_id)
830 {
831 struct bt_graph_listener_port_added listener = {
832 .base = {
833 .removed = listener_removed,
834 .data = data,
835 },
836 .func = (port_added_func_t) func,
837 };
838 int listener_id;
839
840 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
841 BT_ASSERT_PRE_NON_NULL(func, "Listener");
842 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
843 BT_ASSERT_PRE(!graph->in_remove_listener,
844 "Graph currently executing a \"listener removed\" listener: "
845 "%!+g", graph);
846 g_array_append_val(graph->listeners.filter_output_port_added, listener);
847 listener_id = graph->listeners.filter_output_port_added->len - 1;
848 BT_LIB_LOGD("Added \"filter component output port added\" listener to graph: "
849 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
850 listener_id);
851
852 if (listener_id) {
853 *out_listener_id = listener_id;
854 }
855
856 return BT_FUNC_STATUS_OK;
857 }
858
859 enum bt_graph_add_listener_status
860 bt_graph_add_filter_component_input_port_added_listener(
861 struct bt_graph *graph,
862 bt_graph_filter_component_input_port_added_listener_func func,
863 bt_graph_listener_removed_func listener_removed, void *data,
864 int *out_listener_id)
865 {
866 struct bt_graph_listener_port_added listener = {
867 .base = {
868 .removed = listener_removed,
869 .data = data,
870 },
871 .func = (port_added_func_t) func,
872 };
873 int listener_id;
874
875 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
876 BT_ASSERT_PRE_NON_NULL(func, "Listener");
877 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
878 BT_ASSERT_PRE(!graph->in_remove_listener,
879 "Graph currently executing a \"listener removed\" listener: "
880 "%!+g", graph);
881 g_array_append_val(graph->listeners.filter_input_port_added, listener);
882 listener_id = graph->listeners.filter_input_port_added->len - 1;
883 BT_LIB_LOGD("Added \"filter component input port added\" listener to graph: "
884 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
885 listener_id);
886
887 if (listener_id) {
888 *out_listener_id = listener_id;
889 }
890
891 return BT_FUNC_STATUS_OK;
892 }
893
894 enum bt_graph_add_listener_status
895 bt_graph_add_sink_component_input_port_added_listener(
896 struct bt_graph *graph,
897 bt_graph_sink_component_input_port_added_listener_func func,
898 bt_graph_listener_removed_func listener_removed, void *data,
899 int *out_listener_id)
900 {
901 struct bt_graph_listener_port_added listener = {
902 .base = {
903 .removed = listener_removed,
904 .data = data,
905 },
906 .func = (port_added_func_t) func,
907 };
908 int listener_id;
909
910 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
911 BT_ASSERT_PRE_NON_NULL(func, "Listener");
912 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
913 BT_ASSERT_PRE(!graph->in_remove_listener,
914 "Graph currently executing a \"listener removed\" listener: "
915 "%!+g", graph);
916 g_array_append_val(graph->listeners.sink_input_port_added, listener);
917 listener_id = graph->listeners.sink_input_port_added->len - 1;
918 BT_LIB_LOGD("Added \"sink component input port added\" listener to graph: "
919 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
920 listener_id);
921
922 if (listener_id) {
923 *out_listener_id = listener_id;
924 }
925
926 return BT_FUNC_STATUS_OK;
927 }
928
929 enum bt_graph_add_listener_status
930 bt_graph_add_source_filter_component_ports_connected_listener(
931 struct bt_graph *graph,
932 bt_graph_source_filter_component_ports_connected_listener_func func,
933 bt_graph_listener_removed_func listener_removed, void *data,
934 int *out_listener_id)
935 {
936 struct bt_graph_listener_ports_connected listener = {
937 .base = {
938 .removed = listener_removed,
939 .data = data,
940 },
941 .func = (ports_connected_func_t) func,
942 };
943 int listener_id;
944
945 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
946 BT_ASSERT_PRE_NON_NULL(func, "Listener");
947 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
948 BT_ASSERT_PRE(!graph->in_remove_listener,
949 "Graph currently executing a \"listener removed\" listener: "
950 "%!+g", graph);
951 g_array_append_val(graph->listeners.source_filter_ports_connected,
952 listener);
953 listener_id = graph->listeners.source_filter_ports_connected->len - 1;
954 BT_LIB_LOGD("Added \"source to filter component ports connected\" listener to graph: "
955 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
956 listener_id);
957
958 if (listener_id) {
959 *out_listener_id = listener_id;
960 }
961
962 return BT_FUNC_STATUS_OK;
963 }
964
965 enum bt_graph_add_listener_status
966 bt_graph_add_source_sink_component_ports_connected_listener(
967 struct bt_graph *graph,
968 bt_graph_source_sink_component_ports_connected_listener_func func,
969 bt_graph_listener_removed_func listener_removed, void *data,
970 int *out_listener_id)
971 {
972 struct bt_graph_listener_ports_connected listener = {
973 .base = {
974 .removed = listener_removed,
975 .data = data,
976 },
977 .func = (ports_connected_func_t) func,
978 };
979 int listener_id;
980
981 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
982 BT_ASSERT_PRE_NON_NULL(func, "Listener");
983 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
984 BT_ASSERT_PRE(!graph->in_remove_listener,
985 "Graph currently executing a \"listener removed\" listener: "
986 "%!+g", graph);
987 g_array_append_val(graph->listeners.source_sink_ports_connected,
988 listener);
989 listener_id = graph->listeners.source_sink_ports_connected->len - 1;
990 BT_LIB_LOGD("Added \"source to sink component ports connected\" listener to graph: "
991 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
992 listener_id);
993
994 if (listener_id) {
995 *out_listener_id = listener_id;
996 }
997
998 return BT_FUNC_STATUS_OK;
999 }
1000
1001 enum bt_graph_add_listener_status
1002 bt_graph_add_filter_filter_component_ports_connected_listener(
1003 struct bt_graph *graph,
1004 bt_graph_filter_filter_component_ports_connected_listener_func func,
1005 bt_graph_listener_removed_func listener_removed, void *data,
1006 int *out_listener_id)
1007 {
1008 struct bt_graph_listener_ports_connected listener = {
1009 .base = {
1010 .removed = listener_removed,
1011 .data = data,
1012 },
1013 .func = (ports_connected_func_t) func,
1014 };
1015 int listener_id;
1016
1017 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1018 BT_ASSERT_PRE_NON_NULL(func, "Listener");
1019 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
1020 BT_ASSERT_PRE(!graph->in_remove_listener,
1021 "Graph currently executing a \"listener removed\" listener: "
1022 "%!+g", graph);
1023 g_array_append_val(graph->listeners.filter_filter_ports_connected,
1024 listener);
1025 listener_id = graph->listeners.filter_filter_ports_connected->len - 1;
1026 BT_LIB_LOGD("Added \"filter to filter component ports connected\" listener to graph: "
1027 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
1028 listener_id);
1029
1030 if (listener_id) {
1031 *out_listener_id = listener_id;
1032 }
1033
1034 return BT_FUNC_STATUS_OK;
1035 }
1036
1037 enum bt_graph_add_listener_status
1038 bt_graph_add_filter_sink_component_ports_connected_listener(
1039 struct bt_graph *graph,
1040 bt_graph_filter_sink_component_ports_connected_listener_func func,
1041 bt_graph_listener_removed_func listener_removed, void *data,
1042 int *out_listener_id)
1043 {
1044 struct bt_graph_listener_ports_connected listener = {
1045 .base = {
1046 .removed = listener_removed,
1047 .data = data,
1048 },
1049 .func = (ports_connected_func_t) func,
1050 };
1051 int listener_id;
1052
1053 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1054 BT_ASSERT_PRE_NON_NULL(func, "Listener");
1055 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
1056 BT_ASSERT_PRE(!graph->in_remove_listener,
1057 "Graph currently executing a \"listener removed\" listener: "
1058 "%!+g", graph);
1059 g_array_append_val(graph->listeners.filter_sink_ports_connected,
1060 listener);
1061 listener_id = graph->listeners.filter_sink_ports_connected->len - 1;
1062 BT_LIB_LOGD("Added \"filter to sink component ports connected\" listener to graph: "
1063 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
1064 listener_id);
1065
1066 if (listener_id) {
1067 *out_listener_id = listener_id;
1068 }
1069
1070 return BT_FUNC_STATUS_OK;
1071 }
1072
1073 BT_HIDDEN
1074 enum bt_graph_listener_func_status bt_graph_notify_port_added(
1075 struct bt_graph *graph, struct bt_port *port)
1076 {
1077 uint64_t i;
1078 GArray *listeners;
1079 struct bt_component *comp;
1080 enum bt_graph_listener_func_status status = BT_FUNC_STATUS_OK;
1081
1082 BT_ASSERT(graph);
1083 BT_ASSERT(port);
1084 BT_LIB_LOGD("Notifying graph listeners that a port was added: "
1085 "%![graph-]+g, %![port-]+p", graph, port);
1086 comp = bt_port_borrow_component_inline(port);
1087 BT_ASSERT(comp);
1088
1089 switch (comp->class->type) {
1090 case BT_COMPONENT_CLASS_TYPE_SOURCE:
1091 {
1092 switch (port->type) {
1093 case BT_PORT_TYPE_OUTPUT:
1094 listeners = graph->listeners.source_output_port_added;
1095 break;
1096 default:
1097 abort();
1098 }
1099
1100 break;
1101 }
1102 case BT_COMPONENT_CLASS_TYPE_FILTER:
1103 {
1104 switch (port->type) {
1105 case BT_PORT_TYPE_INPUT:
1106 listeners = graph->listeners.filter_input_port_added;
1107 break;
1108 case BT_PORT_TYPE_OUTPUT:
1109 listeners = graph->listeners.filter_output_port_added;
1110 break;
1111 default:
1112 abort();
1113 }
1114
1115 break;
1116 }
1117 case BT_COMPONENT_CLASS_TYPE_SINK:
1118 {
1119 switch (port->type) {
1120 case BT_PORT_TYPE_INPUT:
1121 listeners = graph->listeners.sink_input_port_added;
1122 break;
1123 default:
1124 abort();
1125 }
1126
1127 break;
1128 }
1129 default:
1130 abort();
1131 }
1132
1133 for (i = 0; i < listeners->len; i++) {
1134 struct bt_graph_listener_port_added *listener =
1135 &g_array_index(listeners,
1136 struct bt_graph_listener_port_added, i);
1137
1138
1139 BT_ASSERT(listener->func);
1140 status = listener->func(comp, port, listener->base.data);
1141 if (status != BT_FUNC_STATUS_OK) {
1142 goto end;
1143 }
1144 }
1145
1146 end:
1147 return status;
1148 }
1149
1150 BT_HIDDEN
1151 enum bt_graph_listener_func_status bt_graph_notify_ports_connected(
1152 struct bt_graph *graph, struct bt_port *upstream_port,
1153 struct bt_port *downstream_port)
1154 {
1155 uint64_t i;
1156 GArray *listeners;
1157 struct bt_component *upstream_comp;
1158 struct bt_component *downstream_comp;
1159 enum bt_graph_listener_func_status status = BT_FUNC_STATUS_OK;
1160
1161 BT_ASSERT(graph);
1162 BT_ASSERT(upstream_port);
1163 BT_ASSERT(downstream_port);
1164 BT_LIB_LOGD("Notifying graph listeners that ports were connected: "
1165 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
1166 graph, upstream_port, downstream_port);
1167 upstream_comp = bt_port_borrow_component_inline(upstream_port);
1168 BT_ASSERT(upstream_comp);
1169 downstream_comp = bt_port_borrow_component_inline(downstream_port);
1170 BT_ASSERT(downstream_comp);
1171
1172 switch (upstream_comp->class->type) {
1173 case BT_COMPONENT_CLASS_TYPE_SOURCE:
1174 {
1175 switch (downstream_comp->class->type) {
1176 case BT_COMPONENT_CLASS_TYPE_FILTER:
1177 listeners =
1178 graph->listeners.source_filter_ports_connected;
1179 break;
1180 case BT_COMPONENT_CLASS_TYPE_SINK:
1181 listeners =
1182 graph->listeners.source_sink_ports_connected;
1183 break;
1184 default:
1185 abort();
1186 }
1187
1188 break;
1189 }
1190 case BT_COMPONENT_CLASS_TYPE_FILTER:
1191 {
1192 switch (downstream_comp->class->type) {
1193 case BT_COMPONENT_CLASS_TYPE_FILTER:
1194 listeners =
1195 graph->listeners.filter_filter_ports_connected;
1196 break;
1197 case BT_COMPONENT_CLASS_TYPE_SINK:
1198 listeners =
1199 graph->listeners.filter_sink_ports_connected;
1200 break;
1201 default:
1202 abort();
1203 }
1204
1205 break;
1206 }
1207 default:
1208 abort();
1209 }
1210
1211 for (i = 0; i < listeners->len; i++) {
1212 struct bt_graph_listener_ports_connected *listener =
1213 &g_array_index(listeners,
1214 struct bt_graph_listener_ports_connected, i);
1215
1216 BT_ASSERT(listener->func);
1217 status = listener->func(upstream_comp, downstream_comp,
1218 upstream_port, downstream_port, listener->base.data);
1219 if (status != BT_FUNC_STATUS_OK) {
1220 goto end;
1221 }
1222 }
1223
1224 end:
1225 return status;
1226 }
1227
1228 BT_HIDDEN
1229 void bt_graph_remove_connection(struct bt_graph *graph,
1230 struct bt_connection *connection)
1231 {
1232 BT_ASSERT(graph);
1233 BT_ASSERT(connection);
1234 BT_LIB_LOGD("Removing graph's connection: %![graph-]+g, %![conn-]+x",
1235 graph, connection);
1236 g_ptr_array_remove(graph->connections, connection);
1237 }
1238
1239 static inline
1240 bool component_name_exists(struct bt_graph *graph, const char *name)
1241 {
1242 bool exists = false;
1243 uint64_t i;
1244
1245 for (i = 0; i < graph->components->len; i++) {
1246 struct bt_component *other_comp = graph->components->pdata[i];
1247
1248 if (strcmp(name, bt_component_get_name(other_comp)) == 0) {
1249 BT_ASSERT_PRE_MSG("Another component with the same name already exists in the graph: "
1250 "%![other-comp-]+c, name=\"%s\"",
1251 other_comp, name);
1252 exists = true;
1253 goto end;
1254 }
1255 }
1256
1257 end:
1258 return exists;
1259 }
1260
1261 static
1262 int add_component_with_init_method_data(
1263 struct bt_graph *graph,
1264 struct bt_component_class *comp_cls,
1265 comp_init_method_t init_method,
1266 const char *name, const struct bt_value *params,
1267 void *init_method_data, bt_logging_level log_level,
1268 struct bt_component **user_component)
1269 {
1270 int status = BT_FUNC_STATUS_OK;
1271 enum bt_component_class_init_method_status init_status;
1272 struct bt_component *component = NULL;
1273 int ret;
1274 bool init_can_consume;
1275 struct bt_value *new_params = NULL;
1276
1277 BT_ASSERT(comp_cls);
1278 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1279 BT_ASSERT_PRE_NON_NULL(name, "Name");
1280 BT_ASSERT_PRE(
1281 graph->config_state == BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
1282 "Graph is not in the \"configuring\" state: %!+g", graph);
1283 BT_ASSERT_PRE(!component_name_exists(graph, name),
1284 "Duplicate component name: %!+g, name=\"%s\"", graph, name);
1285 BT_ASSERT_PRE(!params || bt_value_is_map(params),
1286 "Parameter value is not a map value: %!+v", params);
1287 init_can_consume = graph->can_consume;
1288 bt_graph_set_can_consume(graph, false);
1289 BT_LIB_LOGI("Adding component to graph: "
1290 "%![graph-]+g, %![cc-]+C, name=\"%s\", log-level=%s, "
1291 "%![params-]+v, init-method-data-addr=%p",
1292 graph, comp_cls, name,
1293 bt_common_logging_level_string(log_level), params,
1294 init_method_data);
1295
1296 if (!params) {
1297 new_params = bt_value_map_create();
1298 if (!new_params) {
1299 BT_LIB_LOGE_APPEND_CAUSE(
1300 "Cannot create empty map value object.");
1301 status = BT_FUNC_STATUS_MEMORY_ERROR;
1302 goto end;
1303 }
1304
1305 params = new_params;
1306 }
1307
1308 ret = bt_component_create(comp_cls, name, log_level, &component);
1309 if (ret) {
1310 BT_LIB_LOGE_APPEND_CAUSE(
1311 "Cannot create empty component object: ret=%d",
1312 ret);
1313 status = BT_FUNC_STATUS_MEMORY_ERROR;
1314 goto end;
1315 }
1316
1317 /*
1318 * The user's initialization method needs to see that this
1319 * component is part of the graph. If the user method fails, we
1320 * immediately remove the component from the graph's components.
1321 */
1322 g_ptr_array_add(graph->components, component);
1323 bt_component_set_graph(component, graph);
1324 bt_value_freeze(params);
1325
1326 if (init_method) {
1327 BT_LOGD_STR("Calling user's initialization method.");
1328 init_status = init_method(component, params, init_method_data);
1329 BT_LOGD("User method returned: status=%s",
1330 bt_common_func_status_string(init_status));
1331 if (init_status != BT_FUNC_STATUS_OK) {
1332 if (init_status < 0) {
1333 BT_LIB_LOGW_APPEND_CAUSE(
1334 "Component initialization method failed: "
1335 "status=%s, %![comp-]+c",
1336 bt_common_func_status_string(init_status),
1337 component);
1338 }
1339
1340 status = init_status;
1341 bt_component_set_graph(component, NULL);
1342 g_ptr_array_remove_fast(graph->components, component);
1343 goto end;
1344 }
1345 }
1346
1347 /*
1348 * Mark the component as initialized so that its finalization
1349 * method is called when it is destroyed.
1350 */
1351 component->initialized = true;
1352
1353 /*
1354 * If it's a sink component, it needs to be part of the graph's
1355 * sink queue to be consumed by bt_graph_consume().
1356 */
1357 if (bt_component_is_sink(component)) {
1358 graph->has_sink = true;
1359 g_queue_push_tail(graph->sinks_to_consume, component);
1360 }
1361
1362 /*
1363 * Freeze the component class now that it's instantiated at
1364 * least once.
1365 */
1366 BT_LOGD_STR("Freezing component class.");
1367 bt_component_class_freeze(comp_cls);
1368 BT_LIB_LOGI("Added component to graph: "
1369 "%![graph-]+g, %![cc-]+C, name=\"%s\", log-level=%s, "
1370 "%![params-]+v, init-method-data-addr=%p, %![comp-]+c",
1371 graph, comp_cls, name,
1372 bt_common_logging_level_string(log_level), params,
1373 init_method_data, component);
1374
1375 if (user_component) {
1376 /* Move reference to user */
1377 *user_component = component;
1378 component = NULL;
1379 }
1380
1381 end:
1382 if (status != BT_FUNC_STATUS_OK) {
1383 bt_graph_make_faulty(graph);
1384 }
1385
1386 bt_object_put_ref(component);
1387 bt_object_put_ref(new_params);
1388 (void) init_can_consume;
1389 bt_graph_set_can_consume(graph, init_can_consume);
1390 return status;
1391 }
1392
1393 enum bt_graph_add_component_status
1394 bt_graph_add_source_component_with_init_method_data(
1395 struct bt_graph *graph,
1396 const struct bt_component_class_source *comp_cls,
1397 const char *name, const struct bt_value *params,
1398 void *init_method_data, bt_logging_level log_level,
1399 const struct bt_component_source **component)
1400 {
1401 BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class");
1402 return add_component_with_init_method_data(graph,
1403 (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init,
1404 name, params, init_method_data, log_level, (void *) component);
1405 }
1406
1407 enum bt_graph_add_component_status bt_graph_add_source_component(
1408 struct bt_graph *graph,
1409 const struct bt_component_class_source *comp_cls,
1410 const char *name, const struct bt_value *params,
1411 bt_logging_level log_level,
1412 const struct bt_component_source **component)
1413 {
1414 return bt_graph_add_source_component_with_init_method_data(
1415 graph, comp_cls, name, params, NULL, log_level, component);
1416 }
1417
1418 enum bt_graph_add_component_status
1419 bt_graph_add_filter_component_with_init_method_data(
1420 struct bt_graph *graph,
1421 const struct bt_component_class_filter *comp_cls,
1422 const char *name, const struct bt_value *params,
1423 void *init_method_data, bt_logging_level log_level,
1424 const struct bt_component_filter **component)
1425 {
1426 BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class");
1427 return add_component_with_init_method_data(graph,
1428 (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init,
1429 name, params, init_method_data, log_level, (void *) component);
1430 }
1431
1432 enum bt_graph_add_component_status bt_graph_add_filter_component(
1433 struct bt_graph *graph,
1434 const struct bt_component_class_filter *comp_cls,
1435 const char *name, const struct bt_value *params,
1436 bt_logging_level log_level,
1437 const struct bt_component_filter **component)
1438 {
1439 return bt_graph_add_filter_component_with_init_method_data(
1440 graph, comp_cls, name, params, NULL, log_level, component);
1441 }
1442
1443 enum bt_graph_add_component_status
1444 bt_graph_add_sink_component_with_init_method_data(
1445 struct bt_graph *graph,
1446 const struct bt_component_class_sink *comp_cls,
1447 const char *name, const struct bt_value *params,
1448 void *init_method_data, bt_logging_level log_level,
1449 const struct bt_component_sink **component)
1450 {
1451 BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class");
1452 return add_component_with_init_method_data(graph,
1453 (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init,
1454 name, params, init_method_data, log_level, (void *) component);
1455 }
1456
1457 enum bt_graph_add_component_status bt_graph_add_sink_component(
1458 struct bt_graph *graph,
1459 const struct bt_component_class_sink *comp_cls,
1460 const char *name, const struct bt_value *params,
1461 bt_logging_level log_level,
1462 const struct bt_component_sink **component)
1463 {
1464 return bt_graph_add_sink_component_with_init_method_data(
1465 graph, comp_cls, name, params, NULL, log_level, component);
1466 }
1467
1468 BT_HIDDEN
1469 int bt_graph_remove_unconnected_component(struct bt_graph *graph,
1470 struct bt_component *component)
1471 {
1472 bool init_can_consume;
1473 uint64_t count;
1474 uint64_t i;
1475 int ret = 0;
1476
1477 BT_ASSERT(graph);
1478 BT_ASSERT(component);
1479 BT_ASSERT(component->base.ref_count == 0);
1480 BT_ASSERT(bt_component_borrow_graph(component) == graph);
1481
1482 init_can_consume = graph->can_consume;
1483 count = bt_component_get_input_port_count(component);
1484
1485 for (i = 0; i < count; i++) {
1486 struct bt_port *port = (void *)
1487 bt_component_borrow_input_port_by_index(component, i);
1488
1489 BT_ASSERT(port);
1490
1491 if (bt_port_is_connected(port)) {
1492 BT_LIB_LOGW_APPEND_CAUSE(
1493 "Cannot remove component from graph: "
1494 "an input port is connected: "
1495 "%![graph-]+g, %![comp-]+c, %![port-]+p",
1496 graph, component, port);
1497 goto error;
1498 }
1499 }
1500
1501 count = bt_component_get_output_port_count(component);
1502
1503 for (i = 0; i < count; i++) {
1504 struct bt_port *port = (void *)
1505 bt_component_borrow_output_port_by_index(component, i);
1506
1507 BT_ASSERT(port);
1508
1509 if (bt_port_is_connected(port)) {
1510 BT_LIB_LOGW_APPEND_CAUSE(
1511 "Cannot remove component from graph: "
1512 "an output port is connected: "
1513 "%![graph-]+g, %![comp-]+c, %![port-]+p",
1514 graph, component, port);
1515 goto error;
1516 }
1517 }
1518
1519 bt_graph_set_can_consume(graph, false);
1520
1521 /* Possibly remove from sinks to consume */
1522 (void) g_queue_remove(graph->sinks_to_consume, component);
1523
1524 if (graph->sinks_to_consume->length == 0) {
1525 graph->has_sink = false;
1526 }
1527
1528 /*
1529 * This calls bt_object_try_spec_release() on the component, and
1530 * since its reference count is 0, its destructor is called. Its
1531 * destructor calls the user's finalization method (if set).
1532 */
1533 g_ptr_array_remove(graph->components, component);
1534 goto end;
1535
1536 error:
1537 ret = -1;
1538
1539 end:
1540 (void) init_can_consume;
1541 bt_graph_set_can_consume(graph, init_can_consume);
1542 return ret;
1543 }
1544
1545 BT_HIDDEN
1546 void bt_graph_add_message(struct bt_graph *graph,
1547 struct bt_message *msg)
1548 {
1549 BT_ASSERT(graph);
1550 BT_ASSERT(msg);
1551
1552 /*
1553 * It's okay not to take a reference because, when a
1554 * message's reference count drops to 0, either:
1555 *
1556 * * It is recycled back to one of this graph's pool.
1557 * * It is destroyed because it doesn't have any link to any
1558 * graph, which means the original graph is already destroyed.
1559 */
1560 g_ptr_array_add(graph->messages, msg);
1561 }
1562
1563 BT_HIDDEN
1564 bool bt_graph_is_interrupted(const struct bt_graph *graph)
1565 {
1566 BT_ASSERT(graph);
1567 return bt_interrupter_array_any_is_set(graph->interrupters);
1568 }
1569
1570 enum bt_graph_add_interrupter_status bt_graph_add_interrupter(
1571 struct bt_graph *graph, const struct bt_interrupter *intr)
1572 {
1573 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1574 BT_ASSERT_PRE_NON_NULL(intr, "Interrupter");
1575 g_ptr_array_add(graph->interrupters, (void *) intr);
1576 bt_object_get_no_null_check(intr);
1577 BT_LIB_LOGD("Added interrupter to graph: %![graph-]+g, %![intr-]+z",
1578 graph, intr);
1579 return BT_FUNC_STATUS_OK;
1580 }
1581
1582 void bt_graph_interrupt(struct bt_graph *graph)
1583 {
1584 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1585 bt_interrupter_set(graph->default_interrupter);
1586 BT_LIB_LOGI("Interrupted graph: %!+g", graph);
1587 }
1588
1589 void bt_graph_get_ref(const struct bt_graph *graph)
1590 {
1591 bt_object_get_ref(graph);
1592 }
1593
1594 void bt_graph_put_ref(const struct bt_graph *graph)
1595 {
1596 bt_object_put_ref(graph);
1597 }
This page took 0.100157 seconds and 4 git commands to generate.