2 * SPDX-License-Identifier: MIT
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 #define BT_LOG_TAG "LIB/CONNECTION"
9 #include "lib/logging.h"
11 #include "common/assert.h"
12 #include "lib/assert-pre.h"
13 #include "lib/assert-post.h"
14 #include <babeltrace2/graph/connection.h>
15 #include "lib/object.h"
16 #include "compat/compiler.h"
21 #include "component.h"
22 #include "connection.h"
24 #include "message/iterator.h"
28 void destroy_connection(struct bt_object
*obj
)
30 struct bt_connection
*connection
= container_of(obj
,
31 struct bt_connection
, base
);
33 BT_LIB_LOGI("Destroying connection: %!+x", connection
);
36 * Make sure that each message iterator which was created for
37 * this connection is finalized before we destroy it. Once a
38 * message iterator is finalized, you cannot use it.
40 * Because connections are destroyed before components within a
41 * graph, this ensures that message iterators are always
42 * finalized before their upstream component.
44 * Ending the connection does exactly this. We pass `false` to
45 * bt_connection_end() here to avoid removing this connection
46 * from the graph: if we're here, we're already in the graph's
49 bt_connection_end(connection
, false);
50 g_ptr_array_free(connection
->iterators
, TRUE
);
51 connection
->iterators
= NULL
;
54 * No bt_object_put_ref on ports as a connection only holds _weak_
61 void try_remove_connection_from_graph(struct bt_connection
*connection
)
63 void *graph
= (void *) bt_object_borrow_parent(&connection
->base
);
65 if (connection
->base
.ref_count
> 0 ||
66 connection
->downstream_port
||
67 connection
->upstream_port
||
68 connection
->iterators
->len
> 0) {
73 * At this point we know that:
75 * 1. The connection is ended (ports were disconnected).
76 * 2. All the message iterators that this connection
77 * created, if any, are finalized.
78 * 3. The connection's reference count is 0, so only the
79 * parent (graph) owns this connection after this call.
81 * In other words, no other object than the graph knows this
84 * It is safe to remove the connection from the graph, therefore
87 BT_LIB_LOGD("Removing self from graph's connections: "
88 "%![graph-]+g, %![conn-]+x", graph
, connection
);
89 bt_graph_remove_connection(graph
, connection
);
93 void parent_is_owner(struct bt_object
*obj
)
95 struct bt_connection
*connection
= container_of(obj
,
96 struct bt_connection
, base
);
98 try_remove_connection_from_graph(connection
);
102 struct bt_connection
*bt_connection_create(struct bt_graph
*graph
,
103 struct bt_port
*upstream_port
,
104 struct bt_port
*downstream_port
)
106 struct bt_connection
*connection
= NULL
;
108 BT_LIB_LOGI("Creating connection: "
109 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
110 graph
, upstream_port
, downstream_port
);
111 connection
= g_new0(struct bt_connection
, 1);
113 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one connection.");
117 bt_object_init_shared_with_parent(&connection
->base
,
119 bt_object_set_parent_is_owner_listener_func(&connection
->base
,
121 connection
->iterators
= g_ptr_array_new();
122 if (!connection
->iterators
) {
123 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
124 BT_OBJECT_PUT_REF_AND_RESET(connection
);
128 /* Weak references are taken, see comment in header. */
129 connection
->upstream_port
= upstream_port
;
130 connection
->downstream_port
= downstream_port
;
131 BT_LIB_LOGD("Setting upstream port's connection: %!+p", upstream_port
);
132 bt_port_set_connection(upstream_port
, connection
);
133 BT_LIB_LOGD("Setting downstream port's connection: %!+p",
135 bt_port_set_connection(downstream_port
, connection
);
136 bt_object_set_parent(&connection
->base
, &graph
->base
);
137 BT_LIB_LOGI("Created connection: %!+x", connection
);
144 void bt_connection_end(struct bt_connection
*conn
, bool try_remove_from_graph
)
146 struct bt_port
*downstream_port
= conn
->downstream_port
;
147 struct bt_port
*upstream_port
= conn
->upstream_port
;
150 BT_LIB_LOGI("Ending connection: %!+x, try-remove-from-graph=%d",
151 conn
, try_remove_from_graph
);
154 * Any of the following message callback functions could
155 * remove one of the connection's ports from its component. To
156 * make sure that at least logging in called functions works
157 * with existing objects, get a local reference on both ports.
159 bt_object_get_ref(downstream_port
);
160 bt_object_get_ref(upstream_port
);
162 if (downstream_port
) {
163 BT_LIB_LOGD("Disconnecting connection's downstream port: %!+p",
165 bt_port_set_connection(downstream_port
, NULL
);
166 conn
->downstream_port
= NULL
;
170 BT_LIB_LOGD("Disconnecting connection's upstream port: %!+p",
172 bt_port_set_connection(upstream_port
, NULL
);
173 conn
->upstream_port
= NULL
;
177 * It is safe to put the local port references now that we don't
178 * need them anymore. This could indeed destroy them.
180 bt_object_put_ref(downstream_port
);
181 bt_object_put_ref(upstream_port
);
184 * Because this connection is ended, finalize each message
185 * iterator created from it.
187 * In practice, this only happens when the connection is
188 * destroyed and not all its message iterators were finalized,
189 * which is on graph destruction.
191 for (i
= 0; i
< conn
->iterators
->len
; i
++) {
192 struct bt_message_iterator
*iterator
=
193 g_ptr_array_index(conn
->iterators
, i
);
195 BT_LIB_LOGD("Finalizing message iterator created by "
196 "this ended connection: %![iter-]+i", iterator
);
197 bt_message_iterator_try_finalize(
201 * Make sure this iterator does not try to remove itself
202 * from this connection's iterators on destruction
203 * because this connection won't exist anymore.
205 bt_message_iterator_set_connection(
209 g_ptr_array_set_size(conn
->iterators
, 0);
211 if (try_remove_from_graph
) {
212 try_remove_connection_from_graph(conn
);
216 const struct bt_port_output
*bt_connection_borrow_upstream_port_const(
217 const struct bt_connection
*connection
)
219 BT_ASSERT_PRE_DEV_NON_NULL(connection
, "Connection");
220 return (void *) connection
->upstream_port
;
223 const struct bt_port_input
*bt_connection_borrow_downstream_port_const(
224 const struct bt_connection
*connection
)
226 BT_ASSERT_PRE_DEV_NON_NULL(connection
, "Connection");
227 return (void *) connection
->downstream_port
;
231 void bt_connection_remove_iterator(struct bt_connection
*conn
,
232 struct bt_message_iterator
*iterator
)
234 g_ptr_array_remove(conn
->iterators
, iterator
);
235 BT_LIB_LOGD("Removed message iterator from connection: "
236 "%![conn-]+x, %![iter-]+i", conn
, iterator
);
237 try_remove_connection_from_graph(conn
);
240 void bt_connection_get_ref(const struct bt_connection
*connection
)
242 bt_object_get_ref(connection
);
245 void bt_connection_put_ref(const struct bt_connection
*connection
)
247 bt_object_put_ref(connection
);
This page took 0.034419 seconds and 5 git commands to generate.