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