| 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/compiler-internal.h> |
| 39 | #include <babeltrace/types.h> |
| 40 | #include <unistd.h> |
| 41 | #include <glib.h> |
| 42 | |
| 43 | struct bt_graph_listener { |
| 44 | void *func; |
| 45 | void *data; |
| 46 | }; |
| 47 | |
| 48 | static |
| 49 | void bt_graph_destroy(struct bt_object *obj) |
| 50 | { |
| 51 | struct bt_graph *graph = container_of(obj, |
| 52 | struct bt_graph, base); |
| 53 | |
| 54 | /* |
| 55 | * The graph's reference count is 0 if we're here. Increment |
| 56 | * it to avoid a double-destroy (possibly infinitely recursive) |
| 57 | * in this situation: |
| 58 | * |
| 59 | * 1. We put and destroy a connection. |
| 60 | * 2. This connection's destructor finalizes its active |
| 61 | * notification iterators. |
| 62 | * 3. A notification iterator's finalization function gets a |
| 63 | * new reference on its component (reference count goes from |
| 64 | * 0 to 1). |
| 65 | * 4. Since this component's reference count goes to 1, it takes |
| 66 | * a reference on its parent (this graph). This graph's |
| 67 | * reference count goes from 0 to 1. |
| 68 | * 5. The notification iterator's finalization function puts its |
| 69 | * component reference (reference count goes from 1 to 0). |
| 70 | * 6. Since this component's reference count goes from 1 to 0, |
| 71 | * it puts its parent (this graph). This graph's reference |
| 72 | * count goes from 1 to 0. |
| 73 | * 7. Since this graph's reference count goes from 1 to 0, |
| 74 | * its destructor is called (this function). |
| 75 | * |
| 76 | * With the incrementation below, the graph's reference count at |
| 77 | * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This |
| 78 | * ensures that this function is not called two times. |
| 79 | */ |
| 80 | BT_LOGD("Destroying graph: addr=%p", graph); |
| 81 | obj->ref_count.count++; |
| 82 | |
| 83 | if (graph->connections) { |
| 84 | BT_LOGD_STR("Destroying connections."); |
| 85 | g_ptr_array_free(graph->connections, TRUE); |
| 86 | } |
| 87 | if (graph->components) { |
| 88 | BT_LOGD_STR("Destroying components."); |
| 89 | g_ptr_array_free(graph->components, TRUE); |
| 90 | } |
| 91 | if (graph->sinks_to_consume) { |
| 92 | g_queue_free(graph->sinks_to_consume); |
| 93 | } |
| 94 | |
| 95 | if (graph->listeners.port_added) { |
| 96 | g_array_free(graph->listeners.port_added, TRUE); |
| 97 | } |
| 98 | |
| 99 | if (graph->listeners.port_removed) { |
| 100 | g_array_free(graph->listeners.port_removed, TRUE); |
| 101 | } |
| 102 | |
| 103 | if (graph->listeners.ports_connected) { |
| 104 | g_array_free(graph->listeners.ports_connected, TRUE); |
| 105 | } |
| 106 | |
| 107 | if (graph->listeners.ports_disconnected) { |
| 108 | g_array_free(graph->listeners.ports_disconnected, TRUE); |
| 109 | } |
| 110 | |
| 111 | g_free(graph); |
| 112 | } |
| 113 | |
| 114 | static |
| 115 | int init_listeners_array(GArray **listeners) |
| 116 | { |
| 117 | int ret = 0; |
| 118 | |
| 119 | assert(listeners); |
| 120 | *listeners = g_array_new(FALSE, TRUE, sizeof(struct bt_graph_listener)); |
| 121 | if (!*listeners) { |
| 122 | BT_LOGE_STR("Failed to allocate one GArray."); |
| 123 | ret = -1; |
| 124 | goto end; |
| 125 | } |
| 126 | |
| 127 | end: |
| 128 | return ret; |
| 129 | } |
| 130 | |
| 131 | struct bt_graph *bt_graph_create(void) |
| 132 | { |
| 133 | struct bt_graph *graph; |
| 134 | int ret; |
| 135 | |
| 136 | BT_LOGD_STR("Creating graph object."); |
| 137 | graph = g_new0(struct bt_graph, 1); |
| 138 | if (!graph) { |
| 139 | BT_LOGE_STR("Failed to allocate one graph."); |
| 140 | goto end; |
| 141 | } |
| 142 | |
| 143 | bt_object_init(graph, bt_graph_destroy); |
| 144 | |
| 145 | graph->connections = g_ptr_array_new_with_free_func(bt_object_release); |
| 146 | if (!graph->connections) { |
| 147 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
| 148 | goto error; |
| 149 | } |
| 150 | graph->components = g_ptr_array_new_with_free_func(bt_object_release); |
| 151 | if (!graph->components) { |
| 152 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
| 153 | goto error; |
| 154 | } |
| 155 | graph->sinks_to_consume = g_queue_new(); |
| 156 | if (!graph->sinks_to_consume) { |
| 157 | BT_LOGE_STR("Failed to allocate one GQueue."); |
| 158 | goto error; |
| 159 | } |
| 160 | |
| 161 | ret = init_listeners_array(&graph->listeners.port_added); |
| 162 | if (ret) { |
| 163 | BT_LOGE_STR("Cannot create the \"port added\" listener array."); |
| 164 | goto error; |
| 165 | } |
| 166 | |
| 167 | ret = init_listeners_array(&graph->listeners.port_removed); |
| 168 | if (ret) { |
| 169 | BT_LOGE_STR("Cannot create the \"port removed\" listener array."); |
| 170 | goto error; |
| 171 | } |
| 172 | |
| 173 | ret = init_listeners_array(&graph->listeners.ports_connected); |
| 174 | if (ret) { |
| 175 | BT_LOGE_STR("Cannot create the \"port connected\" listener array."); |
| 176 | goto error; |
| 177 | } |
| 178 | |
| 179 | ret = init_listeners_array(&graph->listeners.ports_disconnected); |
| 180 | if (ret) { |
| 181 | BT_LOGE_STR("Cannot create the \"port disconneted\" listener array."); |
| 182 | goto error; |
| 183 | } |
| 184 | |
| 185 | BT_LOGD("Created graph object: addr=%p", graph); |
| 186 | |
| 187 | end: |
| 188 | return graph; |
| 189 | error: |
| 190 | BT_PUT(graph); |
| 191 | goto end; |
| 192 | } |
| 193 | |
| 194 | enum bt_graph_status bt_graph_connect_ports(struct bt_graph *graph, |
| 195 | struct bt_port *upstream_port, struct bt_port *downstream_port, |
| 196 | struct bt_connection **user_connection) |
| 197 | { |
| 198 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
| 199 | struct bt_connection *connection = NULL; |
| 200 | struct bt_graph *upstream_graph = NULL; |
| 201 | struct bt_graph *downstream_graph = NULL; |
| 202 | struct bt_component *upstream_component = NULL; |
| 203 | struct bt_component *downstream_component = NULL; |
| 204 | enum bt_component_status component_status; |
| 205 | bt_bool upstream_was_already_in_graph; |
| 206 | bt_bool downstream_was_already_in_graph; |
| 207 | |
| 208 | if (!graph) { |
| 209 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 210 | status = BT_GRAPH_STATUS_INVALID; |
| 211 | goto end; |
| 212 | } |
| 213 | |
| 214 | if (!upstream_port) { |
| 215 | BT_LOGW_STR("Invalid parameter: upstream port is NULL."); |
| 216 | status = BT_GRAPH_STATUS_INVALID; |
| 217 | goto end; |
| 218 | } |
| 219 | |
| 220 | if (!downstream_port) { |
| 221 | BT_LOGW_STR("Invalid parameter: downstream port is NULL."); |
| 222 | status = BT_GRAPH_STATUS_INVALID; |
| 223 | goto end; |
| 224 | } |
| 225 | |
| 226 | BT_LOGD("Connecting component ports within graph: " |
| 227 | "graph-addr=%p, " |
| 228 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
| 229 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", |
| 230 | graph, upstream_port, bt_port_get_name(upstream_port), |
| 231 | downstream_port, bt_port_get_name(downstream_port)); |
| 232 | |
| 233 | if (graph->canceled) { |
| 234 | BT_LOGW_STR("Invalid parameter: graph is canceled."); |
| 235 | status = BT_GRAPH_STATUS_CANCELED; |
| 236 | goto end; |
| 237 | } |
| 238 | |
| 239 | /* Ensure appropriate types for upstream and downstream ports. */ |
| 240 | if (bt_port_get_type(upstream_port) != BT_PORT_TYPE_OUTPUT) { |
| 241 | BT_LOGW_STR("Invalid parameter: upstream port is not an output port."); |
| 242 | status = BT_GRAPH_STATUS_INVALID; |
| 243 | goto end; |
| 244 | } |
| 245 | if (bt_port_get_type(downstream_port) != BT_PORT_TYPE_INPUT) { |
| 246 | BT_LOGW_STR("Invalid parameter: downstream port is not an input port."); |
| 247 | status = BT_GRAPH_STATUS_INVALID; |
| 248 | goto end; |
| 249 | } |
| 250 | |
| 251 | /* Ensure that both ports are currently unconnected. */ |
| 252 | if (bt_port_is_connected(upstream_port)) { |
| 253 | BT_LOGW_STR("Invalid parameter: upstream port is already connected."); |
| 254 | status = BT_GRAPH_STATUS_INVALID; |
| 255 | goto end; |
| 256 | } |
| 257 | |
| 258 | if (bt_port_is_connected(downstream_port)) { |
| 259 | BT_LOGW_STR("Invalid parameter: downstream port is already connected."); |
| 260 | status = BT_GRAPH_STATUS_INVALID; |
| 261 | goto end; |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Ensure that both ports are still attached to their creating |
| 266 | * component. |
| 267 | */ |
| 268 | upstream_component = bt_port_get_component(upstream_port); |
| 269 | if (!upstream_component) { |
| 270 | BT_LOGW_STR("Invalid parameter: upstream port is loose (does not belong to a component)"); |
| 271 | status = BT_GRAPH_STATUS_INVALID; |
| 272 | goto end; |
| 273 | } |
| 274 | |
| 275 | downstream_component = bt_port_get_component(downstream_port); |
| 276 | if (!downstream_component) { |
| 277 | BT_LOGW_STR("Invalid parameter: downstream port is loose (does not belong to a component)"); |
| 278 | status = BT_GRAPH_STATUS_INVALID; |
| 279 | goto end; |
| 280 | } |
| 281 | |
| 282 | BT_LOGD("Connecting component ports: " |
| 283 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " |
| 284 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\"", |
| 285 | upstream_component, bt_component_get_name(upstream_component), |
| 286 | downstream_component, bt_component_get_name(downstream_component)); |
| 287 | |
| 288 | /* Ensure the components are not already part of another graph. */ |
| 289 | upstream_graph = bt_component_get_graph(upstream_component); |
| 290 | if (upstream_graph && (graph != upstream_graph)) { |
| 291 | BT_LOGW("Invalid parameter: upstream port's component is already part of another graph: " |
| 292 | "other-graph-addr=%p", upstream_graph); |
| 293 | status = BT_GRAPH_STATUS_ALREADY_IN_A_GRAPH; |
| 294 | goto end; |
| 295 | } |
| 296 | upstream_was_already_in_graph = (graph == upstream_graph); |
| 297 | downstream_graph = bt_component_get_graph(downstream_component); |
| 298 | if (downstream_graph && (graph != downstream_graph)) { |
| 299 | BT_LOGW("Invalid parameter: downstream port's component is already part of another graph: " |
| 300 | "other-graph-addr=%p", downstream_graph); |
| 301 | status = BT_GRAPH_STATUS_ALREADY_IN_A_GRAPH; |
| 302 | goto end; |
| 303 | } |
| 304 | downstream_was_already_in_graph = (graph == downstream_graph); |
| 305 | |
| 306 | /* |
| 307 | * At this point the ports are not connected yet. Both |
| 308 | * components need to accept an eventual connection to their |
| 309 | * port by the other port before we continue. |
| 310 | */ |
| 311 | BT_LOGD_STR("Asking upstream component to accept the connection."); |
| 312 | component_status = bt_component_accept_port_connection( |
| 313 | upstream_component, upstream_port, downstream_port); |
| 314 | if (component_status != BT_COMPONENT_STATUS_OK) { |
| 315 | if (component_status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) { |
| 316 | BT_LOGD_STR("Upstream component refused the connection."); |
| 317 | } else { |
| 318 | BT_LOGW("Cannot ask upstream component to accept the connection: " |
| 319 | "status=%s", bt_component_status_string(component_status)); |
| 320 | } |
| 321 | |
| 322 | status = bt_graph_status_from_component_status( |
| 323 | component_status); |
| 324 | goto end; |
| 325 | } |
| 326 | |
| 327 | BT_LOGD_STR("Asking downstream component to accept the connection."); |
| 328 | component_status = bt_component_accept_port_connection( |
| 329 | downstream_component, downstream_port, upstream_port); |
| 330 | if (component_status != BT_COMPONENT_STATUS_OK) { |
| 331 | if (component_status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) { |
| 332 | BT_LOGD_STR("Downstream component refused the connection."); |
| 333 | } else { |
| 334 | BT_LOGW("Cannot ask downstream component to accept the connection: " |
| 335 | "status=%s", bt_component_status_string(component_status)); |
| 336 | } |
| 337 | |
| 338 | status = bt_graph_status_from_component_status( |
| 339 | component_status); |
| 340 | goto end; |
| 341 | } |
| 342 | |
| 343 | BT_LOGD_STR("Creating connection."); |
| 344 | connection = bt_connection_create(graph, upstream_port, |
| 345 | downstream_port); |
| 346 | if (!connection) { |
| 347 | BT_LOGW("Cannot create connection object."); |
| 348 | status = BT_GRAPH_STATUS_NOMEM; |
| 349 | goto end; |
| 350 | } |
| 351 | |
| 352 | BT_LOGD("Connection object created: conn-addr=%p", connection); |
| 353 | |
| 354 | /* |
| 355 | * Ownership of upstream_component/downstream_component and of |
| 356 | * the connection object is transferred to the graph. |
| 357 | */ |
| 358 | g_ptr_array_add(graph->connections, connection); |
| 359 | |
| 360 | if (!upstream_was_already_in_graph) { |
| 361 | g_ptr_array_add(graph->components, upstream_component); |
| 362 | bt_component_set_graph(upstream_component, graph); |
| 363 | } |
| 364 | if (!downstream_was_already_in_graph) { |
| 365 | g_ptr_array_add(graph->components, downstream_component); |
| 366 | bt_component_set_graph(downstream_component, graph); |
| 367 | if (bt_component_get_class_type(downstream_component) == |
| 368 | BT_COMPONENT_CLASS_TYPE_SINK) { |
| 369 | g_queue_push_tail(graph->sinks_to_consume, |
| 370 | downstream_component); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /* |
| 375 | * The graph is now the parent of these components which |
| 376 | * garantees their existence for the duration of the graph's |
| 377 | * lifetime. |
| 378 | */ |
| 379 | |
| 380 | /* |
| 381 | * Notify both components that their port is connected. |
| 382 | */ |
| 383 | BT_LOGD_STR("Notifying upstream component that its port is connected."); |
| 384 | bt_component_port_connected(upstream_component, upstream_port, |
| 385 | downstream_port); |
| 386 | BT_LOGD_STR("Notifying downstream component that its port is connected."); |
| 387 | bt_component_port_connected(downstream_component, downstream_port, |
| 388 | upstream_port); |
| 389 | |
| 390 | /* |
| 391 | * Notify the graph's creator that both ports are connected. |
| 392 | */ |
| 393 | BT_LOGD_STR("Notifying graph's user that new component ports are connected."); |
| 394 | bt_graph_notify_ports_connected(graph, upstream_port, downstream_port); |
| 395 | BT_LOGD("Connected component ports within graph: " |
| 396 | "graph-addr=%p, " |
| 397 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " |
| 398 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\", " |
| 399 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
| 400 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", |
| 401 | graph, |
| 402 | upstream_component, bt_component_get_name(upstream_component), |
| 403 | downstream_component, bt_component_get_name(downstream_component), |
| 404 | upstream_port, bt_port_get_name(upstream_port), |
| 405 | downstream_port, bt_port_get_name(downstream_port)); |
| 406 | |
| 407 | if (user_connection) { |
| 408 | /* Move reference to user */ |
| 409 | *user_connection = connection; |
| 410 | connection = NULL; |
| 411 | } |
| 412 | |
| 413 | end: |
| 414 | bt_put(upstream_graph); |
| 415 | bt_put(downstream_graph); |
| 416 | bt_put(upstream_component); |
| 417 | bt_put(downstream_component); |
| 418 | bt_put(connection); |
| 419 | return status; |
| 420 | } |
| 421 | |
| 422 | static |
| 423 | enum bt_graph_status bt_graph_consume_no_check(struct bt_graph *graph) |
| 424 | { |
| 425 | struct bt_component *sink; |
| 426 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
| 427 | enum bt_component_status comp_status; |
| 428 | GList *current_node; |
| 429 | |
| 430 | BT_LOGV("Making next sink consume: addr=%p", graph); |
| 431 | |
| 432 | if (g_queue_is_empty(graph->sinks_to_consume)) { |
| 433 | BT_LOGV_STR("Graph's sink queue is empty: end of graph."); |
| 434 | status = BT_GRAPH_STATUS_END; |
| 435 | goto end; |
| 436 | } |
| 437 | |
| 438 | current_node = g_queue_pop_head_link(graph->sinks_to_consume); |
| 439 | sink = current_node->data; |
| 440 | BT_LOGV("Chose next sink to consume: comp-addr=%p, comp-name=\"%s\"", |
| 441 | sink, bt_component_get_name(sink)); |
| 442 | comp_status = bt_component_sink_consume(sink); |
| 443 | BT_LOGV("Consumed from sink: status=%s", |
| 444 | bt_component_status_string(comp_status)); |
| 445 | switch (comp_status) { |
| 446 | case BT_COMPONENT_STATUS_OK: |
| 447 | break; |
| 448 | case BT_COMPONENT_STATUS_END: |
| 449 | status = BT_GRAPH_STATUS_END; |
| 450 | break; |
| 451 | case BT_COMPONENT_STATUS_AGAIN: |
| 452 | status = BT_GRAPH_STATUS_AGAIN; |
| 453 | break; |
| 454 | case BT_COMPONENT_STATUS_INVALID: |
| 455 | status = BT_GRAPH_STATUS_INVALID; |
| 456 | break; |
| 457 | default: |
| 458 | status = BT_GRAPH_STATUS_ERROR; |
| 459 | break; |
| 460 | } |
| 461 | |
| 462 | if (status != BT_GRAPH_STATUS_END) { |
| 463 | g_queue_push_tail_link(graph->sinks_to_consume, current_node); |
| 464 | goto end; |
| 465 | } |
| 466 | |
| 467 | /* End reached, the node is not added back to the queue and free'd. */ |
| 468 | g_queue_delete_link(graph->sinks_to_consume, current_node); |
| 469 | |
| 470 | /* Don't forward an END status if there are sinks left to consume. */ |
| 471 | if (!g_queue_is_empty(graph->sinks_to_consume)) { |
| 472 | status = BT_GRAPH_STATUS_OK; |
| 473 | goto end; |
| 474 | } |
| 475 | end: |
| 476 | BT_LOGV("Graph consumed: status=%s", bt_graph_status_string(status)); |
| 477 | return status; |
| 478 | } |
| 479 | |
| 480 | enum bt_graph_status bt_graph_consume(struct bt_graph *graph) |
| 481 | { |
| 482 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
| 483 | |
| 484 | if (!graph) { |
| 485 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 486 | status = BT_GRAPH_STATUS_INVALID; |
| 487 | goto end; |
| 488 | } |
| 489 | |
| 490 | if (graph->canceled) { |
| 491 | BT_LOGW("Invalid parameter: graph is canceled: " |
| 492 | "graph-addr=%p", graph); |
| 493 | status = BT_GRAPH_STATUS_CANCELED; |
| 494 | goto end; |
| 495 | } |
| 496 | |
| 497 | status = bt_graph_consume_no_check(graph); |
| 498 | |
| 499 | end: |
| 500 | return status; |
| 501 | } |
| 502 | |
| 503 | enum bt_graph_status bt_graph_run(struct bt_graph *graph) |
| 504 | { |
| 505 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
| 506 | |
| 507 | if (!graph) { |
| 508 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 509 | status = BT_GRAPH_STATUS_INVALID; |
| 510 | goto end; |
| 511 | } |
| 512 | |
| 513 | if (graph->canceled) { |
| 514 | BT_LOGW("Invalid parameter: graph is canceled: " |
| 515 | "graph-addr=%p", graph); |
| 516 | status = BT_GRAPH_STATUS_CANCELED; |
| 517 | goto end; |
| 518 | } |
| 519 | |
| 520 | BT_LOGV("Running graph: addr=%p", graph); |
| 521 | |
| 522 | do { |
| 523 | /* |
| 524 | * Check if the graph is canceled at each iteration. If |
| 525 | * the graph was canceled by another thread or by a |
| 526 | * signal, this is not a warning nor an error, it was |
| 527 | * intentional: log with a DEBUG level only. |
| 528 | */ |
| 529 | if (graph->canceled) { |
| 530 | BT_LOGD("Stopping the graph: graph is canceled: " |
| 531 | "graph-addr=%p", graph); |
| 532 | status = BT_GRAPH_STATUS_CANCELED; |
| 533 | goto end; |
| 534 | } |
| 535 | |
| 536 | status = bt_graph_consume(graph); |
| 537 | if (status == BT_GRAPH_STATUS_AGAIN) { |
| 538 | /* |
| 539 | * If AGAIN is received and there are multiple |
| 540 | * sinks, go ahead and consume from the next |
| 541 | * sink. |
| 542 | * |
| 543 | * However, in the case where a single sink is |
| 544 | * left, the caller can decide to busy-wait and |
| 545 | * call bt_graph_run() continuously until the |
| 546 | * source is ready or it can decide to sleep for |
| 547 | * an arbitrary amount of time. |
| 548 | */ |
| 549 | if (graph->sinks_to_consume->length > 1) { |
| 550 | status = BT_GRAPH_STATUS_OK; |
| 551 | } |
| 552 | } |
| 553 | } while (status == BT_GRAPH_STATUS_OK); |
| 554 | |
| 555 | if (g_queue_is_empty(graph->sinks_to_consume)) { |
| 556 | status = BT_GRAPH_STATUS_END; |
| 557 | } |
| 558 | |
| 559 | end: |
| 560 | BT_LOGV("Graph ran: status=%s", bt_graph_status_string(status)); |
| 561 | return status; |
| 562 | } |
| 563 | |
| 564 | static |
| 565 | int add_listener(GArray *listeners, void *func, void *data) |
| 566 | { |
| 567 | struct bt_graph_listener listener = { |
| 568 | .func = func, |
| 569 | .data = data, |
| 570 | }; |
| 571 | |
| 572 | g_array_append_val(listeners, listener); |
| 573 | return listeners->len - 1; |
| 574 | } |
| 575 | |
| 576 | int bt_graph_add_port_added_listener( |
| 577 | struct bt_graph *graph, |
| 578 | bt_graph_port_added_listener listener, void *data) |
| 579 | { |
| 580 | int ret; |
| 581 | |
| 582 | if (!graph) { |
| 583 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 584 | ret = -1; |
| 585 | goto end; |
| 586 | } |
| 587 | |
| 588 | if (!listener) { |
| 589 | BT_LOGW_STR("Invalid parameter: listener is NULL."); |
| 590 | ret = -1; |
| 591 | goto end; |
| 592 | } |
| 593 | |
| 594 | ret = add_listener(graph->listeners.port_added, listener, data); |
| 595 | BT_LOGV("Added \"port added\" listener to graph: " |
| 596 | "graph-addr=%p, listener-addr=%p, pos=%d", |
| 597 | graph, listener, ret); |
| 598 | |
| 599 | end: |
| 600 | return ret; |
| 601 | } |
| 602 | |
| 603 | int bt_graph_add_port_removed_listener( |
| 604 | struct bt_graph *graph, |
| 605 | bt_graph_port_removed_listener listener, void *data) |
| 606 | { |
| 607 | int ret; |
| 608 | |
| 609 | if (!graph) { |
| 610 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 611 | ret = -1; |
| 612 | goto end; |
| 613 | } |
| 614 | |
| 615 | if (!listener) { |
| 616 | BT_LOGW_STR("Invalid parameter: listener is NULL."); |
| 617 | ret = -1; |
| 618 | goto end; |
| 619 | } |
| 620 | |
| 621 | ret = add_listener(graph->listeners.port_removed, listener, data); |
| 622 | BT_LOGV("Added \"port removed\" listener to graph: " |
| 623 | "graph-addr=%p, listener-addr=%p, pos=%d", |
| 624 | graph, listener, ret); |
| 625 | |
| 626 | end: |
| 627 | return ret; |
| 628 | } |
| 629 | |
| 630 | int bt_graph_add_ports_connected_listener( |
| 631 | struct bt_graph *graph, |
| 632 | bt_graph_ports_connected_listener listener, void *data) |
| 633 | { |
| 634 | int ret; |
| 635 | |
| 636 | if (!graph) { |
| 637 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 638 | ret = -1; |
| 639 | goto end; |
| 640 | } |
| 641 | |
| 642 | if (!listener) { |
| 643 | BT_LOGW_STR("Invalid parameter: listener is NULL."); |
| 644 | ret = -1; |
| 645 | goto end; |
| 646 | } |
| 647 | |
| 648 | ret = add_listener(graph->listeners.ports_connected, listener, data); |
| 649 | BT_LOGV("Added \"port connected\" listener to graph: " |
| 650 | "graph-addr=%p, listener-addr=%p, pos=%d", |
| 651 | graph, listener, ret); |
| 652 | |
| 653 | end: |
| 654 | return ret; |
| 655 | } |
| 656 | |
| 657 | int bt_graph_add_ports_disconnected_listener( |
| 658 | struct bt_graph *graph, |
| 659 | bt_graph_ports_disconnected_listener listener, void *data) |
| 660 | { |
| 661 | int ret; |
| 662 | |
| 663 | if (!graph) { |
| 664 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 665 | ret = -1; |
| 666 | goto end; |
| 667 | } |
| 668 | |
| 669 | if (!listener) { |
| 670 | BT_LOGW_STR("Invalid parameter: listener is NULL."); |
| 671 | ret = -1; |
| 672 | goto end; |
| 673 | } |
| 674 | |
| 675 | ret = add_listener(graph->listeners.ports_disconnected, listener, data); |
| 676 | BT_LOGV("Added \"port disconnected\" listener to graph: " |
| 677 | "graph-addr=%p, listener-addr=%p, pos=%d", |
| 678 | graph, listener, ret); |
| 679 | |
| 680 | end: |
| 681 | return ret; |
| 682 | } |
| 683 | |
| 684 | BT_HIDDEN |
| 685 | void bt_graph_notify_port_added(struct bt_graph *graph, struct bt_port *port) |
| 686 | { |
| 687 | size_t i; |
| 688 | |
| 689 | BT_LOGV("Notifying graph listeners that a port was added: " |
| 690 | "graph-addr=%p, port-addr=%p, port-name=\"%s\"", |
| 691 | graph, port, bt_port_get_name(port)); |
| 692 | |
| 693 | for (i = 0; i < graph->listeners.port_added->len; i++) { |
| 694 | struct bt_graph_listener listener = |
| 695 | g_array_index(graph->listeners.port_added, |
| 696 | struct bt_graph_listener, i); |
| 697 | bt_graph_port_added_listener func = listener.func; |
| 698 | |
| 699 | assert(func); |
| 700 | func(port, listener.data); |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | BT_HIDDEN |
| 705 | void bt_graph_notify_port_removed(struct bt_graph *graph, |
| 706 | struct bt_component *comp, struct bt_port *port) |
| 707 | { |
| 708 | size_t i; |
| 709 | |
| 710 | BT_LOGV("Notifying graph listeners that a port was removed: " |
| 711 | "graph-addr=%p, port-addr=%p, port-name=\"%s\"", |
| 712 | graph, port, bt_port_get_name(port)); |
| 713 | |
| 714 | for (i = 0; i < graph->listeners.port_removed->len; i++) { |
| 715 | struct bt_graph_listener listener = |
| 716 | g_array_index(graph->listeners.port_removed, |
| 717 | struct bt_graph_listener, i); |
| 718 | bt_graph_port_removed_listener func = listener.func; |
| 719 | |
| 720 | assert(func); |
| 721 | func(comp, port, listener.data); |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | BT_HIDDEN |
| 726 | void bt_graph_notify_ports_connected(struct bt_graph *graph, |
| 727 | struct bt_port *upstream_port, struct bt_port *downstream_port) |
| 728 | { |
| 729 | size_t i; |
| 730 | |
| 731 | BT_LOGV("Notifying graph listeners that two ports were connected: " |
| 732 | "graph-addr=%p, " |
| 733 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
| 734 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", |
| 735 | graph, upstream_port, bt_port_get_name(upstream_port), |
| 736 | downstream_port, bt_port_get_name(downstream_port)); |
| 737 | |
| 738 | for (i = 0; i < graph->listeners.ports_connected->len; i++) { |
| 739 | struct bt_graph_listener listener = |
| 740 | g_array_index(graph->listeners.ports_connected, |
| 741 | struct bt_graph_listener, i); |
| 742 | bt_graph_ports_connected_listener func = listener.func; |
| 743 | |
| 744 | assert(func); |
| 745 | func(upstream_port, downstream_port, listener.data); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | BT_HIDDEN |
| 750 | void bt_graph_notify_ports_disconnected(struct bt_graph *graph, |
| 751 | struct bt_component *upstream_comp, |
| 752 | struct bt_component *downstream_comp, |
| 753 | struct bt_port *upstream_port, struct bt_port *downstream_port) |
| 754 | { |
| 755 | size_t i; |
| 756 | |
| 757 | BT_LOGV("Notifying graph listeners that two ports were disconnected: " |
| 758 | "graph-addr=%p, " |
| 759 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
| 760 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", |
| 761 | graph, upstream_port, bt_port_get_name(upstream_port), |
| 762 | downstream_port, bt_port_get_name(downstream_port)); |
| 763 | |
| 764 | for (i = 0; i < graph->listeners.ports_disconnected->len; i++) { |
| 765 | struct bt_graph_listener listener = |
| 766 | g_array_index(graph->listeners.ports_disconnected, |
| 767 | struct bt_graph_listener, i); |
| 768 | bt_graph_ports_disconnected_listener func = listener.func; |
| 769 | |
| 770 | assert(func); |
| 771 | func(upstream_comp, downstream_comp, upstream_port, |
| 772 | downstream_port, listener.data); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | extern enum bt_graph_status bt_graph_cancel(struct bt_graph *graph) |
| 777 | { |
| 778 | enum bt_graph_status ret = BT_GRAPH_STATUS_OK; |
| 779 | |
| 780 | if (!graph) { |
| 781 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
| 782 | ret = BT_GRAPH_STATUS_INVALID; |
| 783 | goto end; |
| 784 | } |
| 785 | |
| 786 | graph->canceled = BT_TRUE; |
| 787 | BT_LOGV("Canceled graph: addr=%p", graph); |
| 788 | |
| 789 | end: |
| 790 | return ret; |
| 791 | } |
| 792 | |
| 793 | extern bt_bool bt_graph_is_canceled(struct bt_graph *graph) |
| 794 | { |
| 795 | return graph ? graph->canceled : BT_FALSE; |
| 796 | } |
| 797 | |
| 798 | BT_HIDDEN |
| 799 | void bt_graph_remove_connection(struct bt_graph *graph, |
| 800 | struct bt_connection *connection) |
| 801 | { |
| 802 | assert(graph); |
| 803 | assert(connection); |
| 804 | BT_LOGV("Removing graph's connection: graph-addr=%p, conn-addr=%p", |
| 805 | graph, connection); |
| 806 | g_ptr_array_remove(graph->connections, connection); |
| 807 | } |