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