From 3e9b00233085bfafb21da3746f41d7d1876920dd Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Thu, 27 Apr 2017 19:17:04 -0400 Subject: [PATCH] Set private port's user data on creation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit With separate port creation and user data setting calls, the following scenario is problematic: Source adds a port (private data is not set yet) Graph user's "port added" callback is called This callback connects the new port to a sink port This sink's "port connected" method is called This sink creates a notification iterator on the connected port's connection The source's notification iterator initialization method is called with the added port with NO private data yet With this change, it is guaranteed that as soon as the port can be accessed by any user, its private data is set. Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau --- include/babeltrace/graph/component-internal.h | 6 ++++-- include/babeltrace/graph/port-internal.h | 2 +- .../graph/private-component-filter.h | 4 ++-- .../babeltrace/graph/private-component-sink.h | 2 +- .../graph/private-component-source.h | 2 +- include/babeltrace/graph/private-port.h | 2 -- lib/graph/component.c | 18 ++++++++++-------- lib/graph/filter.c | 8 ++++---- lib/graph/port.c | 19 ++----------------- lib/graph/sink.c | 4 ++-- lib/graph/source.c | 4 ++-- plugins/ctf/fs/fs.c | 11 +++-------- plugins/utils/muxer/muxer.c | 4 ++-- tests/lib/test_graph_topo.c | 2 +- tests/plugins/test-utils-muxer.c | 16 ++++++++-------- 15 files changed, 43 insertions(+), 61 deletions(-) diff --git a/include/babeltrace/graph/component-internal.h b/include/babeltrace/graph/component-internal.h index f6ed6ac7..2308afdd 100644 --- a/include/babeltrace/graph/component-internal.h +++ b/include/babeltrace/graph/component-internal.h @@ -129,11 +129,13 @@ struct bt_port *bt_component_get_output_port_by_name(struct bt_component *comp, BT_HIDDEN struct bt_port *bt_component_add_input_port( - struct bt_component *component, const char *name); + struct bt_component *component, const char *name, + void *user_data); BT_HIDDEN struct bt_port *bt_component_add_output_port( - struct bt_component *component, const char *name); + struct bt_component *component, const char *name, + void *user_data); BT_HIDDEN enum bt_component_status bt_component_remove_port( diff --git a/include/babeltrace/graph/port-internal.h b/include/babeltrace/graph/port-internal.h index 3a277674..807728cb 100644 --- a/include/babeltrace/graph/port-internal.h +++ b/include/babeltrace/graph/port-internal.h @@ -57,7 +57,7 @@ struct bt_private_port *bt_private_port_from_port( BT_HIDDEN struct bt_port *bt_port_create(struct bt_component *parent_component, - enum bt_port_type type, const char *name); + enum bt_port_type type, const char *name, void *user_data); BT_HIDDEN void bt_port_set_connection(struct bt_port *port, diff --git a/include/babeltrace/graph/private-component-filter.h b/include/babeltrace/graph/private-component-filter.h index 51944afd..bb3cfe75 100644 --- a/include/babeltrace/graph/private-component-filter.h +++ b/include/babeltrace/graph/private-component-filter.h @@ -49,7 +49,7 @@ bt_private_component_filter_get_default_output_private_port( extern struct bt_private_port * bt_private_component_filter_add_output_private_port( struct bt_private_component *private_component, - const char *name); + const char *name, void *user_data); extern struct bt_private_port * bt_private_component_filter_get_input_private_port_by_name( @@ -67,7 +67,7 @@ bt_private_component_filter_get_default_input_private_port( extern struct bt_private_port * bt_private_component_filter_add_input_private_port( struct bt_private_component *private_component, - const char *name); + const char *name, void *user_data); #ifdef __cplusplus } diff --git a/include/babeltrace/graph/private-component-sink.h b/include/babeltrace/graph/private-component-sink.h index db3e71f1..9c473ccc 100644 --- a/include/babeltrace/graph/private-component-sink.h +++ b/include/babeltrace/graph/private-component-sink.h @@ -49,7 +49,7 @@ bt_private_component_sink_get_default_input_private_port( extern struct bt_private_port * bt_private_component_sink_add_input_private_port( struct bt_private_component *private_component, - const char *name); + const char *name, void *user_data); #ifdef __cplusplus } diff --git a/include/babeltrace/graph/private-component-source.h b/include/babeltrace/graph/private-component-source.h index 257e6485..da653203 100644 --- a/include/babeltrace/graph/private-component-source.h +++ b/include/babeltrace/graph/private-component-source.h @@ -50,7 +50,7 @@ bt_private_component_source_get_default_output_private_port( extern struct bt_private_port * bt_private_component_source_add_output_private_port( struct bt_private_component *private_component, - const char *name); + const char *name, void *user_data); #ifdef __cplusplus } diff --git a/include/babeltrace/graph/private-port.h b/include/babeltrace/graph/private-port.h index 1455369c..c9c140b7 100644 --- a/include/babeltrace/graph/private-port.h +++ b/include/babeltrace/graph/private-port.h @@ -39,8 +39,6 @@ extern struct bt_private_component *bt_private_port_get_private_component( struct bt_private_port *private_port); extern int bt_private_port_remove_from_component( struct bt_private_port *private_port); -extern int bt_private_port_set_user_data( - struct bt_private_port *private_port, void *user_data); extern void *bt_private_port_get_user_data( struct bt_private_port *private_port); diff --git a/lib/graph/component.c b/lib/graph/component.c index 792a3614..7b433ea9 100644 --- a/lib/graph/component.c +++ b/lib/graph/component.c @@ -135,7 +135,7 @@ enum bt_component_class_type bt_component_get_class_type( static struct bt_port *bt_component_add_port( struct bt_component *component, GPtrArray *ports, - enum bt_port_type port_type, const char *name) + enum bt_port_type port_type, const char *name, void *user_data) { size_t i; struct bt_port *new_port = NULL; @@ -162,7 +162,7 @@ struct bt_port *bt_component_add_port( } } - new_port = bt_port_create(component, port_type, name); + new_port = bt_port_create(component, port_type, name, user_data); if (!new_port) { goto end; } @@ -277,7 +277,7 @@ struct bt_component *bt_component_create_with_init_method_data( type == BT_COMPONENT_CLASS_TYPE_FILTER) { default_port = bt_component_add_port(component, component->output_ports, BT_PORT_TYPE_OUTPUT, - DEFAULT_OUTPUT_PORT_NAME); + DEFAULT_OUTPUT_PORT_NAME, NULL); if (!default_port) { BT_PUT(component); goto end; @@ -290,7 +290,7 @@ struct bt_component *bt_component_create_with_init_method_data( type == BT_COMPONENT_CLASS_TYPE_SINK) { default_port = bt_component_add_port(component, component->input_ports, BT_PORT_TYPE_INPUT, - DEFAULT_INPUT_PORT_NAME); + DEFAULT_INPUT_PORT_NAME, NULL); if (!default_port) { BT_PUT(component); goto end; @@ -492,18 +492,20 @@ struct bt_port *bt_component_get_output_port_by_index(struct bt_component *comp, BT_HIDDEN struct bt_port *bt_component_add_input_port( - struct bt_component *component, const char *name) + struct bt_component *component, const char *name, + void *user_data) { return bt_component_add_port(component, component->input_ports, - BT_PORT_TYPE_INPUT, name); + BT_PORT_TYPE_INPUT, name, user_data); } BT_HIDDEN struct bt_port *bt_component_add_output_port( - struct bt_component *component, const char *name) + struct bt_component *component, const char *name, + void *user_data) { return bt_component_add_port(component, component->output_ports, - BT_PORT_TYPE_OUTPUT, name); + BT_PORT_TYPE_OUTPUT, name, user_data); } static diff --git a/lib/graph/filter.c b/lib/graph/filter.c index 81f872e6..e109e15e 100644 --- a/lib/graph/filter.c +++ b/lib/graph/filter.c @@ -206,7 +206,7 @@ bt_private_component_filter_get_default_input_private_port( struct bt_private_port *bt_private_component_filter_add_input_private_port( struct bt_private_component *private_component, - const char *name) + const char *name, void *user_data) { struct bt_port *port = NULL; struct bt_component *component = @@ -217,7 +217,7 @@ struct bt_private_port *bt_private_component_filter_add_input_private_port( goto end; } - port = bt_component_add_input_port(component, name); + port = bt_component_add_input_port(component, name, user_data); end: return bt_private_port_from_port(port); } @@ -242,7 +242,7 @@ bt_private_component_filter_get_default_output_private_port( struct bt_private_port *bt_private_component_filter_add_output_private_port( struct bt_private_component *private_component, - const char *name) + const char *name, void *user_data) { struct bt_port *port = NULL; struct bt_component *component = @@ -253,7 +253,7 @@ struct bt_private_port *bt_private_component_filter_add_output_private_port( goto end; } - port = bt_component_add_output_port(component, name); + port = bt_component_add_output_port(component, name, user_data); end: return bt_private_port_from_port(port); } diff --git a/lib/graph/port.c b/lib/graph/port.c index c0d5e061..b0e95f6e 100644 --- a/lib/graph/port.c +++ b/lib/graph/port.c @@ -51,7 +51,7 @@ struct bt_port *bt_port_from_private_port( BT_HIDDEN struct bt_port *bt_port_create(struct bt_component *parent_component, - enum bt_port_type type, const char *name) + enum bt_port_type type, const char *name, void *user_data) { struct bt_port *port = NULL; @@ -76,6 +76,7 @@ struct bt_port *bt_port_create(struct bt_component *parent_component, } port->type = type; + port->user_data = user_data; bt_object_set_parent(port, &parent_component->base); end: @@ -188,22 +189,6 @@ end: return ret; } -int bt_private_port_set_user_data( - struct bt_private_port *private_port, void *user_data) -{ - int ret = 0; - - if (!private_port) { - ret = -1; - goto end; - } - - bt_port_from_private(private_port)->user_data = user_data; - -end: - return ret; -} - void *bt_private_port_get_user_data( struct bt_private_port *private_port) { diff --git a/lib/graph/sink.c b/lib/graph/sink.c index 1bd09283..1eedcf73 100644 --- a/lib/graph/sink.c +++ b/lib/graph/sink.c @@ -171,7 +171,7 @@ struct bt_private_port *bt_private_component_sink_get_default_input_private_port struct bt_private_port *bt_private_component_sink_add_input_private_port( struct bt_private_component *private_component, - const char *name) + const char *name, void *user_data) { struct bt_port *port = NULL; struct bt_component *component = @@ -182,7 +182,7 @@ struct bt_private_port *bt_private_component_sink_add_input_private_port( goto end; } - port = bt_component_add_input_port(component, name); + port = bt_component_add_input_port(component, name, user_data); end: return bt_private_port_from_port(port); } diff --git a/lib/graph/source.c b/lib/graph/source.c index ed052ded..e76959b8 100644 --- a/lib/graph/source.c +++ b/lib/graph/source.c @@ -161,7 +161,7 @@ struct bt_private_port *bt_private_component_source_get_default_output_private_p struct bt_private_port *bt_private_component_source_add_output_private_port( struct bt_private_component *private_component, - const char *name) + const char *name, void *user_data) { struct bt_port *port = NULL; struct bt_component *component = @@ -172,7 +172,7 @@ struct bt_private_port *bt_private_component_source_add_output_private_port( goto end; } - port = bt_component_add_output_port(component, name); + port = bt_component_add_output_port(component, name, user_data); end: return bt_private_port_from_port(port); } diff --git a/plugins/ctf/fs/fs.c b/plugins/ctf/fs/fs.c index e734a1e1..669d2daa 100644 --- a/plugins/ctf/fs/fs.c +++ b/plugins/ctf/fs/fs.c @@ -189,12 +189,6 @@ int create_one_port(struct ctf_fs_component *ctf_fs, port_name->str, stream_path); /* Create output port for this file */ - port = bt_private_component_source_add_output_private_port( - ctf_fs->priv_comp, port_name->str); - if (!port) { - goto error; - } - port_data = g_new0(struct ctf_fs_port_data, 1); if (!port_data) { goto error; @@ -205,8 +199,9 @@ int create_one_port(struct ctf_fs_component *ctf_fs, goto error; } - ret = bt_private_port_set_user_data(port, port_data); - if (ret) { + port = bt_private_component_source_add_output_private_port( + ctf_fs->priv_comp, port_name->str, port_data); + if (!port) { goto error; } diff --git a/plugins/utils/muxer/muxer.c b/plugins/utils/muxer/muxer.c index 5db1e426..475dbf61 100644 --- a/plugins/utils/muxer/muxer.c +++ b/plugins/utils/muxer/muxer.c @@ -173,7 +173,7 @@ int ensure_available_input_port(struct bt_private_component *priv_comp) g_string_append_printf(port_name, "%u", muxer_comp->next_port_num); priv_port = bt_private_component_filter_add_input_private_port( - priv_comp, port_name->str); + priv_comp, port_name->str, NULL); if (!priv_port) { ret = -1; goto end; @@ -228,7 +228,7 @@ int create_output_port(struct bt_private_component *priv_comp) int ret = 0; priv_port = bt_private_component_filter_add_output_private_port( - priv_comp, "out"); + priv_comp, "out", NULL); if (!priv_port) { ret = -1; } diff --git a/tests/lib/test_graph_topo.c b/tests/lib/test_graph_topo.c index 274f7872..ccde32af 100644 --- a/tests/lib/test_graph_topo.c +++ b/tests/lib/test_graph_topo.c @@ -338,7 +338,7 @@ void src_port_connected(struct bt_private_component *private_component, switch (current_test) { case TEST_SRC_ADDS_PORT_IN_PORT_CONNECTED: port = bt_private_component_source_add_output_private_port( - private_component, "hello"); + private_component, "hello", NULL); assert(port); bt_put(port); break; diff --git a/tests/plugins/test-utils-muxer.c b/tests/plugins/test-utils-muxer.c index 707672c5..50f5be5d 100644 --- a/tests/plugins/test-utils-muxer.c +++ b/tests/plugins/test-utils-muxer.c @@ -634,11 +634,11 @@ struct bt_notification_iterator_next_return src_iter_next( struct bt_private_port *priv_port; priv_port = bt_private_component_source_add_output_private_port( - private_component, "out1"); + private_component, "out1", NULL); assert(priv_port); bt_put(priv_port); priv_port = bt_private_component_source_add_output_private_port( - private_component, "out2"); + private_component, "out2", NULL); assert(priv_port); bt_put(priv_port); next_return.status = BT_NOTIFICATION_ITERATOR_STATUS_END; @@ -655,11 +655,11 @@ struct bt_notification_iterator_next_return src_iter_next( struct bt_private_port *priv_port; priv_port = bt_private_component_source_add_output_private_port( - private_component, "out1"); + private_component, "out1", NULL); assert(priv_port); bt_put(priv_port); priv_port = bt_private_component_source_add_output_private_port( - private_component, "out2"); + private_component, "out2", NULL); assert(priv_port); bt_put(priv_port); next_return.status = BT_NOTIFICATION_ITERATOR_STATUS_END; @@ -708,28 +708,28 @@ enum bt_component_status src_init( if (nb_ports >= 1) { priv_port = bt_private_component_source_add_output_private_port( - private_component, "out0"); + private_component, "out0", NULL); assert(priv_port); bt_put(priv_port); } if (nb_ports >= 2) { priv_port = bt_private_component_source_add_output_private_port( - private_component, "out1"); + private_component, "out1", NULL); assert(priv_port); bt_put(priv_port); } if (nb_ports >= 3) { priv_port = bt_private_component_source_add_output_private_port( - private_component, "out2"); + private_component, "out2", NULL); assert(priv_port); bt_put(priv_port); } if (nb_ports >= 4) { priv_port = bt_private_component_source_add_output_private_port( - private_component, "out3"); + private_component, "out3", NULL); assert(priv_port); bt_put(priv_port); } -- 2.34.1