cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[babeltrace.git] / src / lib / graph / connection.c
CommitLineData
784cdc68 1/*
0235b0db
MJ
2 * SPDX-License-Identifier: MIT
3 *
e2f7325d 4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
784cdc68 5 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
784cdc68
JG
6 */
7
350ad6c1 8#define BT_LOG_TAG "LIB/CONNECTION"
c2d9d9cf 9#include "lib/logging.h"
a36bfb16 10
d98421f2 11#include "lib/assert-cond.h"
43c59509 12#include <babeltrace2/graph/connection.h>
578e048b
MJ
13#include "lib/object.h"
14#include "compat/compiler.h"
c4f23e30 15#include <stdbool.h>
0fbb9a9f 16#include <stdlib.h>
784cdc68
JG
17#include <glib.h>
18
578e048b
MJ
19#include "component.h"
20#include "connection.h"
21#include "graph.h"
5155b005 22#include "iterator.h"
578e048b
MJ
23#include "port.h"
24
784cdc68 25static
d94d92ac 26void destroy_connection(struct bt_object *obj)
784cdc68
JG
27{
28 struct bt_connection *connection = container_of(obj,
29 struct bt_connection, base);
bd14d768 30
3f7d4d90 31 BT_LIB_LOGI("Destroying connection: %!+x", connection);
a36bfb16 32
bd14d768 33 /*
d0fea130
PP
34 * Make sure that each message iterator which was created for
35 * this connection is finalized before we destroy it. Once a
9b4f9b42 36 * message iterator is finalized, you cannot use it.
bd14d768
PP
37 *
38 * Because connections are destroyed before components within a
d6e69534 39 * graph, this ensures that message iterators are always
bd14d768 40 * finalized before their upstream component.
c42ea0af
PP
41 *
42 * Ending the connection does exactly this. We pass `false` to
43 * bt_connection_end() here to avoid removing this connection
44 * from the graph: if we're here, we're already in the graph's
45 * destructor.
bd14d768 46 */
c42ea0af
PP
47 bt_connection_end(connection, false);
48 g_ptr_array_free(connection->iterators, TRUE);
d94d92ac 49 connection->iterators = NULL;
784cdc68
JG
50
51 /*
65300d60 52 * No bt_object_put_ref on ports as a connection only holds _weak_
c42ea0af 53 * references to them.
784cdc68
JG
54 */
55 g_free(connection);
56}
57
f167d3c0 58static
d94d92ac 59void try_remove_connection_from_graph(struct bt_connection *connection)
f167d3c0 60{
3fea54f6 61 void *graph = (void *) bt_object_borrow_parent(&connection->base);
f167d3c0 62
3fea54f6 63 if (connection->base.ref_count > 0 ||
f167d3c0
PP
64 connection->downstream_port ||
65 connection->upstream_port ||
66 connection->iterators->len > 0) {
67 return;
68 }
69
70 /*
71 * At this point we know that:
72 *
a36bfb16 73 * 1. The connection is ended (ports were disconnected).
d6e69534 74 * 2. All the message iterators that this connection
f167d3c0
PP
75 * created, if any, are finalized.
76 * 3. The connection's reference count is 0, so only the
77 * parent (graph) owns this connection after this call.
78 *
79 * In other words, no other object than the graph knows this
80 * connection.
81 *
82 * It is safe to remove the connection from the graph, therefore
83 * destroying it.
84 */
d94d92ac
PP
85 BT_LIB_LOGD("Removing self from graph's connections: "
86 "%![graph-]+g, %![conn-]+x", graph, connection);
f167d3c0
PP
87 bt_graph_remove_connection(graph, connection);
88}
89
90static
d94d92ac 91void parent_is_owner(struct bt_object *obj)
f167d3c0
PP
92{
93 struct bt_connection *connection = container_of(obj,
94 struct bt_connection, base);
95
d94d92ac 96 try_remove_connection_from_graph(connection);
890882ef
PP
97}
98
d94d92ac 99struct bt_connection *bt_connection_create(struct bt_graph *graph,
72b913fb
PP
100 struct bt_port *upstream_port,
101 struct bt_port *downstream_port)
784cdc68
JG
102{
103 struct bt_connection *connection = NULL;
104
3f7d4d90 105 BT_LIB_LOGI("Creating connection: "
d94d92ac
PP
106 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
107 graph, upstream_port, downstream_port);
784cdc68
JG
108 connection = g_new0(struct bt_connection, 1);
109 if (!connection) {
870631a2 110 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one connection.");
784cdc68
JG
111 goto end;
112 }
113
3fea54f6 114 bt_object_init_shared_with_parent(&connection->base,
d94d92ac 115 destroy_connection);
3fea54f6 116 bt_object_set_parent_is_owner_listener_func(&connection->base,
d94d92ac 117 parent_is_owner);
bd14d768
PP
118 connection->iterators = g_ptr_array_new();
119 if (!connection->iterators) {
870631a2 120 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
65300d60 121 BT_OBJECT_PUT_REF_AND_RESET(connection);
bd14d768
PP
122 goto end;
123 }
124
784cdc68 125 /* Weak references are taken, see comment in header. */
72b913fb
PP
126 connection->upstream_port = upstream_port;
127 connection->downstream_port = downstream_port;
d94d92ac 128 BT_LIB_LOGD("Setting upstream port's connection: %!+p", upstream_port);
72b913fb 129 bt_port_set_connection(upstream_port, connection);
d94d92ac
PP
130 BT_LIB_LOGD("Setting downstream port's connection: %!+p",
131 downstream_port);
72b913fb 132 bt_port_set_connection(downstream_port, connection);
3fea54f6 133 bt_object_set_parent(&connection->base, &graph->base);
3f7d4d90 134 BT_LIB_LOGI("Created connection: %!+x", connection);
a36bfb16 135
784cdc68
JG
136end:
137 return connection;
138}
139
d94d92ac 140void bt_connection_end(struct bt_connection *conn, bool try_remove_from_graph)
72b913fb 141{
72b913fb
PP
142 struct bt_port *downstream_port = conn->downstream_port;
143 struct bt_port *upstream_port = conn->upstream_port;
f167d3c0 144 size_t i;
72b913fb 145
3f7d4d90 146 BT_LIB_LOGI("Ending connection: %!+x, try-remove-from-graph=%d",
c42ea0af
PP
147 conn, try_remove_from_graph);
148
d94d92ac 149 /*
d6e69534 150 * Any of the following message callback functions could
d94d92ac
PP
151 * remove one of the connection's ports from its component. To
152 * make sure that at least logging in called functions works
153 * with existing objects, get a local reference on both ports.
154 */
155 bt_object_get_ref(downstream_port);
156 bt_object_get_ref(upstream_port);
157
72b913fb 158 if (downstream_port) {
d94d92ac
PP
159 BT_LIB_LOGD("Disconnecting connection's downstream port: %!+p",
160 downstream_port);
72b913fb
PP
161 bt_port_set_connection(downstream_port, NULL);
162 conn->downstream_port = NULL;
163 }
164
165 if (upstream_port) {
d94d92ac
PP
166 BT_LIB_LOGD("Disconnecting connection's upstream port: %!+p",
167 upstream_port);
72b913fb
PP
168 bt_port_set_connection(upstream_port, NULL);
169 conn->upstream_port = NULL;
170 }
171
d94d92ac
PP
172 /*
173 * It is safe to put the local port references now that we don't
174 * need them anymore. This could indeed destroy them.
175 */
176 bt_object_put_ref(downstream_port);
177 bt_object_put_ref(upstream_port);
f167d3c0
PP
178
179 /*
d0fea130
PP
180 * Because this connection is ended, finalize each message
181 * iterator created from it.
182 *
183 * In practice, this only happens when the connection is
184 * destroyed and not all its message iterators were finalized,
185 * which is on graph destruction.
f167d3c0
PP
186 */
187 for (i = 0; i < conn->iterators->len; i++) {
9a2c8b8e 188 struct bt_message_iterator *iterator =
f167d3c0
PP
189 g_ptr_array_index(conn->iterators, i);
190
d6e69534 191 BT_LIB_LOGD("Finalizing message iterator created by "
d94d92ac 192 "this ended connection: %![iter-]+i", iterator);
9a2c8b8e 193 bt_message_iterator_try_finalize(
d94d92ac 194 iterator);
f167d3c0
PP
195
196 /*
197 * Make sure this iterator does not try to remove itself
198 * from this connection's iterators on destruction
199 * because this connection won't exist anymore.
200 */
9a2c8b8e 201 bt_message_iterator_set_connection(
90157d89 202 iterator, NULL);
f167d3c0
PP
203 }
204
205 g_ptr_array_set_size(conn->iterators, 0);
c42ea0af
PP
206
207 if (try_remove_from_graph) {
d94d92ac 208 try_remove_connection_from_graph(conn);
c42ea0af 209 }
72b913fb
PP
210}
211
1353b066 212BT_EXPORT
0d72b8c3
PP
213const struct bt_port_output *bt_connection_borrow_upstream_port_const(
214 const struct bt_connection *connection)
784cdc68 215{
d5b13b9b 216 BT_ASSERT_PRE_DEV_CONN_NON_NULL(connection);
d94d92ac 217 return (void *) connection->upstream_port;
784cdc68
JG
218}
219
1353b066 220BT_EXPORT
0d72b8c3
PP
221const struct bt_port_input *bt_connection_borrow_downstream_port_const(
222 const struct bt_connection *connection)
784cdc68 223{
d5b13b9b 224 BT_ASSERT_PRE_DEV_CONN_NON_NULL(connection);
d94d92ac 225 return (void *) connection->downstream_port;
784cdc68 226}
bd14d768 227
bd14d768 228void bt_connection_remove_iterator(struct bt_connection *conn,
9a2c8b8e 229 struct bt_message_iterator *iterator)
bd14d768
PP
230{
231 g_ptr_array_remove(conn->iterators, iterator);
3f7d4d90 232 BT_LIB_LOGD("Removed message iterator from connection: "
d94d92ac
PP
233 "%![conn-]+x, %![iter-]+i", conn, iterator);
234 try_remove_connection_from_graph(conn);
bd14d768 235}
090a4c0a 236
1353b066 237BT_EXPORT
c5b9b441
PP
238void bt_connection_get_ref(const struct bt_connection *connection)
239{
240 bt_object_get_ref(connection);
241}
242
1353b066 243BT_EXPORT
c5b9b441
PP
244void bt_connection_put_ref(const struct bt_connection *connection)
245{
246 bt_object_put_ref(connection);
247}
This page took 0.102504 seconds and 4 git commands to generate.