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-cond.h"
13 #include <babeltrace2/graph/connection.h>
14 #include "lib/object.h"
15 #include "compat/compiler.h"
20 #include "component.h"
21 #include "connection.h"
23 #include "message/iterator.h"
27 void destroy_connection(struct bt_object
*obj
)
29 struct bt_connection
*connection
= container_of(obj
,
30 struct bt_connection
, base
);
32 BT_LIB_LOGI("Destroying connection: %!+x", connection
);
35 * Make sure that each message iterator which was created for
36 * this connection is finalized before we destroy it. Once a
37 * message iterator is finalized, you cannot use it.
39 * Because connections are destroyed before components within a
40 * graph, this ensures that message iterators are always
41 * finalized before their upstream component.
43 * Ending the connection does exactly this. We pass `false` to
44 * bt_connection_end() here to avoid removing this connection
45 * from the graph: if we're here, we're already in the graph's
48 bt_connection_end(connection
, false);
49 g_ptr_array_free(connection
->iterators
, TRUE
);
50 connection
->iterators
= NULL
;
53 * No bt_object_put_ref on ports as a connection only holds _weak_
60 void try_remove_connection_from_graph(struct bt_connection
*connection
)
62 void *graph
= (void *) bt_object_borrow_parent(&connection
->base
);
64 if (connection
->base
.ref_count
> 0 ||
65 connection
->downstream_port
||
66 connection
->upstream_port
||
67 connection
->iterators
->len
> 0) {
72 * At this point we know that:
74 * 1. The connection is ended (ports were disconnected).
75 * 2. All the message iterators that this connection
76 * created, if any, are finalized.
77 * 3. The connection's reference count is 0, so only the
78 * parent (graph) owns this connection after this call.
80 * In other words, no other object than the graph knows this
83 * It is safe to remove the connection from the graph, therefore
86 BT_LIB_LOGD("Removing self from graph's connections: "
87 "%![graph-]+g, %![conn-]+x", graph
, connection
);
88 bt_graph_remove_connection(graph
, connection
);
92 void parent_is_owner(struct bt_object
*obj
)
94 struct bt_connection
*connection
= container_of(obj
,
95 struct bt_connection
, base
);
97 try_remove_connection_from_graph(connection
);
100 struct bt_connection
*bt_connection_create(struct bt_graph
*graph
,
101 struct bt_port
*upstream_port
,
102 struct bt_port
*downstream_port
)
104 struct bt_connection
*connection
= NULL
;
106 BT_LIB_LOGI("Creating connection: "
107 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
108 graph
, upstream_port
, downstream_port
);
109 connection
= g_new0(struct bt_connection
, 1);
111 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one connection.");
115 bt_object_init_shared_with_parent(&connection
->base
,
117 bt_object_set_parent_is_owner_listener_func(&connection
->base
,
119 connection
->iterators
= g_ptr_array_new();
120 if (!connection
->iterators
) {
121 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
122 BT_OBJECT_PUT_REF_AND_RESET(connection
);
126 /* Weak references are taken, see comment in header. */
127 connection
->upstream_port
= upstream_port
;
128 connection
->downstream_port
= downstream_port
;
129 BT_LIB_LOGD("Setting upstream port's connection: %!+p", upstream_port
);
130 bt_port_set_connection(upstream_port
, connection
);
131 BT_LIB_LOGD("Setting downstream port's connection: %!+p",
133 bt_port_set_connection(downstream_port
, connection
);
134 bt_object_set_parent(&connection
->base
, &graph
->base
);
135 BT_LIB_LOGI("Created connection: %!+x", connection
);
141 void bt_connection_end(struct bt_connection
*conn
, bool try_remove_from_graph
)
143 struct bt_port
*downstream_port
= conn
->downstream_port
;
144 struct bt_port
*upstream_port
= conn
->upstream_port
;
147 BT_LIB_LOGI("Ending connection: %!+x, try-remove-from-graph=%d",
148 conn
, try_remove_from_graph
);
151 * Any of the following message callback functions could
152 * remove one of the connection's ports from its component. To
153 * make sure that at least logging in called functions works
154 * with existing objects, get a local reference on both ports.
156 bt_object_get_ref(downstream_port
);
157 bt_object_get_ref(upstream_port
);
159 if (downstream_port
) {
160 BT_LIB_LOGD("Disconnecting connection's downstream port: %!+p",
162 bt_port_set_connection(downstream_port
, NULL
);
163 conn
->downstream_port
= NULL
;
167 BT_LIB_LOGD("Disconnecting connection's upstream port: %!+p",
169 bt_port_set_connection(upstream_port
, NULL
);
170 conn
->upstream_port
= NULL
;
174 * It is safe to put the local port references now that we don't
175 * need them anymore. This could indeed destroy them.
177 bt_object_put_ref(downstream_port
);
178 bt_object_put_ref(upstream_port
);
181 * Because this connection is ended, finalize each message
182 * iterator created from it.
184 * In practice, this only happens when the connection is
185 * destroyed and not all its message iterators were finalized,
186 * which is on graph destruction.
188 for (i
= 0; i
< conn
->iterators
->len
; i
++) {
189 struct bt_message_iterator
*iterator
=
190 g_ptr_array_index(conn
->iterators
, i
);
192 BT_LIB_LOGD("Finalizing message iterator created by "
193 "this ended connection: %![iter-]+i", iterator
);
194 bt_message_iterator_try_finalize(
198 * Make sure this iterator does not try to remove itself
199 * from this connection's iterators on destruction
200 * because this connection won't exist anymore.
202 bt_message_iterator_set_connection(
206 g_ptr_array_set_size(conn
->iterators
, 0);
208 if (try_remove_from_graph
) {
209 try_remove_connection_from_graph(conn
);
214 const struct bt_port_output
*bt_connection_borrow_upstream_port_const(
215 const struct bt_connection
*connection
)
217 BT_ASSERT_PRE_DEV_CONN_NON_NULL(connection
);
218 return (void *) connection
->upstream_port
;
222 const struct bt_port_input
*bt_connection_borrow_downstream_port_const(
223 const struct bt_connection
*connection
)
225 BT_ASSERT_PRE_DEV_CONN_NON_NULL(connection
);
226 return (void *) connection
->downstream_port
;
229 void bt_connection_remove_iterator(struct bt_connection
*conn
,
230 struct bt_message_iterator
*iterator
)
232 g_ptr_array_remove(conn
->iterators
, iterator
);
233 BT_LIB_LOGD("Removed message iterator from connection: "
234 "%![conn-]+x, %![iter-]+i", conn
, iterator
);
235 try_remove_connection_from_graph(conn
);
239 void bt_connection_get_ref(const struct bt_connection
*connection
)
241 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.035522 seconds and 5 git commands to generate.