4 * Babeltrace Connection
6 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/graph/notification-iterator-internal.h>
30 #include <babeltrace/graph/component-internal.h>
31 #include <babeltrace/graph/component-source-internal.h>
32 #include <babeltrace/graph/component-filter-internal.h>
33 #include <babeltrace/graph/connection-internal.h>
34 #include <babeltrace/graph/private-connection.h>
35 #include <babeltrace/graph/graph-internal.h>
36 #include <babeltrace/graph/port-internal.h>
37 #include <babeltrace/object-internal.h>
38 #include <babeltrace/compiler-internal.h>
42 void bt_connection_destroy(struct bt_object
*obj
)
44 struct bt_connection
*connection
= container_of(obj
,
45 struct bt_connection
, base
);
49 * Make sure that each notification iterator which was created
50 * for this connection is finalized before we destroy it. Once a
51 * notification iterator is finalized, all its method return
52 * NULL or the BT_NOTIFICATION_ITERATOR_STATUS_CANCELED status.
54 * Because connections are destroyed before components within a
55 * graph, this ensures that notification iterators are always
56 * finalized before their upstream component.
58 if (connection
->iterators
) {
59 for (i
= 0; i
< connection
->iterators
->len
; i
++) {
60 struct bt_notification_iterator
*iterator
=
61 g_ptr_array_index(connection
->iterators
, i
);
63 bt_notification_iterator_finalize(iterator
);
66 * Make sure this iterator does not try to
67 * remove itself from this connection's
68 * iterators on destruction because this
69 * connection won't exist anymore.
71 bt_notification_iterator_set_connection(iterator
,
75 g_ptr_array_free(connection
->iterators
, TRUE
);
79 * No bt_put on ports as a connection only holds _weak_ references
86 void bt_connection_try_remove_from_graph(struct bt_connection
*connection
)
88 void *graph
= bt_object_borrow_parent(&connection
->base
);
90 if (connection
->base
.ref_count
.count
> 0 ||
91 connection
->downstream_port
||
92 connection
->upstream_port
||
93 connection
->iterators
->len
> 0) {
98 * At this point we know that:
100 * 1. The connection is dead (ports were disconnected).
101 * 2. All the notification iterators that this connection
102 * created, if any, are finalized.
103 * 3. The connection's reference count is 0, so only the
104 * parent (graph) owns this connection after this call.
106 * In other words, no other object than the graph knows this
109 * It is safe to remove the connection from the graph, therefore
112 bt_graph_remove_connection(graph
, connection
);
116 void bt_connection_parent_is_owner(struct bt_object
*obj
)
118 struct bt_connection
*connection
= container_of(obj
,
119 struct bt_connection
, base
);
121 bt_connection_try_remove_from_graph(connection
);
124 struct bt_connection
*bt_connection_from_private_connection(
125 struct bt_private_connection
*private_connection
)
127 return bt_get(bt_connection_from_private(private_connection
));
131 struct bt_connection
*bt_connection_create(
132 struct bt_graph
*graph
,
133 struct bt_port
*upstream_port
,
134 struct bt_port
*downstream_port
)
136 struct bt_connection
*connection
= NULL
;
138 if (bt_port_get_type(upstream_port
) != BT_PORT_TYPE_OUTPUT
) {
141 if (bt_port_get_type(downstream_port
) != BT_PORT_TYPE_INPUT
) {
145 connection
= g_new0(struct bt_connection
, 1);
150 bt_object_init(connection
, bt_connection_destroy
);
151 bt_object_set_parent_is_owner_listener(connection
,
152 bt_connection_parent_is_owner
);
153 connection
->iterators
= g_ptr_array_new();
154 if (!connection
->iterators
) {
159 /* Weak references are taken, see comment in header. */
160 connection
->upstream_port
= upstream_port
;
161 connection
->downstream_port
= downstream_port
;
162 bt_port_set_connection(upstream_port
, connection
);
163 bt_port_set_connection(downstream_port
, connection
);
164 bt_object_set_parent(connection
, &graph
->base
);
170 void bt_connection_disconnect_ports(struct bt_connection
*conn
)
172 struct bt_component
*downstream_comp
= NULL
;
173 struct bt_component
*upstream_comp
= NULL
;
174 struct bt_port
*downstream_port
= conn
->downstream_port
;
175 struct bt_port
*upstream_port
= conn
->upstream_port
;
176 struct bt_graph
*graph
= (void *) bt_object_borrow_parent(conn
);
179 if (downstream_port
) {
180 downstream_comp
= bt_port_get_component(downstream_port
);
181 bt_port_set_connection(downstream_port
, NULL
);
182 conn
->downstream_port
= NULL
;
186 upstream_comp
= bt_port_get_component(upstream_port
);
187 bt_port_set_connection(upstream_port
, NULL
);
188 conn
->upstream_port
= NULL
;
191 if (downstream_comp
) {
192 bt_component_port_disconnected(downstream_comp
,
197 bt_component_port_disconnected(upstream_comp
, upstream_port
);
201 bt_graph_notify_ports_disconnected(graph
, upstream_comp
,
202 downstream_comp
, upstream_port
, downstream_port
);
203 bt_put(downstream_comp
);
204 bt_put(upstream_comp
);
207 * Because this connection is dead, finalize (cancel) each
208 * notification iterator created from it.
210 for (i
= 0; i
< conn
->iterators
->len
; i
++) {
211 struct bt_notification_iterator
*iterator
=
212 g_ptr_array_index(conn
->iterators
, i
);
214 bt_notification_iterator_finalize(iterator
);
217 * Make sure this iterator does not try to remove itself
218 * from this connection's iterators on destruction
219 * because this connection won't exist anymore.
221 bt_notification_iterator_set_connection(iterator
,
225 g_ptr_array_set_size(conn
->iterators
, 0);
226 bt_connection_try_remove_from_graph(conn
);
229 struct bt_port
*bt_connection_get_upstream_port(
230 struct bt_connection
*connection
)
232 return connection
? bt_get(connection
->upstream_port
) : NULL
;
235 struct bt_port
*bt_connection_get_downstream_port(
236 struct bt_connection
*connection
)
238 return connection
? bt_get(connection
->downstream_port
) : NULL
;
241 struct bt_notification_iterator
*
242 bt_private_connection_create_notification_iterator(
243 struct bt_private_connection
*private_connection
,
244 const enum bt_notification_type
*notification_types
)
246 enum bt_notification_iterator_status ret_iterator
;
247 enum bt_component_class_type upstream_comp_class_type
;
248 struct bt_notification_iterator
*iterator
= NULL
;
249 struct bt_port
*upstream_port
= NULL
;
250 struct bt_component
*upstream_component
= NULL
;
251 struct bt_component_class
*upstream_comp_class
= NULL
;
252 struct bt_connection
*connection
= NULL
;
253 bt_component_class_notification_iterator_init_method init_method
= NULL
;
254 static const enum bt_notification_type all_notif_types
[] = {
255 BT_NOTIFICATION_TYPE_ALL
,
256 BT_NOTIFICATION_TYPE_SENTINEL
,
259 if (!private_connection
) {
263 if (!notification_types
) {
264 notification_types
= all_notif_types
;
267 connection
= bt_connection_from_private(private_connection
);
268 if (!connection
->upstream_port
|| !connection
->downstream_port
) {
272 upstream_port
= connection
->upstream_port
;
273 assert(upstream_port
);
274 upstream_component
= bt_port_get_component(upstream_port
);
275 assert(upstream_component
);
276 upstream_comp_class
= upstream_component
->class;
278 if (!upstream_component
) {
282 upstream_comp_class_type
=
283 bt_component_get_class_type(upstream_component
);
284 if (upstream_comp_class_type
!= BT_COMPONENT_CLASS_TYPE_SOURCE
&&
285 upstream_comp_class_type
!= BT_COMPONENT_CLASS_TYPE_FILTER
) {
286 /* Unsupported operation. */
290 iterator
= bt_notification_iterator_create(upstream_component
,
291 upstream_port
, notification_types
, connection
);
296 switch (upstream_comp_class_type
) {
297 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
299 struct bt_component_class_source
*source_class
=
300 container_of(upstream_comp_class
,
301 struct bt_component_class_source
, parent
);
302 init_method
= source_class
->methods
.iterator
.init
;
305 case BT_COMPONENT_CLASS_TYPE_FILTER
:
307 struct bt_component_class_filter
*filter_class
=
308 container_of(upstream_comp_class
,
309 struct bt_component_class_filter
, parent
);
310 init_method
= filter_class
->methods
.iterator
.init
;
319 enum bt_notification_iterator_status status
= init_method(
320 bt_private_notification_iterator_from_notification_iterator(iterator
),
321 bt_private_port_from_port(upstream_port
));
327 ret_iterator
= bt_notification_iterator_validate(iterator
);
328 if (ret_iterator
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
332 g_ptr_array_add(connection
->iterators
, iterator
);
339 bt_put(upstream_component
);
344 void bt_connection_remove_iterator(struct bt_connection
*conn
,
345 struct bt_notification_iterator
*iterator
)
347 g_ptr_array_remove(conn
->iterators
, iterator
);
348 bt_connection_try_remove_from_graph(conn
);