Commit | Line | Data |
---|---|---|
784cdc68 | 1 | /* |
f2b0325d | 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
784cdc68 JG |
3 | * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
4 | * | |
784cdc68 JG |
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
6 | * of this software and associated documentation files (the "Software"), to deal | |
7 | * in the Software without restriction, including without limitation the rights | |
8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | * copies of the Software, and to permit persons to whom the Software is | |
10 | * furnished to do so, subject to the following conditions: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be included in | |
13 | * all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
21 | * SOFTWARE. | |
22 | */ | |
23 | ||
a36bfb16 PP |
24 | #define BT_LOG_TAG "CONNECTION" |
25 | #include <babeltrace/lib-logging-internal.h> | |
26 | ||
b2e0c907 PP |
27 | #include <babeltrace/graph/notification-iterator-internal.h> |
28 | #include <babeltrace/graph/component-internal.h> | |
b2e0c907 | 29 | #include <babeltrace/graph/connection-internal.h> |
7b53201c | 30 | #include <babeltrace/graph/connection-const.h> |
b2e0c907 PP |
31 | #include <babeltrace/graph/graph-internal.h> |
32 | #include <babeltrace/graph/port-internal.h> | |
784cdc68 | 33 | #include <babeltrace/object-internal.h> |
3d9990ac | 34 | #include <babeltrace/compiler-internal.h> |
834e9996 | 35 | #include <babeltrace/object.h> |
8b45963b | 36 | #include <babeltrace/assert-internal.h> |
834e9996 | 37 | #include <babeltrace/assert-pre-internal.h> |
0fbb9a9f | 38 | #include <stdlib.h> |
784cdc68 JG |
39 | #include <glib.h> |
40 | ||
41 | static | |
834e9996 | 42 | void destroy_connection(struct bt_object *obj) |
784cdc68 JG |
43 | { |
44 | struct bt_connection *connection = container_of(obj, | |
45 | struct bt_connection, base); | |
bd14d768 | 46 | |
834e9996 | 47 | BT_LIB_LOGD("Destroying connection: %!+x", connection); |
a36bfb16 | 48 | |
bd14d768 PP |
49 | /* |
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. | |
54 | * | |
55 | * Because connections are destroyed before components within a | |
56 | * graph, this ensures that notification iterators are always | |
57 | * finalized before their upstream component. | |
c42ea0af PP |
58 | * |
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 | |
62 | * destructor. | |
bd14d768 | 63 | */ |
c42ea0af PP |
64 | bt_connection_end(connection, false); |
65 | g_ptr_array_free(connection->iterators, TRUE); | |
834e9996 | 66 | connection->iterators = NULL; |
784cdc68 JG |
67 | |
68 | /* | |
8138bfe1 | 69 | * No bt_object_put_ref on ports as a connection only holds _weak_ |
c42ea0af | 70 | * references to them. |
784cdc68 JG |
71 | */ |
72 | g_free(connection); | |
73 | } | |
74 | ||
f167d3c0 | 75 | static |
834e9996 | 76 | void try_remove_connection_from_graph(struct bt_connection *connection) |
f167d3c0 | 77 | { |
1d7bf349 | 78 | void *graph = (void *) bt_object_borrow_parent(&connection->base); |
f167d3c0 | 79 | |
1d7bf349 | 80 | if (connection->base.ref_count > 0 || |
f167d3c0 PP |
81 | connection->downstream_port || |
82 | connection->upstream_port || | |
83 | connection->iterators->len > 0) { | |
84 | return; | |
85 | } | |
86 | ||
87 | /* | |
88 | * At this point we know that: | |
89 | * | |
a36bfb16 | 90 | * 1. The connection is ended (ports were disconnected). |
f167d3c0 PP |
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. | |
95 | * | |
96 | * In other words, no other object than the graph knows this | |
97 | * connection. | |
98 | * | |
99 | * It is safe to remove the connection from the graph, therefore | |
100 | * destroying it. | |
101 | */ | |
834e9996 PP |
102 | BT_LIB_LOGD("Removing self from graph's connections: " |
103 | "%![graph-]+g, %![conn-]+x", graph, connection); | |
f167d3c0 PP |
104 | bt_graph_remove_connection(graph, connection); |
105 | } | |
106 | ||
107 | static | |
834e9996 | 108 | void parent_is_owner(struct bt_object *obj) |
f167d3c0 PP |
109 | { |
110 | struct bt_connection *connection = container_of(obj, | |
111 | struct bt_connection, base); | |
112 | ||
834e9996 | 113 | try_remove_connection_from_graph(connection); |
890882ef PP |
114 | } |
115 | ||
784cdc68 | 116 | BT_HIDDEN |
834e9996 | 117 | struct bt_connection *bt_connection_create(struct bt_graph *graph, |
72b913fb PP |
118 | struct bt_port *upstream_port, |
119 | struct bt_port *downstream_port) | |
784cdc68 JG |
120 | { |
121 | struct bt_connection *connection = NULL; | |
122 | ||
834e9996 PP |
123 | BT_LIB_LOGD("Creating connection: " |
124 | "%![graph-]+g, %![up-port-]+p, %![down-port-]+p", | |
125 | graph, upstream_port, downstream_port); | |
784cdc68 JG |
126 | connection = g_new0(struct bt_connection, 1); |
127 | if (!connection) { | |
a36bfb16 | 128 | BT_LOGE_STR("Failed to allocate one connection."); |
784cdc68 JG |
129 | goto end; |
130 | } | |
131 | ||
1d7bf349 | 132 | bt_object_init_shared_with_parent(&connection->base, |
834e9996 | 133 | destroy_connection); |
1d7bf349 | 134 | bt_object_set_parent_is_owner_listener_func(&connection->base, |
834e9996 | 135 | parent_is_owner); |
bd14d768 PP |
136 | connection->iterators = g_ptr_array_new(); |
137 | if (!connection->iterators) { | |
a36bfb16 | 138 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
8138bfe1 | 139 | BT_OBJECT_PUT_REF_AND_RESET(connection); |
bd14d768 PP |
140 | goto end; |
141 | } | |
142 | ||
784cdc68 | 143 | /* Weak references are taken, see comment in header. */ |
72b913fb PP |
144 | connection->upstream_port = upstream_port; |
145 | connection->downstream_port = downstream_port; | |
834e9996 | 146 | BT_LIB_LOGD("Setting upstream port's connection: %!+p", upstream_port); |
72b913fb | 147 | bt_port_set_connection(upstream_port, connection); |
834e9996 PP |
148 | BT_LIB_LOGD("Setting downstream port's connection: %!+p", |
149 | downstream_port); | |
72b913fb | 150 | bt_port_set_connection(downstream_port, connection); |
1d7bf349 | 151 | bt_object_set_parent(&connection->base, &graph->base); |
834e9996 | 152 | BT_LIB_LOGD("Created connection: %!+x", connection); |
a36bfb16 | 153 | |
784cdc68 JG |
154 | end: |
155 | return connection; | |
156 | } | |
157 | ||
72b913fb | 158 | BT_HIDDEN |
834e9996 | 159 | void bt_connection_end(struct bt_connection *conn, bool try_remove_from_graph) |
72b913fb PP |
160 | { |
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; | |
c42ea0af | 165 | struct bt_graph *graph = bt_connection_borrow_graph(conn); |
f167d3c0 | 166 | size_t i; |
72b913fb | 167 | |
834e9996 | 168 | BT_LIB_LOGD("Ending connection: %!+x, try-remove-from-graph=%d", |
c42ea0af PP |
169 | conn, try_remove_from_graph); |
170 | ||
834e9996 PP |
171 | /* |
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. | |
176 | */ | |
177 | bt_object_get_ref(downstream_port); | |
178 | bt_object_get_ref(upstream_port); | |
179 | ||
72b913fb | 180 | if (downstream_port) { |
834e9996 PP |
181 | BT_LIB_LOGD("Disconnecting connection's downstream port: %!+p", |
182 | downstream_port); | |
7b53201c PP |
183 | downstream_comp = bt_port_borrow_component_inline( |
184 | downstream_port); | |
72b913fb PP |
185 | bt_port_set_connection(downstream_port, NULL); |
186 | conn->downstream_port = NULL; | |
187 | } | |
188 | ||
189 | if (upstream_port) { | |
834e9996 PP |
190 | BT_LIB_LOGD("Disconnecting connection's upstream port: %!+p", |
191 | upstream_port); | |
7b53201c PP |
192 | upstream_comp = bt_port_borrow_component_inline( |
193 | upstream_port); | |
72b913fb PP |
194 | bt_port_set_connection(upstream_port, NULL); |
195 | conn->upstream_port = NULL; | |
196 | } | |
197 | ||
834e9996 PP |
198 | if (downstream_comp && conn->notified_downstream_port_connected && |
199 | !conn->notified_downstream_port_disconnected) { | |
a36bfb16 | 200 | /* bt_component_port_disconnected() logs details */ |
72b913fb PP |
201 | bt_component_port_disconnected(downstream_comp, |
202 | downstream_port); | |
834e9996 | 203 | conn->notified_downstream_port_disconnected = true; |
72b913fb PP |
204 | } |
205 | ||
834e9996 PP |
206 | if (upstream_comp && conn->notified_upstream_port_connected && |
207 | !conn->notified_upstream_port_disconnected) { | |
a36bfb16 | 208 | /* bt_component_port_disconnected() logs details */ |
72b913fb | 209 | bt_component_port_disconnected(upstream_comp, upstream_port); |
834e9996 | 210 | conn->notified_upstream_port_disconnected = true; |
72b913fb PP |
211 | } |
212 | ||
8b45963b | 213 | BT_ASSERT(graph); |
634f394c | 214 | |
834e9996 PP |
215 | if (conn->notified_graph_ports_connected && |
216 | !conn->notified_graph_ports_disconnected) { | |
634f394c PP |
217 | /* bt_graph_notify_ports_disconnected() logs details */ |
218 | bt_graph_notify_ports_disconnected(graph, upstream_comp, | |
219 | downstream_comp, upstream_port, downstream_port); | |
834e9996 | 220 | conn->notified_graph_ports_disconnected = true; |
634f394c PP |
221 | } |
222 | ||
834e9996 PP |
223 | /* |
224 | * It is safe to put the local port references now that we don't | |
225 | * need them anymore. This could indeed destroy them. | |
226 | */ | |
227 | bt_object_put_ref(downstream_port); | |
228 | bt_object_put_ref(upstream_port); | |
f167d3c0 PP |
229 | |
230 | /* | |
a36bfb16 | 231 | * Because this connection is ended, finalize (cancel) each |
f167d3c0 PP |
232 | * notification iterator created from it. |
233 | */ | |
234 | for (i = 0; i < conn->iterators->len; i++) { | |
834e9996 | 235 | struct bt_self_component_port_input_notification_iterator *iterator = |
f167d3c0 PP |
236 | g_ptr_array_index(conn->iterators, i); |
237 | ||
834e9996 PP |
238 | BT_LIB_LOGD("Finalizing notification iterator created by " |
239 | "this ended connection: %![iter-]+i", iterator); | |
240 | bt_self_component_port_input_notification_iterator_finalize( | |
241 | iterator); | |
f167d3c0 PP |
242 | |
243 | /* | |
244 | * Make sure this iterator does not try to remove itself | |
245 | * from this connection's iterators on destruction | |
246 | * because this connection won't exist anymore. | |
247 | */ | |
834e9996 | 248 | bt_self_component_port_input_notification_iterator_set_connection( |
fe7265b5 | 249 | iterator, NULL); |
f167d3c0 PP |
250 | } |
251 | ||
252 | g_ptr_array_set_size(conn->iterators, 0); | |
c42ea0af PP |
253 | |
254 | if (try_remove_from_graph) { | |
834e9996 | 255 | try_remove_connection_from_graph(conn); |
c42ea0af | 256 | } |
72b913fb PP |
257 | } |
258 | ||
7b53201c PP |
259 | const struct bt_port_output *bt_connection_borrow_upstream_port_const( |
260 | const struct bt_connection *connection) | |
784cdc68 | 261 | { |
834e9996 PP |
262 | BT_ASSERT_PRE_NON_NULL(connection, "Connection"); |
263 | return (void *) connection->upstream_port; | |
784cdc68 JG |
264 | } |
265 | ||
7b53201c PP |
266 | const struct bt_port_input *bt_connection_borrow_downstream_port_const( |
267 | const struct bt_connection *connection) | |
784cdc68 | 268 | { |
834e9996 PP |
269 | BT_ASSERT_PRE_NON_NULL(connection, "Connection"); |
270 | return (void *) connection->downstream_port; | |
784cdc68 | 271 | } |
bd14d768 PP |
272 | |
273 | BT_HIDDEN | |
274 | void bt_connection_remove_iterator(struct bt_connection *conn, | |
834e9996 | 275 | struct bt_self_component_port_input_notification_iterator *iterator) |
bd14d768 PP |
276 | { |
277 | g_ptr_array_remove(conn->iterators, iterator); | |
834e9996 PP |
278 | BT_LIB_LOGV("Removed notification iterator from connection: " |
279 | "%![conn-]+x, %![iter-]+i", conn, iterator); | |
280 | try_remove_connection_from_graph(conn); | |
bd14d768 | 281 | } |
090a4c0a | 282 | |
7b53201c | 283 | bt_bool bt_connection_is_ended(const struct bt_connection *connection) |
090a4c0a PP |
284 | { |
285 | return !connection->downstream_port && !connection->upstream_port; | |
286 | } |