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