2 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #define BT_LOG_TAG "CONNECTION"
26 #include <babeltrace/lib-logging-internal.h>
28 #include <babeltrace/graph/notification-iterator-internal.h>
29 #include <babeltrace/graph/component-internal.h>
30 #include <babeltrace/graph/connection-internal.h>
31 #include <babeltrace/graph/graph-internal.h>
32 #include <babeltrace/graph/port-internal.h>
33 #include <babeltrace/object-internal.h>
34 #include <babeltrace/compiler-internal.h>
35 #include <babeltrace/object.h>
36 #include <babeltrace/assert-internal.h>
37 #include <babeltrace/assert-pre-internal.h>
42 void destroy_connection(struct bt_object
*obj
)
44 struct bt_connection
*connection
= container_of(obj
,
45 struct bt_connection
, base
);
47 BT_LIB_LOGD("Destroying connection: %!+x", connection
);
50 * Make sure that each notification iterator which was created
51 * for this connection is finalized before we destroy it. Once a
52 * notification iterator is finalized, all its method return
53 * NULL or the BT_NOTIFICATION_ITERATOR_STATUS_CANCELED status.
55 * Because connections are destroyed before components within a
56 * graph, this ensures that notification iterators are always
57 * finalized before their upstream component.
59 * Ending the connection does exactly this. We pass `false` to
60 * bt_connection_end() here to avoid removing this connection
61 * from the graph: if we're here, we're already in the graph's
64 bt_connection_end(connection
, false);
65 g_ptr_array_free(connection
->iterators
, TRUE
);
66 connection
->iterators
= NULL
;
69 * No bt_object_put_ref on ports as a connection only holds _weak_
76 void try_remove_connection_from_graph(struct bt_connection
*connection
)
78 void *graph
= (void *) bt_object_borrow_parent(&connection
->base
);
80 if (connection
->base
.ref_count
> 0 ||
81 connection
->downstream_port
||
82 connection
->upstream_port
||
83 connection
->iterators
->len
> 0) {
88 * At this point we know that:
90 * 1. The connection is ended (ports were disconnected).
91 * 2. All the notification iterators that this connection
92 * created, if any, are finalized.
93 * 3. The connection's reference count is 0, so only the
94 * parent (graph) owns this connection after this call.
96 * In other words, no other object than the graph knows this
99 * It is safe to remove the connection from the graph, therefore
102 BT_LIB_LOGD("Removing self from graph's connections: "
103 "%![graph-]+g, %![conn-]+x", graph
, connection
);
104 bt_graph_remove_connection(graph
, connection
);
108 void parent_is_owner(struct bt_object
*obj
)
110 struct bt_connection
*connection
= container_of(obj
,
111 struct bt_connection
, base
);
113 try_remove_connection_from_graph(connection
);
117 struct bt_connection
*bt_connection_create(struct bt_graph
*graph
,
118 struct bt_port
*upstream_port
,
119 struct bt_port
*downstream_port
)
121 struct bt_connection
*connection
= NULL
;
123 BT_LIB_LOGD("Creating connection: "
124 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
125 graph
, upstream_port
, downstream_port
);
126 connection
= g_new0(struct bt_connection
, 1);
128 BT_LOGE_STR("Failed to allocate one connection.");
132 bt_object_init_shared_with_parent(&connection
->base
,
134 bt_object_set_parent_is_owner_listener_func(&connection
->base
,
136 connection
->iterators
= g_ptr_array_new();
137 if (!connection
->iterators
) {
138 BT_LOGE_STR("Failed to allocate a GPtrArray.");
139 BT_OBJECT_PUT_REF_AND_RESET(connection
);
143 /* Weak references are taken, see comment in header. */
144 connection
->upstream_port
= upstream_port
;
145 connection
->downstream_port
= downstream_port
;
146 BT_LIB_LOGD("Setting upstream port's connection: %!+p", upstream_port
);
147 bt_port_set_connection(upstream_port
, connection
);
148 BT_LIB_LOGD("Setting downstream port's connection: %!+p",
150 bt_port_set_connection(downstream_port
, connection
);
151 bt_object_set_parent(&connection
->base
, &graph
->base
);
152 BT_LIB_LOGD("Created connection: %!+x", connection
);
159 void bt_connection_end(struct bt_connection
*conn
, bool try_remove_from_graph
)
161 struct bt_component
*downstream_comp
= NULL
;
162 struct bt_component
*upstream_comp
= NULL
;
163 struct bt_port
*downstream_port
= conn
->downstream_port
;
164 struct bt_port
*upstream_port
= conn
->upstream_port
;
165 struct bt_graph
*graph
= bt_connection_borrow_graph(conn
);
168 BT_LIB_LOGD("Ending connection: %!+x, try-remove-from-graph=%d",
169 conn
, try_remove_from_graph
);
172 * Any of the following notification callback functions could
173 * remove one of the connection's ports from its component. To
174 * make sure that at least logging in called functions works
175 * with existing objects, get a local reference on both ports.
177 bt_object_get_ref(downstream_port
);
178 bt_object_get_ref(upstream_port
);
180 if (downstream_port
) {
181 BT_LIB_LOGD("Disconnecting connection's downstream port: %!+p",
183 downstream_comp
= bt_port_borrow_component(downstream_port
);
184 bt_port_set_connection(downstream_port
, NULL
);
185 conn
->downstream_port
= NULL
;
189 BT_LIB_LOGD("Disconnecting connection's upstream port: %!+p",
191 upstream_comp
= bt_port_borrow_component(upstream_port
);
192 bt_port_set_connection(upstream_port
, NULL
);
193 conn
->upstream_port
= NULL
;
196 if (downstream_comp
&& conn
->notified_downstream_port_connected
&&
197 !conn
->notified_downstream_port_disconnected
) {
198 /* bt_component_port_disconnected() logs details */
199 bt_component_port_disconnected(downstream_comp
,
201 conn
->notified_downstream_port_disconnected
= true;
204 if (upstream_comp
&& conn
->notified_upstream_port_connected
&&
205 !conn
->notified_upstream_port_disconnected
) {
206 /* bt_component_port_disconnected() logs details */
207 bt_component_port_disconnected(upstream_comp
, upstream_port
);
208 conn
->notified_upstream_port_disconnected
= true;
213 if (conn
->notified_graph_ports_connected
&&
214 !conn
->notified_graph_ports_disconnected
) {
215 /* bt_graph_notify_ports_disconnected() logs details */
216 bt_graph_notify_ports_disconnected(graph
, upstream_comp
,
217 downstream_comp
, upstream_port
, downstream_port
);
218 conn
->notified_graph_ports_disconnected
= true;
222 * It is safe to put the local port references now that we don't
223 * need them anymore. This could indeed destroy them.
225 bt_object_put_ref(downstream_port
);
226 bt_object_put_ref(upstream_port
);
229 * Because this connection is ended, finalize (cancel) each
230 * notification iterator created from it.
232 for (i
= 0; i
< conn
->iterators
->len
; i
++) {
233 struct bt_self_component_port_input_notification_iterator
*iterator
=
234 g_ptr_array_index(conn
->iterators
, i
);
236 BT_LIB_LOGD("Finalizing notification iterator created by "
237 "this ended connection: %![iter-]+i", iterator
);
238 bt_self_component_port_input_notification_iterator_finalize(
242 * Make sure this iterator does not try to remove itself
243 * from this connection's iterators on destruction
244 * because this connection won't exist anymore.
246 bt_self_component_port_input_notification_iterator_set_connection(
250 g_ptr_array_set_size(conn
->iterators
, 0);
252 if (try_remove_from_graph
) {
253 try_remove_connection_from_graph(conn
);
257 struct bt_port_output
*bt_connection_borrow_upstream_port(
258 struct bt_connection
*connection
)
260 BT_ASSERT_PRE_NON_NULL(connection
, "Connection");
261 return (void *) connection
->upstream_port
;
264 struct bt_port_input
*bt_connection_borrow_downstream_port(
265 struct bt_connection
*connection
)
267 BT_ASSERT_PRE_NON_NULL(connection
, "Connection");
268 return (void *) connection
->downstream_port
;
272 void bt_connection_remove_iterator(struct bt_connection
*conn
,
273 struct bt_self_component_port_input_notification_iterator
*iterator
)
275 g_ptr_array_remove(conn
->iterators
, iterator
);
276 BT_LIB_LOGV("Removed notification iterator from connection: "
277 "%![conn-]+x, %![iter-]+i", conn
, iterator
);
278 try_remove_connection_from_graph(conn
);
281 bt_bool
bt_connection_is_ended(struct bt_connection
*connection
)
283 return !connection
->downstream_port
&& !connection
->upstream_port
;