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