| 1 | /* |
| 2 | * graph.c |
| 3 | * |
| 4 | * Babeltrace Plugin Component Graph |
| 5 | * |
| 6 | * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | * of this software and associated documentation files (the "Software"), to deal |
| 11 | * in the Software without restriction, including without limitation the rights |
| 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | * copies of the Software, and to permit persons to whom the Software is |
| 14 | * furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included in |
| 17 | * all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | * SOFTWARE. |
| 26 | */ |
| 27 | |
| 28 | #define BT_LOG_TAG "GRAPH" |
| 29 | #include <babeltrace/lib-logging-internal.h> |
| 30 | |
| 31 | #include <babeltrace/graph/component-internal.h> |
| 32 | #include <babeltrace/graph/graph-internal.h> |
| 33 | #include <babeltrace/graph/connection-internal.h> |
| 34 | #include <babeltrace/graph/component-sink-internal.h> |
| 35 | #include <babeltrace/graph/component-source.h> |
| 36 | #include <babeltrace/graph/component-filter.h> |
| 37 | #include <babeltrace/graph/port.h> |
| 38 | #include <babeltrace/graph/notification-internal.h> |
| 39 | #include <babeltrace/graph/notification-event-internal.h> |
| 40 | #include <babeltrace/graph/notification-packet-internal.h> |
| 41 | #include <babeltrace/compiler-internal.h> |
| 42 | #include <babeltrace/types.h> |
| 43 | #include <babeltrace/values.h> |
| 44 | #include <babeltrace/values-internal.h> |
| 45 | #include <babeltrace/assert-internal.h> |
| 46 | #include <babeltrace/assert-pre-internal.h> |
| 47 | #include <unistd.h> |
| 48 | #include <glib.h> |
| 49 | |
| 50 | struct bt_graph_listener { |
| 51 | void *func; |
| 52 | bt_graph_listener_removed removed; |
| 53 | void *data; |
| 54 | }; |
| 55 | |
| 56 | static |
| 57 | int init_listeners_array(GArray **listeners) |
| 58 | { |
| 59 | int ret = 0; |
| 60 | |
| 61 | BT_ASSERT(listeners); |
| 62 | *listeners = g_array_new(FALSE, TRUE, sizeof(struct bt_graph_listener)); |
| 63 | if (!*listeners) { |
| 64 | BT_LOGE_STR("Failed to allocate one GArray."); |
| 65 | ret = -1; |
| 66 | goto end; |
| 67 | } |
| 68 | |
| 69 | end: |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 73 | static |
| 74 | void call_remove_listeners(GArray *listeners) |
| 75 | { |
| 76 | size_t i; |
| 77 | |
| 78 | for (i = 0; i < listeners->len; i++) { |
| 79 | struct bt_graph_listener listener = |
| 80 | g_array_index(listeners, struct bt_graph_listener, i); |
| 81 | |
| 82 | if (listener.removed) { |
| 83 | listener.removed(listener.data); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | static |
| 89 | void bt_graph_destroy(struct bt_object *obj) |
| 90 | { |
| 91 | struct bt_graph *graph = container_of(obj, |
| 92 | struct bt_graph, base); |
| 93 | |
| 94 | /* |
| 95 | * The graph's reference count is 0 if we're here. Increment |
| 96 | * it to avoid a double-destroy (possibly infinitely recursive) |
| 97 | * in this situation: |
| 98 | * |
| 99 | * 1. We put and destroy a connection. |
| 100 | * 2. This connection's destructor finalizes its active |
| 101 | * notification iterators. |
| 102 | * 3. A notification iterator's finalization function gets a |
| 103 | * new reference on its component (reference count goes from |
| 104 | * 0 to 1). |
| 105 | * 4. Since this component's reference count goes to 1, it takes |
| 106 | * a reference on its parent (this graph). This graph's |
| 107 | * reference count goes from 0 to 1. |
| 108 | * 5. The notification iterator's finalization function puts its |
| 109 | * component reference (reference count goes from 1 to 0). |
| 110 | * 6. Since this component's reference count goes from 1 to 0, |
| 111 | * it puts its parent (this graph). This graph's reference |
| 112 | * count goes from 1 to 0. |
| 113 | * 7. Since this graph's reference count goes from 1 to 0, |
| 114 | * its destructor is called (this function). |
| 115 | * |
| 116 | * With the incrementation below, the graph's reference count at |
| 117 | * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This |
| 118 | * ensures that this function is not called two times. |
| 119 | */ |
| 120 | BT_LOGD("Destroying graph: addr=%p", graph); |
| 121 | obj->ref_count.count++; |
| 122 | |
| 123 | /* |
| 124 | * Cancel the graph to disallow some operations, like creating |
| 125 | * notification iterators and adding ports to components. |
| 126 | */ |
| 127 | (void) bt_graph_cancel(graph); |
| 128 | |
| 129 | /* Call all remove listeners */ |
| 130 | call_remove_listeners(graph->listeners.port_added); |
| 131 | call_remove_listeners(graph->listeners.port_removed); |
| 132 | call_remove_listeners(graph->listeners.ports_connected); |
| 133 | call_remove_listeners(graph->listeners.ports_disconnected); |
| 134 | |
| 135 | if (graph->notifications) { |
| 136 | g_ptr_array_free(graph->notifications, TRUE); |
| 137 | } |
| 138 | |
| 139 | if (graph->connections) { |
| 140 | BT_LOGD_STR("Destroying connections."); |
| 141 | g_ptr_array_free(graph->connections, TRUE); |
| 142 | } |
| 143 | |
| 144 | if (graph->components) { |
| 145 | BT_LOGD_STR("Destroying components."); |
| 146 | g_ptr_array_free(graph->components, TRUE); |
| 147 | } |
| 148 | |
| 149 | if (graph->sinks_to_consume) { |
| 150 | g_queue_free(graph->sinks_to_consume); |
| 151 | } |
| 152 | |
| 153 | if (graph->listeners.port_added) { |
| 154 | g_array_free(graph->listeners.port_added, TRUE); |
| 155 | } |
| 156 | |
| 157 | if (graph->listeners.port_removed) { |
| 158 | g_array_free(graph->listeners.port_removed, TRUE); |
| 159 | } |
| 160 | |
| 161 | if (graph->listeners.ports_connected) { |
| 162 | g_array_free(graph->listeners.ports_connected, TRUE); |
| 163 | } |
| 164 | |
| 165 | if (graph->listeners.ports_disconnected) { |
| 166 | g_array_free(graph->listeners.ports_disconnected, TRUE); |
| 167 | } |
| 168 | |
| 169 | bt_object_pool_finalize(&graph->event_notif_pool); |
| 170 | bt_object_pool_finalize(&graph->packet_begin_notif_pool); |
| 171 | bt_object_pool_finalize(&graph->packet_end_notif_pool); |
| 172 | g_free(graph); |
| 173 | } |
| 174 | |
| 175 | static |
| 176 | void destroy_notification_event(struct bt_notification *notif, |
| 177 | struct bt_graph *graph) |
| 178 | { |
| 179 | bt_notification_event_destroy(notif); |
| 180 | } |
| 181 | |
| 182 | static |
| 183 | void destroy_notification_packet_begin(struct bt_notification *notif, |
| 184 | struct bt_graph *graph) |
| 185 | { |
| 186 | bt_notification_packet_begin_destroy(notif); |
| 187 | } |
| 188 | |
| 189 | static |
| 190 | void destroy_notification_packet_end(struct bt_notification *notif, |
| 191 | struct bt_graph *graph) |
| 192 | { |
| 193 | bt_notification_packet_end_destroy(notif); |
| 194 | } |
| 195 | |
| 196 | static |
| 197 | void notify_notification_graph_is_destroyed(struct bt_notification *notif) |
| 198 | { |
| 199 | bt_notification_unlink_graph(notif); |
| 200 | } |
| 201 | |
| 202 | struct bt_graph *bt_graph_create(void) |
| 203 | { |
| 204 | struct bt_graph *graph; |
| 205 | int ret; |
| 206 | |
| 207 | BT_LOGD_STR("Creating graph object."); |
| 208 | graph = g_new0(struct bt_graph, 1); |
| 209 | if (!graph) { |
| 210 | BT_LOGE_STR("Failed to allocate one graph."); |
| 211 | goto end; |
| 212 | } |
| 213 | |
| 214 | bt_object_init(graph, bt_graph_destroy); |
| 215 | |
| 216 | graph->connections = g_ptr_array_new_with_free_func(bt_object_release); |
| 217 | if (!graph->connections) { |
| 218 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
| 219 | goto error; |
| 220 | } |
| 221 | graph->components = g_ptr_array_new_with_free_func(bt_object_release); |
| 222 | if (!graph->components) { |
| 223 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
| 224 | goto error; |
| 225 | } |
| 226 | graph->sinks_to_consume = g_queue_new(); |
| 227 | if (!graph->sinks_to_consume) { |
| 228 | BT_LOGE_STR("Failed to allocate one GQueue."); |
| 229 | goto error; |
| 230 | } |
| 231 | |
| 232 | graph->can_consume = BT_TRUE; |
| 233 | ret = init_listeners_array(&graph->listeners.port_added); |
| 234 | if (ret) { |
| 235 | BT_LOGE_STR("Cannot create the \"port added\" listener array."); |
| 236 | goto error; |
| 237 | } |
| 238 | |
| 239 | ret = init_listeners_array(&graph->listeners.port_removed); |
| 240 | if (ret) { |
| 241 | BT_LOGE_STR("Cannot create the \"port removed\" listener array."); |
| 242 | goto error; |
| 243 | } |
| 244 | |
| 245 | ret = init_listeners_array(&graph->listeners.ports_connected); |
| 246 | if (ret) { |
| 247 | BT_LOGE_STR("Cannot create the \"port connected\" listener array."); |
| 248 | goto error; |
| 249 | } |
| 250 | |
| 251 | ret = init_listeners_array(&graph->listeners.ports_disconnected); |
| 252 | if (ret) { |
| 253 | BT_LOGE_STR("Cannot create the \"port disconneted\" listener array."); |
| 254 | goto error; |
| 255 | } |
| 256 | |
| 257 | ret = bt_object_pool_initialize(&graph->event_notif_pool, |
| 258 | (bt_object_pool_new_object_func) bt_notification_event_new, |
| 259 | (bt_object_pool_destroy_object_func) destroy_notification_event, |
| 260 | graph); |
| 261 | if (ret) { |
| 262 | BT_LOGE("Failed to initialize event notification pool: ret=%d", |
| 263 | ret); |
| 264 | goto error; |
| 265 | } |
| 266 | |
| 267 | ret = bt_object_pool_initialize(&graph->packet_begin_notif_pool, |
| 268 | (bt_object_pool_new_object_func) bt_notification_packet_begin_new, |
| 269 | (bt_object_pool_destroy_object_func) destroy_notification_packet_begin, |
| 270 | graph); |
| 271 | if (ret) { |
| 272 | BT_LOGE("Failed to initialize packet beginning notification pool: ret=%d", |
| 273 | ret); |
| 274 | goto error; |
| 275 | } |
| 276 | |
| 277 | ret = bt_object_pool_initialize(&graph->packet_end_notif_pool, |
| 278 | (bt_object_pool_new_object_func) bt_notification_packet_end_new, |
| 279 | (bt_object_pool_destroy_object_func) destroy_notification_packet_end, |
| 280 | graph); |
| 281 | if (ret) { |
| 282 | BT_LOGE("Failed to initialize packet end notification pool: ret=%d", |
| 283 | ret); |
| 284 | goto error; |
| 285 | } |
| 286 | |
| 287 | graph->notifications = g_ptr_array_new_with_free_func( |
| 288 | (GDestroyNotify) notify_notification_graph_is_destroyed); |
| 289 | BT_LOGD("Created graph object: addr=%p", graph); |
| 290 | |
| 291 | end: |
| 292 | return graph; |
| 293 | error: |
| 294 | BT_PUT(graph); |
| 295 | goto end; |
| 296 | } |
| 297 | |
| 298 | enum bt_graph_status bt_graph_connect_ports(struct bt_graph *graph, |
| 299 | struct bt_port *upstream_port, struct bt_port *downstream_port, |
| 300 | struct bt_connection **user_connection) |
| 301 | { |
| 302 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
| 303 | struct bt_connection *connection = NULL; |
| 304 | struct bt_graph *upstream_graph = NULL; |
| 305 | struct bt_graph *downstream_graph = NULL; |
| 306 | struct bt_component *upstream_component = NULL; |
| 307 | struct bt_component *downstream_component = NULL; |
| 308 | enum bt_component_status component_status; |
| 309 | bt_bool init_can_consume; |
| 310 | |
| 311 | if (!graph) { |
| 312 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 313 | status = BT_GRAPH_STATUS_INVALID; |
| 314 | goto end; |
| 315 | } |
| 316 | init_can_consume = graph->can_consume; |
| 317 | |
| 318 | if (!upstream_port) { |
| 319 | BT_LOGW_STR("Invalid parameter: upstream port is NULL."); |
| 320 | status = BT_GRAPH_STATUS_INVALID; |
| 321 | goto end; |
| 322 | } |
| 323 | |
| 324 | if (!downstream_port) { |
| 325 | BT_LOGW_STR("Invalid parameter: downstream port is NULL."); |
| 326 | status = BT_GRAPH_STATUS_INVALID; |
| 327 | goto end; |
| 328 | } |
| 329 | |
| 330 | BT_LOGD("Connecting component ports within graph: " |
| 331 | "graph-addr=%p, " |
| 332 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
| 333 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", |
| 334 | graph, upstream_port, bt_port_get_name(upstream_port), |
| 335 | downstream_port, bt_port_get_name(downstream_port)); |
| 336 | |
| 337 | if (graph->canceled) { |
| 338 | BT_LOGW_STR("Invalid parameter: graph is canceled."); |
| 339 | status = BT_GRAPH_STATUS_CANCELED; |
| 340 | goto end; |
| 341 | } |
| 342 | |
| 343 | graph->can_consume = BT_FALSE; |
| 344 | |
| 345 | /* Ensure appropriate types for upstream and downstream ports. */ |
| 346 | if (bt_port_get_type(upstream_port) != BT_PORT_TYPE_OUTPUT) { |
| 347 | BT_LOGW_STR("Invalid parameter: upstream port is not an output port."); |
| 348 | status = BT_GRAPH_STATUS_INVALID; |
| 349 | goto end; |
| 350 | } |
| 351 | if (bt_port_get_type(downstream_port) != BT_PORT_TYPE_INPUT) { |
| 352 | BT_LOGW_STR("Invalid parameter: downstream port is not an input port."); |
| 353 | status = BT_GRAPH_STATUS_INVALID; |
| 354 | goto end; |
| 355 | } |
| 356 | |
| 357 | /* Ensure that both ports are currently unconnected. */ |
| 358 | if (bt_port_is_connected(upstream_port)) { |
| 359 | BT_LOGW_STR("Invalid parameter: upstream port is already connected."); |
| 360 | status = BT_GRAPH_STATUS_INVALID; |
| 361 | goto end; |
| 362 | } |
| 363 | |
| 364 | if (bt_port_is_connected(downstream_port)) { |
| 365 | BT_LOGW_STR("Invalid parameter: downstream port is already connected."); |
| 366 | status = BT_GRAPH_STATUS_INVALID; |
| 367 | goto end; |
| 368 | } |
| 369 | |
| 370 | /* |
| 371 | * Ensure that both ports are still attached to their creating |
| 372 | * component. |
| 373 | */ |
| 374 | upstream_component = bt_port_get_component(upstream_port); |
| 375 | if (!upstream_component) { |
| 376 | BT_LOGW_STR("Invalid parameter: upstream port is loose (does not belong to a component)"); |
| 377 | status = BT_GRAPH_STATUS_INVALID; |
| 378 | goto end; |
| 379 | } |
| 380 | |
| 381 | downstream_component = bt_port_get_component(downstream_port); |
| 382 | if (!downstream_component) { |
| 383 | BT_LOGW_STR("Invalid parameter: downstream port is loose (does not belong to a component)"); |
| 384 | status = BT_GRAPH_STATUS_INVALID; |
| 385 | goto end; |
| 386 | } |
| 387 | |
| 388 | BT_LOGD("Connecting component ports: " |
| 389 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " |
| 390 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\"", |
| 391 | upstream_component, bt_component_get_name(upstream_component), |
| 392 | downstream_component, bt_component_get_name(downstream_component)); |
| 393 | |
| 394 | /* |
| 395 | * At this point the ports are not connected yet. Both |
| 396 | * components need to accept an eventual connection to their |
| 397 | * port by the other port before we continue. |
| 398 | */ |
| 399 | BT_LOGD_STR("Asking upstream component to accept the connection."); |
| 400 | component_status = bt_component_accept_port_connection( |
| 401 | upstream_component, upstream_port, downstream_port); |
| 402 | if (component_status != BT_COMPONENT_STATUS_OK) { |
| 403 | if (component_status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) { |
| 404 | BT_LOGD_STR("Upstream component refused the connection."); |
| 405 | } else { |
| 406 | BT_LOGW("Cannot ask upstream component to accept the connection: " |
| 407 | "status=%s", bt_component_status_string(component_status)); |
| 408 | } |
| 409 | |
| 410 | status = bt_graph_status_from_component_status( |
| 411 | component_status); |
| 412 | goto end; |
| 413 | } |
| 414 | |
| 415 | BT_LOGD_STR("Asking downstream component to accept the connection."); |
| 416 | component_status = bt_component_accept_port_connection( |
| 417 | downstream_component, downstream_port, upstream_port); |
| 418 | if (component_status != BT_COMPONENT_STATUS_OK) { |
| 419 | if (component_status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) { |
| 420 | BT_LOGD_STR("Downstream component refused the connection."); |
| 421 | } else { |
| 422 | BT_LOGW("Cannot ask downstream component to accept the connection: " |
| 423 | "status=%s", bt_component_status_string(component_status)); |
| 424 | } |
| 425 | |
| 426 | status = bt_graph_status_from_component_status( |
| 427 | component_status); |
| 428 | goto end; |
| 429 | } |
| 430 | |
| 431 | BT_LOGD_STR("Creating connection."); |
| 432 | connection = bt_connection_create(graph, upstream_port, |
| 433 | downstream_port); |
| 434 | if (!connection) { |
| 435 | BT_LOGW("Cannot create connection object."); |
| 436 | status = BT_GRAPH_STATUS_NOMEM; |
| 437 | goto end; |
| 438 | } |
| 439 | |
| 440 | BT_LOGD("Connection object created: conn-addr=%p", connection); |
| 441 | |
| 442 | /* |
| 443 | * Ownership of upstream_component/downstream_component and of |
| 444 | * the connection object is transferred to the graph. |
| 445 | */ |
| 446 | g_ptr_array_add(graph->connections, connection); |
| 447 | |
| 448 | /* |
| 449 | * Notify both components that their port is connected. |
| 450 | */ |
| 451 | BT_LOGD_STR("Notifying upstream component that its port is connected."); |
| 452 | bt_component_port_connected(upstream_component, upstream_port, |
| 453 | downstream_port); |
| 454 | BT_LOGD_STR("Notifying downstream component that its port is connected."); |
| 455 | bt_component_port_connected(downstream_component, downstream_port, |
| 456 | upstream_port); |
| 457 | |
| 458 | /* |
| 459 | * Notify the graph's creator that both ports are connected. |
| 460 | */ |
| 461 | BT_LOGD_STR("Notifying graph's user that new component ports are connected."); |
| 462 | bt_graph_notify_ports_connected(graph, upstream_port, downstream_port); |
| 463 | BT_LOGD("Connected component ports within graph: " |
| 464 | "graph-addr=%p, " |
| 465 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " |
| 466 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\", " |
| 467 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
| 468 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", |
| 469 | graph, |
| 470 | upstream_component, bt_component_get_name(upstream_component), |
| 471 | downstream_component, bt_component_get_name(downstream_component), |
| 472 | upstream_port, bt_port_get_name(upstream_port), |
| 473 | downstream_port, bt_port_get_name(downstream_port)); |
| 474 | |
| 475 | if (user_connection) { |
| 476 | /* Move reference to user */ |
| 477 | *user_connection = connection; |
| 478 | connection = NULL; |
| 479 | } |
| 480 | |
| 481 | end: |
| 482 | bt_put(upstream_graph); |
| 483 | bt_put(downstream_graph); |
| 484 | bt_put(upstream_component); |
| 485 | bt_put(downstream_component); |
| 486 | bt_put(connection); |
| 487 | if (graph) { |
| 488 | graph->can_consume = init_can_consume; |
| 489 | } |
| 490 | return status; |
| 491 | } |
| 492 | |
| 493 | static |
| 494 | enum bt_graph_status consume_graph_sink(struct bt_component *sink) |
| 495 | { |
| 496 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
| 497 | enum bt_component_status comp_status; |
| 498 | |
| 499 | BT_ASSERT(sink); |
| 500 | comp_status = bt_component_sink_consume(sink); |
| 501 | BT_LOGV("Consumed from sink: addr=%p, name=\"%s\", status=%s", |
| 502 | sink, bt_component_get_name(sink), |
| 503 | bt_component_status_string(comp_status)); |
| 504 | |
| 505 | switch (comp_status) { |
| 506 | case BT_COMPONENT_STATUS_OK: |
| 507 | break; |
| 508 | case BT_COMPONENT_STATUS_END: |
| 509 | status = BT_GRAPH_STATUS_END; |
| 510 | break; |
| 511 | case BT_COMPONENT_STATUS_AGAIN: |
| 512 | status = BT_GRAPH_STATUS_AGAIN; |
| 513 | break; |
| 514 | case BT_COMPONENT_STATUS_INVALID: |
| 515 | status = BT_GRAPH_STATUS_INVALID; |
| 516 | break; |
| 517 | default: |
| 518 | status = BT_GRAPH_STATUS_ERROR; |
| 519 | break; |
| 520 | } |
| 521 | |
| 522 | return status; |
| 523 | } |
| 524 | |
| 525 | /* |
| 526 | * `node` is removed from the queue of sinks to consume when passed to |
| 527 | * this function. This function adds it back to the queue if there's |
| 528 | * still something to consume afterwards. |
| 529 | */ |
| 530 | static |
| 531 | enum bt_graph_status consume_sink_node(struct bt_graph *graph, |
| 532 | GList *node) |
| 533 | { |
| 534 | enum bt_graph_status status; |
| 535 | struct bt_component *sink; |
| 536 | |
| 537 | sink = node->data; |
| 538 | status = consume_graph_sink(sink); |
| 539 | if (status != BT_GRAPH_STATUS_END) { |
| 540 | g_queue_push_tail_link(graph->sinks_to_consume, node); |
| 541 | goto end; |
| 542 | } |
| 543 | |
| 544 | /* End reached, the node is not added back to the queue and free'd. */ |
| 545 | g_queue_delete_link(graph->sinks_to_consume, node); |
| 546 | |
| 547 | /* Don't forward an END status if there are sinks left to consume. */ |
| 548 | if (!g_queue_is_empty(graph->sinks_to_consume)) { |
| 549 | status = BT_GRAPH_STATUS_OK; |
| 550 | goto end; |
| 551 | } |
| 552 | |
| 553 | end: |
| 554 | BT_LOGV("Consumed sink node: status=%s", bt_graph_status_string(status)); |
| 555 | return status; |
| 556 | } |
| 557 | |
| 558 | BT_HIDDEN |
| 559 | enum bt_graph_status bt_graph_consume_sink_no_check(struct bt_graph *graph, |
| 560 | struct bt_component *sink) |
| 561 | { |
| 562 | enum bt_graph_status status; |
| 563 | GList *sink_node; |
| 564 | int index; |
| 565 | |
| 566 | BT_LOGV("Making specific sink consume: addr=%p, " |
| 567 | "comp-addr=%p, comp-name=\"%s\"", |
| 568 | graph, sink, bt_component_get_name(sink)); |
| 569 | |
| 570 | BT_ASSERT(bt_component_borrow_graph(sink) == graph); |
| 571 | |
| 572 | if (g_queue_is_empty(graph->sinks_to_consume)) { |
| 573 | BT_LOGV_STR("Graph's sink queue is empty: end of graph."); |
| 574 | status = BT_GRAPH_STATUS_END; |
| 575 | goto end; |
| 576 | } |
| 577 | |
| 578 | index = g_queue_index(graph->sinks_to_consume, sink); |
| 579 | if (index < 0) { |
| 580 | BT_LOGV_STR("Sink is not marked as consumable: sink is ended."); |
| 581 | status = BT_GRAPH_STATUS_END; |
| 582 | goto end; |
| 583 | } |
| 584 | |
| 585 | sink_node = g_queue_pop_nth_link(graph->sinks_to_consume, index); |
| 586 | BT_ASSERT(sink_node); |
| 587 | status = consume_sink_node(graph, sink_node); |
| 588 | |
| 589 | end: |
| 590 | return status; |
| 591 | } |
| 592 | |
| 593 | BT_HIDDEN |
| 594 | enum bt_graph_status bt_graph_consume_no_check(struct bt_graph *graph) |
| 595 | { |
| 596 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
| 597 | struct bt_component *sink; |
| 598 | GList *current_node; |
| 599 | |
| 600 | BT_LOGV("Making next sink consume: addr=%p", graph); |
| 601 | BT_ASSERT_PRE(graph->has_sink, |
| 602 | "Graph has no sink component: %!+g", graph); |
| 603 | |
| 604 | if (g_queue_is_empty(graph->sinks_to_consume)) { |
| 605 | BT_LOGV_STR("Graph's sink queue is empty: end of graph."); |
| 606 | status = BT_GRAPH_STATUS_END; |
| 607 | goto end; |
| 608 | } |
| 609 | |
| 610 | current_node = g_queue_pop_head_link(graph->sinks_to_consume); |
| 611 | sink = current_node->data; |
| 612 | BT_LOGV("Chose next sink to consume: comp-addr=%p, comp-name=\"%s\"", |
| 613 | sink, bt_component_get_name(sink)); |
| 614 | status = consume_sink_node(graph, current_node); |
| 615 | |
| 616 | end: |
| 617 | return status; |
| 618 | } |
| 619 | |
| 620 | enum bt_graph_status bt_graph_consume(struct bt_graph *graph) |
| 621 | { |
| 622 | enum bt_graph_status status; |
| 623 | |
| 624 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 625 | BT_ASSERT_PRE(!graph->canceled, "Graph is canceled: %!+g", graph); |
| 626 | BT_ASSERT_PRE(graph->can_consume, |
| 627 | "Cannot consume graph in its current state: %!+g", graph); |
| 628 | graph->can_consume = BT_FALSE; |
| 629 | status = bt_graph_consume_no_check(graph); |
| 630 | graph->can_consume = BT_TRUE; |
| 631 | return BT_GRAPH_STATUS_OK; |
| 632 | } |
| 633 | |
| 634 | enum bt_graph_status bt_graph_run(struct bt_graph *graph) |
| 635 | { |
| 636 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
| 637 | |
| 638 | if (!graph) { |
| 639 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 640 | status = BT_GRAPH_STATUS_INVALID; |
| 641 | goto end; |
| 642 | } |
| 643 | |
| 644 | if (graph->canceled) { |
| 645 | BT_LOGW("Invalid parameter: graph is canceled: " |
| 646 | "graph-addr=%p", graph); |
| 647 | status = BT_GRAPH_STATUS_CANCELED; |
| 648 | goto end; |
| 649 | } |
| 650 | |
| 651 | if (!graph->can_consume) { |
| 652 | BT_LOGW_STR("Cannot run graph in its current state."); |
| 653 | status = BT_GRAPH_STATUS_CANNOT_CONSUME; |
| 654 | goto end; |
| 655 | } |
| 656 | |
| 657 | graph->can_consume = BT_FALSE; |
| 658 | BT_LOGV("Running graph: addr=%p", graph); |
| 659 | |
| 660 | do { |
| 661 | /* |
| 662 | * Check if the graph is canceled at each iteration. If |
| 663 | * the graph was canceled by another thread or by a |
| 664 | * signal, this is not a warning nor an error, it was |
| 665 | * intentional: log with a DEBUG level only. |
| 666 | */ |
| 667 | if (graph->canceled) { |
| 668 | BT_LOGD("Stopping the graph: graph is canceled: " |
| 669 | "graph-addr=%p", graph); |
| 670 | status = BT_GRAPH_STATUS_CANCELED; |
| 671 | goto end; |
| 672 | } |
| 673 | |
| 674 | status = bt_graph_consume_no_check(graph); |
| 675 | if (status == BT_GRAPH_STATUS_AGAIN) { |
| 676 | /* |
| 677 | * If AGAIN is received and there are multiple |
| 678 | * sinks, go ahead and consume from the next |
| 679 | * sink. |
| 680 | * |
| 681 | * However, in the case where a single sink is |
| 682 | * left, the caller can decide to busy-wait and |
| 683 | * call bt_graph_run() continuously until the |
| 684 | * source is ready or it can decide to sleep for |
| 685 | * an arbitrary amount of time. |
| 686 | */ |
| 687 | if (graph->sinks_to_consume->length > 1) { |
| 688 | status = BT_GRAPH_STATUS_OK; |
| 689 | } |
| 690 | } else if (status == BT_GRAPH_STATUS_NO_SINK) { |
| 691 | goto end; |
| 692 | } |
| 693 | } while (status == BT_GRAPH_STATUS_OK); |
| 694 | |
| 695 | if (g_queue_is_empty(graph->sinks_to_consume)) { |
| 696 | status = BT_GRAPH_STATUS_END; |
| 697 | } |
| 698 | |
| 699 | end: |
| 700 | BT_LOGV("Graph ran: status=%s", bt_graph_status_string(status)); |
| 701 | if (graph) { |
| 702 | graph->can_consume = BT_TRUE; |
| 703 | } |
| 704 | return status; |
| 705 | } |
| 706 | |
| 707 | static |
| 708 | int add_listener(GArray *listeners, void *func, void *removed, void *data) |
| 709 | { |
| 710 | struct bt_graph_listener listener = { |
| 711 | .func = func, |
| 712 | .removed = removed, |
| 713 | .data = data, |
| 714 | }; |
| 715 | |
| 716 | g_array_append_val(listeners, listener); |
| 717 | return listeners->len - 1; |
| 718 | } |
| 719 | |
| 720 | int bt_graph_add_port_added_listener( |
| 721 | struct bt_graph *graph, |
| 722 | bt_graph_port_added_listener listener, |
| 723 | bt_graph_listener_removed listener_removed, void *data) |
| 724 | { |
| 725 | int ret; |
| 726 | |
| 727 | if (!graph) { |
| 728 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 729 | ret = -1; |
| 730 | goto end; |
| 731 | } |
| 732 | |
| 733 | if (graph->in_remove_listener) { |
| 734 | BT_LOGW("Cannot call this function during the execution of a remove listener: " |
| 735 | "addr=%p", graph); |
| 736 | ret = -1; |
| 737 | goto end; |
| 738 | } |
| 739 | |
| 740 | if (!listener) { |
| 741 | BT_LOGW_STR("Invalid parameter: listener is NULL."); |
| 742 | ret = -1; |
| 743 | goto end; |
| 744 | } |
| 745 | |
| 746 | ret = add_listener(graph->listeners.port_added, listener, |
| 747 | listener_removed, data); |
| 748 | BT_LOGV("Added \"port added\" listener to graph: " |
| 749 | "graph-addr=%p, listener-addr=%p, pos=%d", |
| 750 | graph, listener, ret); |
| 751 | |
| 752 | end: |
| 753 | return ret; |
| 754 | } |
| 755 | |
| 756 | int bt_graph_add_port_removed_listener( |
| 757 | struct bt_graph *graph, |
| 758 | bt_graph_port_removed_listener listener, |
| 759 | bt_graph_listener_removed listener_removed, void *data) |
| 760 | { |
| 761 | int ret; |
| 762 | |
| 763 | if (!graph) { |
| 764 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 765 | ret = -1; |
| 766 | goto end; |
| 767 | } |
| 768 | |
| 769 | if (graph->in_remove_listener) { |
| 770 | BT_LOGW("Cannot call this function during the execution of a remove listener: " |
| 771 | "addr=%p", graph); |
| 772 | ret = -1; |
| 773 | goto end; |
| 774 | } |
| 775 | |
| 776 | if (!listener) { |
| 777 | BT_LOGW_STR("Invalid parameter: listener is NULL."); |
| 778 | ret = -1; |
| 779 | goto end; |
| 780 | } |
| 781 | |
| 782 | ret = add_listener(graph->listeners.port_removed, listener, |
| 783 | listener_removed, data); |
| 784 | BT_LOGV("Added \"port removed\" listener to graph: " |
| 785 | "graph-addr=%p, listener-addr=%p, pos=%d", |
| 786 | graph, listener, ret); |
| 787 | |
| 788 | end: |
| 789 | return ret; |
| 790 | } |
| 791 | |
| 792 | int bt_graph_add_ports_connected_listener( |
| 793 | struct bt_graph *graph, |
| 794 | bt_graph_ports_connected_listener listener, |
| 795 | bt_graph_listener_removed listener_removed, void *data) |
| 796 | { |
| 797 | int ret; |
| 798 | |
| 799 | if (!graph) { |
| 800 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 801 | ret = -1; |
| 802 | goto end; |
| 803 | } |
| 804 | |
| 805 | if (graph->in_remove_listener) { |
| 806 | BT_LOGW("Cannot call this function during the execution of a remove listener: " |
| 807 | "addr=%p", graph); |
| 808 | ret = -1; |
| 809 | goto end; |
| 810 | } |
| 811 | |
| 812 | if (!listener) { |
| 813 | BT_LOGW_STR("Invalid parameter: listener is NULL."); |
| 814 | ret = -1; |
| 815 | goto end; |
| 816 | } |
| 817 | |
| 818 | ret = add_listener(graph->listeners.ports_connected, listener, |
| 819 | listener_removed, data); |
| 820 | BT_LOGV("Added \"port connected\" listener to graph: " |
| 821 | "graph-addr=%p, listener-addr=%p, pos=%d", |
| 822 | graph, listener, ret); |
| 823 | |
| 824 | end: |
| 825 | return ret; |
| 826 | } |
| 827 | |
| 828 | int bt_graph_add_ports_disconnected_listener( |
| 829 | struct bt_graph *graph, |
| 830 | bt_graph_ports_disconnected_listener listener, |
| 831 | bt_graph_listener_removed listener_removed, void *data) |
| 832 | { |
| 833 | int ret; |
| 834 | |
| 835 | if (!graph) { |
| 836 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 837 | ret = -1; |
| 838 | goto end; |
| 839 | } |
| 840 | |
| 841 | if (graph->in_remove_listener) { |
| 842 | BT_LOGW("Cannot call this function during the execution of a remove listener: " |
| 843 | "addr=%p", graph); |
| 844 | ret = -1; |
| 845 | goto end; |
| 846 | } |
| 847 | |
| 848 | if (!listener) { |
| 849 | BT_LOGW_STR("Invalid parameter: listener is NULL."); |
| 850 | ret = -1; |
| 851 | goto end; |
| 852 | } |
| 853 | |
| 854 | ret = add_listener(graph->listeners.ports_disconnected, listener, |
| 855 | listener_removed, data); |
| 856 | BT_LOGV("Added \"port disconnected\" listener to graph: " |
| 857 | "graph-addr=%p, listener-addr=%p, pos=%d", |
| 858 | graph, listener, ret); |
| 859 | |
| 860 | end: |
| 861 | return ret; |
| 862 | } |
| 863 | |
| 864 | BT_HIDDEN |
| 865 | void bt_graph_notify_port_added(struct bt_graph *graph, struct bt_port *port) |
| 866 | { |
| 867 | size_t i; |
| 868 | |
| 869 | BT_LOGV("Notifying graph listeners that a port was added: " |
| 870 | "graph-addr=%p, port-addr=%p, port-name=\"%s\"", |
| 871 | graph, port, bt_port_get_name(port)); |
| 872 | |
| 873 | for (i = 0; i < graph->listeners.port_added->len; i++) { |
| 874 | struct bt_graph_listener listener = |
| 875 | g_array_index(graph->listeners.port_added, |
| 876 | struct bt_graph_listener, i); |
| 877 | bt_graph_port_added_listener func = listener.func; |
| 878 | |
| 879 | BT_ASSERT(func); |
| 880 | func(port, listener.data); |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | BT_HIDDEN |
| 885 | void bt_graph_notify_port_removed(struct bt_graph *graph, |
| 886 | struct bt_component *comp, struct bt_port *port) |
| 887 | { |
| 888 | size_t i; |
| 889 | |
| 890 | BT_LOGV("Notifying graph listeners that a port was removed: " |
| 891 | "graph-addr=%p, port-addr=%p, port-name=\"%s\"", |
| 892 | graph, port, bt_port_get_name(port)); |
| 893 | |
| 894 | for (i = 0; i < graph->listeners.port_removed->len; i++) { |
| 895 | struct bt_graph_listener listener = |
| 896 | g_array_index(graph->listeners.port_removed, |
| 897 | struct bt_graph_listener, i); |
| 898 | bt_graph_port_removed_listener func = listener.func; |
| 899 | |
| 900 | BT_ASSERT(func); |
| 901 | func(comp, port, listener.data); |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | BT_HIDDEN |
| 906 | void bt_graph_notify_ports_connected(struct bt_graph *graph, |
| 907 | struct bt_port *upstream_port, struct bt_port *downstream_port) |
| 908 | { |
| 909 | size_t i; |
| 910 | |
| 911 | BT_LOGV("Notifying graph listeners that two ports were connected: " |
| 912 | "graph-addr=%p, " |
| 913 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
| 914 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", |
| 915 | graph, upstream_port, bt_port_get_name(upstream_port), |
| 916 | downstream_port, bt_port_get_name(downstream_port)); |
| 917 | |
| 918 | for (i = 0; i < graph->listeners.ports_connected->len; i++) { |
| 919 | struct bt_graph_listener listener = |
| 920 | g_array_index(graph->listeners.ports_connected, |
| 921 | struct bt_graph_listener, i); |
| 922 | bt_graph_ports_connected_listener func = listener.func; |
| 923 | |
| 924 | BT_ASSERT(func); |
| 925 | func(upstream_port, downstream_port, listener.data); |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | BT_HIDDEN |
| 930 | void bt_graph_notify_ports_disconnected(struct bt_graph *graph, |
| 931 | struct bt_component *upstream_comp, |
| 932 | struct bt_component *downstream_comp, |
| 933 | struct bt_port *upstream_port, struct bt_port *downstream_port) |
| 934 | { |
| 935 | size_t i; |
| 936 | |
| 937 | BT_LOGV("Notifying graph listeners that two ports were disconnected: " |
| 938 | "graph-addr=%p, " |
| 939 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
| 940 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", |
| 941 | graph, upstream_port, bt_port_get_name(upstream_port), |
| 942 | downstream_port, bt_port_get_name(downstream_port)); |
| 943 | |
| 944 | for (i = 0; i < graph->listeners.ports_disconnected->len; i++) { |
| 945 | struct bt_graph_listener listener = |
| 946 | g_array_index(graph->listeners.ports_disconnected, |
| 947 | struct bt_graph_listener, i); |
| 948 | bt_graph_ports_disconnected_listener func = listener.func; |
| 949 | |
| 950 | BT_ASSERT(func); |
| 951 | func(upstream_comp, downstream_comp, upstream_port, |
| 952 | downstream_port, listener.data); |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | enum bt_graph_status bt_graph_cancel(struct bt_graph *graph) |
| 957 | { |
| 958 | enum bt_graph_status ret = BT_GRAPH_STATUS_OK; |
| 959 | |
| 960 | if (!graph) { |
| 961 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 962 | ret = BT_GRAPH_STATUS_INVALID; |
| 963 | goto end; |
| 964 | } |
| 965 | |
| 966 | graph->canceled = BT_TRUE; |
| 967 | BT_LOGV("Canceled graph: addr=%p", graph); |
| 968 | |
| 969 | end: |
| 970 | return ret; |
| 971 | } |
| 972 | |
| 973 | bt_bool bt_graph_is_canceled(struct bt_graph *graph) |
| 974 | { |
| 975 | bt_bool canceled = BT_FALSE; |
| 976 | |
| 977 | if (!graph) { |
| 978 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 979 | goto end; |
| 980 | } |
| 981 | |
| 982 | canceled = graph->canceled; |
| 983 | |
| 984 | end: |
| 985 | return canceled; |
| 986 | } |
| 987 | |
| 988 | BT_HIDDEN |
| 989 | void bt_graph_remove_connection(struct bt_graph *graph, |
| 990 | struct bt_connection *connection) |
| 991 | { |
| 992 | BT_ASSERT(graph); |
| 993 | BT_ASSERT(connection); |
| 994 | BT_LOGV("Removing graph's connection: graph-addr=%p, conn-addr=%p", |
| 995 | graph, connection); |
| 996 | g_ptr_array_remove(graph->connections, connection); |
| 997 | } |
| 998 | |
| 999 | enum bt_graph_status bt_graph_add_component_with_init_method_data( |
| 1000 | struct bt_graph *graph, |
| 1001 | struct bt_component_class *component_class, |
| 1002 | const char *name, struct bt_value *params, |
| 1003 | void *init_method_data, |
| 1004 | struct bt_component **user_component) |
| 1005 | { |
| 1006 | enum bt_graph_status graph_status = BT_GRAPH_STATUS_OK; |
| 1007 | enum bt_component_status comp_status; |
| 1008 | struct bt_component *component = NULL; |
| 1009 | enum bt_component_class_type type; |
| 1010 | size_t i; |
| 1011 | bt_bool init_can_consume; |
| 1012 | |
| 1013 | bt_get(params); |
| 1014 | |
| 1015 | if (!graph) { |
| 1016 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 1017 | graph_status = BT_GRAPH_STATUS_INVALID; |
| 1018 | goto end; |
| 1019 | } |
| 1020 | init_can_consume = graph->can_consume; |
| 1021 | |
| 1022 | if (!component_class) { |
| 1023 | BT_LOGW_STR("Invalid parameter: component class is NULL."); |
| 1024 | graph_status = BT_GRAPH_STATUS_INVALID; |
| 1025 | goto end; |
| 1026 | } |
| 1027 | |
| 1028 | graph->can_consume = BT_FALSE; |
| 1029 | type = bt_component_class_get_type(component_class); |
| 1030 | BT_LOGD("Adding component to graph: " |
| 1031 | "graph-addr=%p, comp-cls-addr=%p, " |
| 1032 | "comp-cls-type=%s, name=\"%s\", params-addr=%p, " |
| 1033 | "init-method-data-addr=%p", |
| 1034 | graph, component_class, bt_component_class_type_string(type), |
| 1035 | name, params, init_method_data); |
| 1036 | |
| 1037 | if (!name) { |
| 1038 | BT_LOGW_STR("Invalid parameter: name is NULL."); |
| 1039 | graph_status = BT_GRAPH_STATUS_INVALID; |
| 1040 | goto end; |
| 1041 | } |
| 1042 | |
| 1043 | if (graph->canceled) { |
| 1044 | BT_LOGW_STR("Invalid parameter: graph is canceled."); |
| 1045 | graph_status = BT_GRAPH_STATUS_CANCELED; |
| 1046 | goto end; |
| 1047 | } |
| 1048 | |
| 1049 | if (type != BT_COMPONENT_CLASS_TYPE_SOURCE && |
| 1050 | type != BT_COMPONENT_CLASS_TYPE_FILTER && |
| 1051 | type != BT_COMPONENT_CLASS_TYPE_SINK) { |
| 1052 | BT_LOGW("Invalid parameter: unknown component class type: " |
| 1053 | "type=%d", type); |
| 1054 | graph_status = BT_GRAPH_STATUS_INVALID; |
| 1055 | goto end; |
| 1056 | } |
| 1057 | |
| 1058 | for (i = 0; i < graph->components->len; i++) { |
| 1059 | void *other_comp = graph->components->pdata[i]; |
| 1060 | |
| 1061 | if (strcmp(name, bt_component_get_name(other_comp)) == 0) { |
| 1062 | BT_LOGW("Invalid parameter: another component with the same name already exists in the graph: " |
| 1063 | "other-comp-addr=%p, name=\"%s\"", |
| 1064 | other_comp, name); |
| 1065 | graph_status = BT_GRAPH_STATUS_INVALID; |
| 1066 | goto end; |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | /* |
| 1071 | * Parameters must be a map value, but we create a convenient |
| 1072 | * empty one if it's NULL. |
| 1073 | */ |
| 1074 | if (params) { |
| 1075 | if (!bt_value_is_map(params)) { |
| 1076 | BT_LOGW("Invalid parameter: initialization parameters must be a map value: " |
| 1077 | "type=%s", |
| 1078 | bt_value_type_string(bt_value_get_type(params))); |
| 1079 | graph_status = BT_GRAPH_STATUS_INVALID; |
| 1080 | goto end; |
| 1081 | } |
| 1082 | } else { |
| 1083 | params = bt_value_map_create(); |
| 1084 | if (!params) { |
| 1085 | BT_LOGE_STR("Cannot create map value object."); |
| 1086 | graph_status = BT_GRAPH_STATUS_NOMEM; |
| 1087 | goto end; |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | comp_status = bt_component_create(component_class, name, &component); |
| 1092 | if (comp_status != BT_COMPONENT_STATUS_OK) { |
| 1093 | BT_LOGE("Cannot create empty component object: status=%s", |
| 1094 | bt_component_status_string(comp_status)); |
| 1095 | graph_status = bt_graph_status_from_component_status( |
| 1096 | comp_status); |
| 1097 | goto end; |
| 1098 | } |
| 1099 | |
| 1100 | /* |
| 1101 | * The user's initialization method needs to see that this |
| 1102 | * component is part of the graph. If the user method fails, we |
| 1103 | * immediately remove the component from the graph's components. |
| 1104 | */ |
| 1105 | g_ptr_array_add(graph->components, component); |
| 1106 | bt_component_set_graph(component, graph); |
| 1107 | |
| 1108 | if (component_class->methods.init) { |
| 1109 | BT_LOGD_STR("Calling user's initialization method."); |
| 1110 | comp_status = component_class->methods.init( |
| 1111 | bt_private_component_from_component(component), params, |
| 1112 | init_method_data); |
| 1113 | BT_LOGD("User method returned: status=%s", |
| 1114 | bt_component_status_string(comp_status)); |
| 1115 | if (comp_status != BT_COMPONENT_STATUS_OK) { |
| 1116 | BT_LOGW_STR("Initialization method failed."); |
| 1117 | graph_status = bt_graph_status_from_component_status( |
| 1118 | comp_status); |
| 1119 | bt_component_set_graph(component, NULL); |
| 1120 | g_ptr_array_remove_fast(graph->components, component); |
| 1121 | goto end; |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | /* |
| 1126 | * Mark the component as initialized so that its finalization |
| 1127 | * method is called when it is destroyed. |
| 1128 | */ |
| 1129 | component->initialized = true; |
| 1130 | |
| 1131 | /* |
| 1132 | * If it's a sink component, it needs to be part of the graph's |
| 1133 | * sink queue to be consumed by bt_graph_consume(). |
| 1134 | */ |
| 1135 | if (bt_component_is_sink(component)) { |
| 1136 | graph->has_sink = BT_TRUE; |
| 1137 | g_queue_push_tail(graph->sinks_to_consume, component); |
| 1138 | } |
| 1139 | |
| 1140 | /* |
| 1141 | * Freeze the component class now that it's instantiated at |
| 1142 | * least once. |
| 1143 | */ |
| 1144 | BT_LOGD_STR("Freezing component class."); |
| 1145 | bt_component_class_freeze(component->class); |
| 1146 | BT_LOGD("Added component to graph: " |
| 1147 | "graph-addr=%p, comp-cls-addr=%p, " |
| 1148 | "comp-cls-type=%s, name=\"%s\", params-addr=%p, " |
| 1149 | "init-method-data-addr=%p, comp-addr=%p", |
| 1150 | graph, component_class, bt_component_class_type_string(type), |
| 1151 | name, params, init_method_data, component); |
| 1152 | |
| 1153 | if (user_component) { |
| 1154 | /* Move reference to user */ |
| 1155 | *user_component = component; |
| 1156 | component = NULL; |
| 1157 | } |
| 1158 | |
| 1159 | end: |
| 1160 | bt_put(component); |
| 1161 | bt_put(params); |
| 1162 | if (graph) { |
| 1163 | graph->can_consume = init_can_consume; |
| 1164 | } |
| 1165 | return graph_status; |
| 1166 | } |
| 1167 | |
| 1168 | enum bt_graph_status bt_graph_add_component( |
| 1169 | struct bt_graph *graph, |
| 1170 | struct bt_component_class *component_class, |
| 1171 | const char *name, struct bt_value *params, |
| 1172 | struct bt_component **component) |
| 1173 | { |
| 1174 | return bt_graph_add_component_with_init_method_data(graph, |
| 1175 | component_class, name, params, NULL, component); |
| 1176 | } |
| 1177 | |
| 1178 | BT_HIDDEN |
| 1179 | int bt_graph_remove_unconnected_component(struct bt_graph *graph, |
| 1180 | struct bt_component *component) |
| 1181 | { |
| 1182 | bt_bool init_can_consume; |
| 1183 | int64_t count; |
| 1184 | uint64_t i; |
| 1185 | int ret = 0; |
| 1186 | |
| 1187 | BT_ASSERT(graph); |
| 1188 | BT_ASSERT(component); |
| 1189 | BT_ASSERT(component->base.ref_count.count == 0); |
| 1190 | BT_ASSERT(bt_component_borrow_graph(component) == graph); |
| 1191 | |
| 1192 | init_can_consume = graph->can_consume; |
| 1193 | count = bt_component_get_input_port_count(component); |
| 1194 | |
| 1195 | for (i = 0; i < count; i++) { |
| 1196 | struct bt_port *port = |
| 1197 | bt_component_get_input_port_by_index(component, i); |
| 1198 | |
| 1199 | BT_ASSERT(port); |
| 1200 | bt_put(port); |
| 1201 | |
| 1202 | if (bt_port_is_connected(port)) { |
| 1203 | BT_LOGW("Cannot remove component from graph: " |
| 1204 | "an input port is connected: " |
| 1205 | "graph-addr=%p, comp-addr=%p, " |
| 1206 | "comp-name=\"%s\", connected-port-addr=%p, " |
| 1207 | "connected-port-name=\"%s\"", |
| 1208 | graph, component, |
| 1209 | bt_component_get_name(component), |
| 1210 | port, bt_port_get_name(port)); |
| 1211 | goto error; |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | count = bt_component_get_output_port_count(component); |
| 1216 | |
| 1217 | for (i = 0; i < count; i++) { |
| 1218 | struct bt_port *port = |
| 1219 | bt_component_get_output_port_by_index(component, i); |
| 1220 | |
| 1221 | BT_ASSERT(port); |
| 1222 | bt_put(port); |
| 1223 | |
| 1224 | if (bt_port_is_connected(port)) { |
| 1225 | BT_LOGW("Cannot remove component from graph: " |
| 1226 | "an output port is connected: " |
| 1227 | "graph-addr=%p, comp-addr=%p, " |
| 1228 | "comp-name=\"%s\", connected-port-addr=%p, " |
| 1229 | "connected-port-name=\"%s\"", |
| 1230 | graph, component, |
| 1231 | bt_component_get_name(component), |
| 1232 | port, bt_port_get_name(port)); |
| 1233 | goto error; |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | graph->can_consume = BT_FALSE; |
| 1238 | |
| 1239 | /* Possibly remove from sinks to consume */ |
| 1240 | (void) g_queue_remove(graph->sinks_to_consume, component); |
| 1241 | |
| 1242 | if (graph->sinks_to_consume->length == 0) { |
| 1243 | graph->has_sink = BT_FALSE; |
| 1244 | } |
| 1245 | |
| 1246 | /* |
| 1247 | * This calls bt_object_release() on the component, and since |
| 1248 | * its reference count is 0, its destructor is called. Its |
| 1249 | * destructor calls the user's finalization method (if set). |
| 1250 | */ |
| 1251 | g_ptr_array_remove(graph->components, component); |
| 1252 | goto end; |
| 1253 | |
| 1254 | error: |
| 1255 | ret = -1; |
| 1256 | |
| 1257 | end: |
| 1258 | graph->can_consume = init_can_consume; |
| 1259 | return ret; |
| 1260 | } |
| 1261 | |
| 1262 | BT_HIDDEN |
| 1263 | void bt_graph_add_notification(struct bt_graph *graph, |
| 1264 | struct bt_notification *notif) |
| 1265 | { |
| 1266 | BT_ASSERT(graph); |
| 1267 | BT_ASSERT(notif); |
| 1268 | |
| 1269 | /* |
| 1270 | * It's okay not to take a reference because, when a |
| 1271 | * notification's reference count drops to 0, either: |
| 1272 | * |
| 1273 | * * It is recycled back to one of this graph's pool. |
| 1274 | * * It is destroyed because it doesn't have any link to any |
| 1275 | * graph, which means the original graph is already destroyed. |
| 1276 | */ |
| 1277 | g_ptr_array_add(graph->notifications, notif); |
| 1278 | } |