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