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