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