Rename: bt_put(), bt_get() -> bt_object_put_ref(), bt_object_get_ref()
[babeltrace.git] / lib / graph / connection.c
1 /*
2 * connection.c
3 *
4 * Babeltrace Connection
5 *
6 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
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
26 * SOFTWARE.
27 */
28
29 #define BT_LOG_TAG "CONNECTION"
30 #include <babeltrace/lib-logging-internal.h>
31
32 #include <babeltrace/graph/notification-iterator-internal.h>
33 #include <babeltrace/graph/component-internal.h>
34 #include <babeltrace/graph/component-source-internal.h>
35 #include <babeltrace/graph/component-filter-internal.h>
36 #include <babeltrace/graph/connection-internal.h>
37 #include <babeltrace/graph/private-connection.h>
38 #include <babeltrace/graph/graph-internal.h>
39 #include <babeltrace/graph/port-internal.h>
40 #include <babeltrace/object-internal.h>
41 #include <babeltrace/compiler-internal.h>
42 #include <babeltrace/assert-internal.h>
43 #include <stdlib.h>
44 #include <glib.h>
45
46 static
47 void bt_connection_destroy(struct bt_object *obj)
48 {
49 struct bt_connection *connection = container_of(obj,
50 struct bt_connection, base);
51
52 BT_LOGD("Destroying connection: addr=%p", connection);
53
54 /*
55 * Make sure that each notification iterator which was created
56 * for this connection is finalized before we destroy it. Once a
57 * notification iterator is finalized, all its method return
58 * NULL or the BT_NOTIFICATION_ITERATOR_STATUS_CANCELED status.
59 *
60 * Because connections are destroyed before components within a
61 * graph, this ensures that notification iterators are always
62 * finalized before their upstream component.
63 *
64 * Ending the connection does exactly this. We pass `false` to
65 * bt_connection_end() here to avoid removing this connection
66 * from the graph: if we're here, we're already in the graph's
67 * destructor.
68 */
69 bt_connection_end(connection, false);
70 g_ptr_array_free(connection->iterators, TRUE);
71
72 /*
73 * No bt_object_put_ref on ports as a connection only holds _weak_
74 * references to them.
75 */
76 g_free(connection);
77 }
78
79 static
80 void bt_connection_try_remove_from_graph(struct bt_connection *connection)
81 {
82 void *graph = (void *) bt_object_borrow_parent(&connection->base);
83
84 if (connection->base.ref_count > 0 ||
85 connection->downstream_port ||
86 connection->upstream_port ||
87 connection->iterators->len > 0) {
88 return;
89 }
90
91 /*
92 * At this point we know that:
93 *
94 * 1. The connection is ended (ports were disconnected).
95 * 2. All the notification iterators that this connection
96 * created, if any, are finalized.
97 * 3. The connection's reference count is 0, so only the
98 * parent (graph) owns this connection after this call.
99 *
100 * In other words, no other object than the graph knows this
101 * connection.
102 *
103 * It is safe to remove the connection from the graph, therefore
104 * destroying it.
105 */
106 BT_LOGD("Removing self from graph's connections: "
107 "graph-addr=%p, conn-addr=%p", graph, connection);
108 bt_graph_remove_connection(graph, connection);
109 }
110
111 static
112 void bt_connection_parent_is_owner(struct bt_object *obj)
113 {
114 struct bt_connection *connection = container_of(obj,
115 struct bt_connection, base);
116
117 bt_connection_try_remove_from_graph(connection);
118 }
119
120 struct bt_connection *bt_connection_borrow_from_private(
121 struct bt_private_connection *private_connection)
122 {
123 return (void *) private_connection;
124 }
125
126 BT_HIDDEN
127 struct bt_connection *bt_connection_create(
128 struct bt_graph *graph,
129 struct bt_port *upstream_port,
130 struct bt_port *downstream_port)
131 {
132 struct bt_connection *connection = NULL;
133
134 if (bt_port_get_type(upstream_port) != BT_PORT_TYPE_OUTPUT) {
135 BT_LOGW_STR("Invalid parameter: upstream port is not an output port.");
136 goto end;
137 }
138 if (bt_port_get_type(downstream_port) != BT_PORT_TYPE_INPUT) {
139 BT_LOGW_STR("Invalid parameter: downstream port is not an input port.");
140 goto end;
141 }
142
143 BT_LOGD("Creating connection: "
144 "graph-addr=%p, upstream-port-addr=%p, uptream-port-name=\"%s\", "
145 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
146 graph, upstream_port, bt_port_get_name(upstream_port),
147 downstream_port, bt_port_get_name(downstream_port));
148 connection = g_new0(struct bt_connection, 1);
149 if (!connection) {
150 BT_LOGE_STR("Failed to allocate one connection.");
151 goto end;
152 }
153
154 bt_object_init_shared_with_parent(&connection->base,
155 bt_connection_destroy);
156 bt_object_set_parent_is_owner_listener_func(&connection->base,
157 bt_connection_parent_is_owner);
158 connection->iterators = g_ptr_array_new();
159 if (!connection->iterators) {
160 BT_LOGE_STR("Failed to allocate a GPtrArray.");
161 BT_OBJECT_PUT_REF_AND_RESET(connection);
162 goto end;
163 }
164
165 /* Weak references are taken, see comment in header. */
166 connection->upstream_port = upstream_port;
167 connection->downstream_port = downstream_port;
168 BT_LOGD_STR("Setting upstream port's connection.");
169 bt_port_set_connection(upstream_port, connection);
170 BT_LOGD_STR("Setting downstream port's connection.");
171 bt_port_set_connection(downstream_port, connection);
172 bt_object_set_parent(&connection->base, &graph->base);
173 BT_LOGD("Created connection: "
174 "graph-addr=%p, upstream-port-addr=%p, uptream-port-name=\"%s\", "
175 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
176 "conn-addr=%p",
177 graph, upstream_port, bt_port_get_name(upstream_port),
178 downstream_port, bt_port_get_name(downstream_port),
179 connection);
180
181 end:
182 return connection;
183 }
184
185 BT_HIDDEN
186 void bt_connection_end(struct bt_connection *conn,
187 bool try_remove_from_graph)
188 {
189 struct bt_component *downstream_comp = NULL;
190 struct bt_component *upstream_comp = NULL;
191 struct bt_port *downstream_port = conn->downstream_port;
192 struct bt_port *upstream_port = conn->upstream_port;
193 struct bt_graph *graph = bt_connection_borrow_graph(conn);
194 size_t i;
195
196 BT_LOGD("Ending connection: conn-addr=%p, try-remove-from-graph=%d",
197 conn, try_remove_from_graph);
198
199 if (downstream_port) {
200 BT_LOGD("Disconnecting connection's downstream port: "
201 "port-addr=%p, port-name=\"%s\"",
202 downstream_port, bt_port_get_name(downstream_port));
203 downstream_comp = bt_port_get_component(downstream_port);
204 bt_port_set_connection(downstream_port, NULL);
205 conn->downstream_port = NULL;
206 }
207
208 if (upstream_port) {
209 BT_LOGD("Disconnecting connection's upstream port: "
210 "port-addr=%p, port-name=\"%s\"",
211 upstream_port, bt_port_get_name(upstream_port));
212 upstream_comp = bt_port_get_component(upstream_port);
213 bt_port_set_connection(upstream_port, NULL);
214 conn->upstream_port = NULL;
215 }
216
217 if (downstream_comp && conn->notified_downstream_port_connected) {
218 /* bt_component_port_disconnected() logs details */
219 bt_component_port_disconnected(downstream_comp,
220 downstream_port);
221 }
222
223 if (upstream_comp && conn->notified_upstream_port_connected) {
224 /* bt_component_port_disconnected() logs details */
225 bt_component_port_disconnected(upstream_comp, upstream_port);
226 }
227
228 BT_ASSERT(graph);
229
230 if (conn->notified_graph_ports_connected) {
231 /* bt_graph_notify_ports_disconnected() logs details */
232 bt_graph_notify_ports_disconnected(graph, upstream_comp,
233 downstream_comp, upstream_port, downstream_port);
234 }
235
236 bt_object_put_ref(downstream_comp);
237 bt_object_put_ref(upstream_comp);
238
239 /*
240 * Because this connection is ended, finalize (cancel) each
241 * notification iterator created from it.
242 */
243 for (i = 0; i < conn->iterators->len; i++) {
244 struct bt_notification_iterator_private_connection *iterator =
245 g_ptr_array_index(conn->iterators, i);
246
247 BT_LOGD("Finalizing notification iterator created by this ended connection: "
248 "conn-addr=%p, iter-addr=%p", conn, iterator);
249 bt_private_connection_notification_iterator_finalize(iterator);
250
251 /*
252 * Make sure this iterator does not try to remove itself
253 * from this connection's iterators on destruction
254 * because this connection won't exist anymore.
255 */
256 bt_private_connection_notification_iterator_set_connection(
257 iterator, NULL);
258 }
259
260 g_ptr_array_set_size(conn->iterators, 0);
261
262 if (try_remove_from_graph) {
263 bt_connection_try_remove_from_graph(conn);
264 }
265 }
266
267 struct bt_port *bt_connection_get_upstream_port(
268 struct bt_connection *connection)
269 {
270 return connection ? bt_object_get_ref(connection->upstream_port) : NULL;
271 }
272
273 struct bt_port *bt_connection_get_downstream_port(
274 struct bt_connection *connection)
275 {
276 return connection ? bt_object_get_ref(connection->downstream_port) : NULL;
277 }
278
279 enum bt_connection_status
280 bt_private_connection_create_notification_iterator(
281 struct bt_private_connection *private_connection,
282 struct bt_notification_iterator **user_iterator)
283 {
284 enum bt_component_class_type upstream_comp_class_type;
285 struct bt_notification_iterator_private_connection *iterator = NULL;
286 struct bt_port *upstream_port = NULL;
287 struct bt_component *upstream_component = NULL;
288 struct bt_component_class *upstream_comp_class = NULL;
289 struct bt_connection *connection = NULL;
290 bt_component_class_notification_iterator_init_method init_method = NULL;
291 enum bt_connection_status status;
292
293 if (!private_connection) {
294 BT_LOGW_STR("Invalid parameter: private connection is NULL.");
295 status = BT_CONNECTION_STATUS_INVALID;
296 goto end;
297 }
298
299 if (!user_iterator) {
300 BT_LOGW_STR("Invalid parameter: notification iterator pointer is NULL.");
301 status = BT_CONNECTION_STATUS_INVALID;
302 goto end;
303 }
304
305 connection = bt_connection_borrow_from_private(private_connection);
306
307 if (bt_graph_is_canceled(bt_connection_borrow_graph(connection))) {
308 BT_LOGW("Cannot create notification iterator from connection: "
309 "connection's graph is canceled: "
310 "conn-addr=%p, upstream-port-addr=%p, "
311 "upstream-port-name=\"%s\", upstream-comp-addr=%p, "
312 "upstream-comp-name=\"%s\", graph-addr=%p",
313 connection, connection->upstream_port,
314 bt_port_get_name(connection->upstream_port),
315 upstream_component,
316 bt_component_get_name(upstream_component),
317 bt_connection_borrow_graph(connection));
318 status = BT_CONNECTION_STATUS_GRAPH_IS_CANCELED;
319 goto end;
320 }
321
322 if (bt_connection_is_ended(connection)) {
323 BT_LOGW("Invalid parameter: connection is ended: "
324 "conn-addr=%p", connection);
325 status = BT_CONNECTION_STATUS_IS_ENDED;
326 goto end;
327 }
328
329 upstream_port = connection->upstream_port;
330 BT_ASSERT(upstream_port);
331 upstream_component = bt_port_get_component(upstream_port);
332 BT_ASSERT(upstream_component);
333 upstream_comp_class = upstream_component->class;
334 BT_LOGD("Creating notification iterator from connection: "
335 "conn-addr=%p, upstream-port-addr=%p, "
336 "upstream-port-name=\"%s\", upstream-comp-addr=%p, "
337 "upstream-comp-name=\"%s\"",
338 connection, connection->upstream_port,
339 bt_port_get_name(connection->upstream_port),
340 upstream_component, bt_component_get_name(upstream_component));
341 upstream_comp_class_type =
342 bt_component_get_class_type(upstream_component);
343 BT_ASSERT(upstream_comp_class_type == BT_COMPONENT_CLASS_TYPE_SOURCE ||
344 upstream_comp_class_type == BT_COMPONENT_CLASS_TYPE_FILTER);
345 status = bt_private_connection_notification_iterator_create(upstream_component,
346 upstream_port, connection, &iterator);
347 if (status != BT_CONNECTION_STATUS_OK) {
348 BT_LOGW("Cannot create notification iterator from connection.");
349 goto end;
350 }
351
352 switch (upstream_comp_class_type) {
353 case BT_COMPONENT_CLASS_TYPE_SOURCE:
354 {
355 struct bt_component_class_source *source_class =
356 container_of(upstream_comp_class,
357 struct bt_component_class_source, parent);
358 init_method = source_class->methods.iterator.init;
359 break;
360 }
361 case BT_COMPONENT_CLASS_TYPE_FILTER:
362 {
363 struct bt_component_class_filter *filter_class =
364 container_of(upstream_comp_class,
365 struct bt_component_class_filter, parent);
366 init_method = filter_class->methods.iterator.init;
367 break;
368 }
369 default:
370 /* Unreachable. */
371 BT_LOGF("Unknown component class type: type=%d",
372 upstream_comp_class_type);
373 abort();
374 }
375
376 if (init_method) {
377 enum bt_notification_iterator_status iter_status;
378
379 BT_LOGD("Calling user's initialization method: iter-addr=%p",
380 iterator);
381 iter_status = init_method(
382 bt_private_connection_private_notification_iterator_from_notification_iterator((void *) iterator),
383 bt_private_port_from_port(upstream_port));
384 BT_LOGD("User method returned: status=%s",
385 bt_notification_iterator_status_string(iter_status));
386 if (iter_status != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
387 BT_LOGW_STR("Initialization method failed.");
388 status = bt_connection_status_from_notification_iterator_status(
389 iter_status);
390 goto end;
391 }
392 }
393
394 iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE;
395 g_ptr_array_add(connection->iterators, iterator);
396 BT_LOGD("Created notification iterator from connection: "
397 "conn-addr=%p, upstream-port-addr=%p, "
398 "upstream-port-name=\"%s\", upstream-comp-addr=%p, "
399 "upstream-comp-name=\"%s\", iter-addr=%p",
400 connection, connection->upstream_port,
401 bt_port_get_name(connection->upstream_port),
402 upstream_component, bt_component_get_name(upstream_component),
403 iterator);
404
405 /* Move reference to user */
406 *user_iterator = (void *) iterator;
407 iterator = NULL;
408
409 end:
410 bt_object_put_ref(upstream_component);
411 bt_object_put_ref(iterator);
412 return status;
413 }
414
415 BT_HIDDEN
416 void bt_connection_remove_iterator(struct bt_connection *conn,
417 struct bt_notification_iterator_private_connection *iterator)
418 {
419 g_ptr_array_remove(conn->iterators, iterator);
420 BT_LOGV("Removed notification iterator from connection: "
421 "conn-addr=%p, iter-addr=%p", conn, iterator);
422 bt_connection_try_remove_from_graph(conn);
423 }
424
425 bt_bool bt_connection_is_ended(struct bt_connection *connection)
426 {
427 return !connection->downstream_port && !connection->upstream_port;
428 }
This page took 0.037563 seconds and 4 git commands to generate.