da4429d038f639d3e74894c7f24d64ac42ea2da8
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 "lib/assert-cond.h"
12 #include <babeltrace2/graph/connection.h>
13 #include "lib/object.h"
14 #include "compat/compiler.h"
19 #include "component.h"
20 #include "connection.h"
22 #include "message/iterator.h"
26 void destroy_connection(struct bt_object
*obj
)
28 struct bt_connection
*connection
= container_of(obj
,
29 struct bt_connection
, base
);
31 BT_LIB_LOGI("Destroying connection: %!+x", connection
);
34 * Make sure that each message iterator which was created for
35 * this connection is finalized before we destroy it. Once a
36 * message iterator is finalized, you cannot use it.
38 * Because connections are destroyed before components within a
39 * graph, this ensures that message iterators are always
40 * finalized before their upstream component.
42 * Ending the connection does exactly this. We pass `false` to
43 * bt_connection_end() here to avoid removing this connection
44 * from the graph: if we're here, we're already in the graph's
47 bt_connection_end(connection
, false);
48 g_ptr_array_free(connection
->iterators
, TRUE
);
49 connection
->iterators
= NULL
;
52 * No bt_object_put_ref on ports as a connection only holds _weak_
59 void try_remove_connection_from_graph(struct bt_connection
*connection
)
61 void *graph
= (void *) bt_object_borrow_parent(&connection
->base
);
63 if (connection
->base
.ref_count
> 0 ||
64 connection
->downstream_port
||
65 connection
->upstream_port
||
66 connection
->iterators
->len
> 0) {
71 * At this point we know that:
73 * 1. The connection is ended (ports were disconnected).
74 * 2. All the message iterators that this connection
75 * created, if any, are finalized.
76 * 3. The connection's reference count is 0, so only the
77 * parent (graph) owns this connection after this call.
79 * In other words, no other object than the graph knows this
82 * It is safe to remove the connection from the graph, therefore
85 BT_LIB_LOGD("Removing self from graph's connections: "
86 "%![graph-]+g, %![conn-]+x", graph
, connection
);
87 bt_graph_remove_connection(graph
, connection
);
91 void parent_is_owner(struct bt_object
*obj
)
93 struct bt_connection
*connection
= container_of(obj
,
94 struct bt_connection
, base
);
96 try_remove_connection_from_graph(connection
);
99 struct bt_connection
*bt_connection_create(struct bt_graph
*graph
,
100 struct bt_port
*upstream_port
,
101 struct bt_port
*downstream_port
)
103 struct bt_connection
*connection
= NULL
;
105 BT_LIB_LOGI("Creating connection: "
106 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
107 graph
, upstream_port
, downstream_port
);
108 connection
= g_new0(struct bt_connection
, 1);
110 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one connection.");
114 bt_object_init_shared_with_parent(&connection
->base
,
116 bt_object_set_parent_is_owner_listener_func(&connection
->base
,
118 connection
->iterators
= g_ptr_array_new();
119 if (!connection
->iterators
) {
120 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
121 BT_OBJECT_PUT_REF_AND_RESET(connection
);
125 /* Weak references are taken, see comment in header. */
126 connection
->upstream_port
= upstream_port
;
127 connection
->downstream_port
= downstream_port
;
128 BT_LIB_LOGD("Setting upstream port's connection: %!+p", upstream_port
);
129 bt_port_set_connection(upstream_port
, connection
);
130 BT_LIB_LOGD("Setting downstream port's connection: %!+p",
132 bt_port_set_connection(downstream_port
, connection
);
133 bt_object_set_parent(&connection
->base
, &graph
->base
);
134 BT_LIB_LOGI("Created connection: %!+x", connection
);
140 void bt_connection_end(struct bt_connection
*conn
, bool try_remove_from_graph
)
142 struct bt_port
*downstream_port
= conn
->downstream_port
;
143 struct bt_port
*upstream_port
= conn
->upstream_port
;
146 BT_LIB_LOGI("Ending connection: %!+x, try-remove-from-graph=%d",
147 conn
, try_remove_from_graph
);
150 * Any of the following message callback functions could
151 * remove one of the connection's ports from its component. To
152 * make sure that at least logging in called functions works
153 * with existing objects, get a local reference on both ports.
155 bt_object_get_ref(downstream_port
);
156 bt_object_get_ref(upstream_port
);
158 if (downstream_port
) {
159 BT_LIB_LOGD("Disconnecting connection's downstream port: %!+p",
161 bt_port_set_connection(downstream_port
, NULL
);
162 conn
->downstream_port
= NULL
;
166 BT_LIB_LOGD("Disconnecting connection's upstream port: %!+p",
168 bt_port_set_connection(upstream_port
, NULL
);
169 conn
->upstream_port
= NULL
;
173 * It is safe to put the local port references now that we don't
174 * need them anymore. This could indeed destroy them.
176 bt_object_put_ref(downstream_port
);
177 bt_object_put_ref(upstream_port
);
180 * Because this connection is ended, finalize each message
181 * iterator created from it.
183 * In practice, this only happens when the connection is
184 * destroyed and not all its message iterators were finalized,
185 * which is on graph destruction.
187 for (i
= 0; i
< conn
->iterators
->len
; i
++) {
188 struct bt_message_iterator
*iterator
=
189 g_ptr_array_index(conn
->iterators
, i
);
191 BT_LIB_LOGD("Finalizing message iterator created by "
192 "this ended connection: %![iter-]+i", iterator
);
193 bt_message_iterator_try_finalize(
197 * Make sure this iterator does not try to remove itself
198 * from this connection's iterators on destruction
199 * because this connection won't exist anymore.
201 bt_message_iterator_set_connection(
205 g_ptr_array_set_size(conn
->iterators
, 0);
207 if (try_remove_from_graph
) {
208 try_remove_connection_from_graph(conn
);
213 const struct bt_port_output
*bt_connection_borrow_upstream_port_const(
214 const struct bt_connection
*connection
)
216 BT_ASSERT_PRE_DEV_CONN_NON_NULL(connection
);
217 return (void *) connection
->upstream_port
;
221 const struct bt_port_input
*bt_connection_borrow_downstream_port_const(
222 const struct bt_connection
*connection
)
224 BT_ASSERT_PRE_DEV_CONN_NON_NULL(connection
);
225 return (void *) connection
->downstream_port
;
228 void bt_connection_remove_iterator(struct bt_connection
*conn
,
229 struct bt_message_iterator
*iterator
)
231 g_ptr_array_remove(conn
->iterators
, iterator
);
232 BT_LIB_LOGD("Removed message iterator from connection: "
233 "%![conn-]+x, %![iter-]+i", conn
, iterator
);
234 try_remove_connection_from_graph(conn
);
238 void bt_connection_get_ref(const struct bt_connection
*connection
)
240 bt_object_get_ref(connection
);
244 void bt_connection_put_ref(const struct bt_connection
*connection
)
246 bt_object_put_ref(connection
);
This page took 0.03442 seconds and 3 git commands to generate.