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